#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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8YWxn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