UVA11368




//
//  UVA 11368
//  Difficult: Hard  
//    Algorithm: Sort LIS
//  Space: O(n)
//    Time: nO(logn) + O(n) + O(n) = nO(logn)
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;

struct A
{
    int w, h;
}point[20010];

bool compare(A a, A b)              

{
    if (a.w == b.w) 
        return a.h < b.h;
    return a.w > b.w;
}

int dp[20010];

int main()
{
    int t;

    scanf("%d", &t);
    while (t--)
    {
        int m;
        scanf("%d", &m);
        for (int i = 0; i < m; ++i)
        {
            scanf("%d%d", &point[i].w, &point[i].h);
        }

        sort(point, point + m, compare);

        fill(dp, dp + m, INF);                    // 初始化 dp 數組,初始化為最大值


        // 利用 nlog(n) 的時間複雜度求解最長上升子序列
        for (int i = 0; i < m; ++i)           
        {
            *upper_bound(dp, dp + m, point[i].h) = point[i].h;

        }

        printf("%ld\n", lower_bound(dp, dp + m, INF) - dp);   // 輸出最長上升子序列的長度
    }
    system("PAUSE");
}