#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <set>
#define mp make_pair
#define heap priority_queue
using namespace std;
const int maxn = 40000;
const int maxh = 100;
int n,l[maxn+1],r[maxn+1],maxi,x[maxn*maxh+1],y[maxn*maxh+1],c[maxn*maxh+1],vt[maxn+1];
void enter()
{
	heap <pair<int,int*> > x;
	set <int> s;
	cin >> n;
	for (int i=1; i<=n; i++)
	cin >> l[i] >> r[i];
	for (int i=1; i<=n; i++)
	{
		x.push(mp(l[i],&l[i]));
		x.push(mp(r[i],&r[i]));
		s.insert(l[i]);
		s.insert(r[i]);
	}
	int pre=-1;
	int i=s.size()+1;
	maxi=i-1;
	while (!x.empty())
	{
		pair<int,int*> y=x.top();
		if (y.first!=pre) 
		{
			pre=y.first;
			i--;
		}
		*y.second=i;
		x.pop();
	}
	/*for (int i=1; i<=n; i++)
	cout << l[i] << ' ' << r[i] << '\n';
	cout << '\n';*/
}
void init(int i,int l, int r)
{
	x[i]=l;
	y[i]=r;
	c[i]=0;
	if (l!=r)
	{
		int mid=(l+r)/2;
		init(i*2,l,mid);
		init(i*2+1,mid+1,r);
	}
	else vt[l]=i;
}
void update(int i, int l, int r, int co)
{
	if ((l<=x[i]) and (y[i]<=r)) c[i]=co;
	else
	if (!((y[i]<l) or (x[i]>r)))
	{
		update(i*2,l,r,co);
		update(i*2+1,l,r,co);
	}
}
int get(int i)
{
	if (i==1) return c[i];
	else
	return max(c[i],get(i/2));
}
void solve()
{
	set <int> s;
	init(1,1,maxi);
	for (int i=1; i<=n; i++)
	update(1,l[i],r[i],i);
	for (int i=1; i<=maxi; i++)
	{
		int p=get(vt[i]);
		if (p!=0) s.insert(p);
		//cout << i << ' ' << get(vt[i]) << '\n';
	}
	cout << s.size() << '\n';
}
int main()
{
	ios_base::sync_with_stdio(0);
	//freopen("POSTERS.INP","r",stdin);
	//freopen("POSTERS.OUT","w",stdout);
	int t;
	cin >> t;
	for (int i=1; i<=t; i++)
	{
		enter();
		solve();
	}
	return 0;
}