#include<bits/stdc++.h>
using namespace std;
#define rep(i,s,t) for (int i=s;i<=t;i++)
#define pi acos(-1)
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<double, double> PDD;
typedef pair<PII, PII> PPP;
typedef pair<PII, int> PPI;
#define repp(i,s,t) for (int i=s;i>=t;i--)
template<class T> T sqr(T x) {return x*x;}
#define debug(x) cerr<<#x"="<<(x)<<endl;
#define pb(x) push_back(x);
#define ori(x) x-'a'
const int maxn = 2001;
int bit[1010][1010];
PII a[maxn];
int tmp[maxn];
int cns;
int n;
void insert(int *tree, int pos, int x)
{
	while (pos <= cns)
	{
		tree[pos] = max(tree[pos], x);
		pos += pos & -pos;
	}
}
int query(int *tree, int pos)
{
	int ret = 0;
	while (pos)
	{
		ret = max(tree[pos], ret);
		pos -= pos & -pos;
	}
	return ret;
}
int main()
{
	int testdata;
	scanf("%d",&testdata);
	map<int,int> hashs;
	while (testdata--)
	{
		int ans = 0;
		memset(bit,0,sizeof(bit));
		hashs.clear();
		scanf("%d",&n);
		rep(i,1,n)
		{
			scanf("%d%d",&a[i].first,&a[i].second);
			a[i].first = -a[i].first;
			hashs[a[i].second] = 1;
		}
		cns = 0;
		map<int,int>::iterator xx;
		for (xx = hashs.begin(); xx != hashs.end(); xx++)
			xx->second = ++cns;
		rep(i,1,n) a[i].second = hashs[a[i].second];
		sort(a+1,a+1+n);
		rep(i,1,n) 
		{
		//	cout << i << endl;
			int tmpd = a[i].second;
			rep(j,1,cns)
			{
				tmp[j] = query(bit[j], tmpd) + 1;
		//		cout << j << " " << tmp[j] << endl;
			}
			rep(j,1,cns)
			{
				ans = max(ans, tmp[j]);
				insert(bit[j], tmpd , tmp[j]);
				insert(bit[tmpd], j, tmp[j]);
			}
		}
		cout << ans << endl;

	}
}

