#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 40005;
int Hash[maxn];//记录离散化后的数据
struct Post{int l, r;}p[maxn];//记录海报
struct Tree
{
int L, R;
bool isCover;//记录这段区间是否被覆盖
int Mid(){return (L+R)/2;}
}tree[maxn*4];
void Up(int root)//向上更新,如果左右子树都被覆盖,那么他也会被覆盖
{
if(tree[root].L != tree[root].R)
if(tree[root<<1].isCover && tree[root<<1|1].isCover)
tree[root].isCover = true;
}
void Build(int root, int L, int R)
{
tree[root].L = L, tree[root].R = R;
tree[root].isCover = false;
if(L == R)return ;
Build(root<<1, L, tree[root].Mid());
Build(root<<1|1, tree[root].Mid()+1, R);
}
//如果区间能内还有位置返回真,否则返回假
bool Insert(int root, int L, int R)
{
if(tree[root].isCover)return false;
if(tree[root].L == L && tree[root].R == R)
{
tree[root].isCover = true;
return true;
}
bool ans;
if(R <= tree[root].Mid())
ans = Insert(root<<1, L, R);
else if(L > tree[root].Mid())
ans = Insert(root<<1|1, L, R);
else
{
bool Lson = Insert(root<<1, L, tree[root].Mid());
bool Rson = Insert(root<<1|1, tree[root].Mid()+1, R);
ans = Lson | Rson;
}
Up(root);
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int i, N, nh=0;
scanf("%d", &N);
for(i=1; i<=N; i++)
{
scanf("%d%d", &p[i].l, &p[i].r);
Hash[nh++] = p[i].l, Hash[nh++] = p[i].l-1;
Hash[nh++] = p[i].r, Hash[nh++] = p[i].r+1;
}
sort(Hash, Hash+nh);
nh = unique(Hash, Hash+nh) - Hash;
Build(1, 1, nh);
int ans = 0;
for(i=N; i>0; i--)
{
int l = lower_bound(Hash, Hash+nh, p[i].l) - Hash;
int r = lower_bound(Hash, Hash+nh, p[i].r) - Hash;
if(Insert(1, l, r) == true)
ans += 1;
}
printf("%d\n", ans);
}
return 0;
}