#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double eps = 1e-9;
const double PI = atan(1.0);
#define readFile freopen("input","r",stdin);
#define writeFile freopen("output","w",stdout);
#define fastIO ios::sync_with_stdio(0);
typedef pair<int,int> ii;
typedef unsigned long long ULL;
const int N = 100000;
int tree [N*4];
map<int,int> hash;
int lazy[N*4];
ii queries[N];
int n;
int query(int node,int l,int r,int ll,int rr){
if (lazy[node]){
tree[node] = lazy[node];
if (l!=r){
lazy[node<<1] = lazy[node];
lazy[node<<1|1] = lazy[node];
}
lazy[node]=0;
}
if (l>rr || r<ll) return 1;
if (l>=ll && r<=rr){
return tree[node];
}
int mid = (l+r)>>1;
return tree[node] = query(node<<1,l,mid,ll,rr)&query(node<<1|1,mid+1,r,ll,rr);
}
int update(int node,int l,int r,int ll,int rr){
if (lazy[node]){
tree[node] = lazy[node];
if (l!=r){
lazy[node<<1] = lazy[node];
lazy[node<<1|1] = lazy[node];
}
lazy[node]=0;
}
if (l>rr || r<ll) return tree[node];
if (l>=ll && r<=rr){
if (l!=r){
lazy[node<<1]=1;
lazy[node<<1|1]=1;
}
return tree[node] = 1;
}
int mid = (l+r)>>1;
tree[node] = update(node<<1,l,mid,ll,rr)&update(node<<1|1,mid+1,r,ll,rr);
}
int main(){
#ifndef ONLINE_JUDGE
readFile;
#endif
fastIO;
int t; cin>>t;
while(t--){
cin>>n;
hash.clear();
memset(lazy,0,sizeof(lazy));
memset(tree,0,sizeof(tree));
vector<int> tmp;
for(int i=1;i<=n;i++){
int a,b; cin>>a>>b;
if (a>b) swap(a,b);
queries[i] = make_pair(a,b);
tmp.push_back(a);
tmp.push_back(b);
}
sort(tmp.begin(),tmp.end());
int pointer=1;
for(int i=0;i<tmp.size();i++){
if (!hash[tmp[i]])
hash[tmp[i]] = pointer++;
}
int res=0;
for(int i=n;i>=1;i--){
if (!query(1,1,pointer,hash[queries[i].first],hash[queries[i].second])){
res++;
update(1,1,pointer,hash[queries[i].first],hash[queries[i].second]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WlkLGxsLHJyKSZxdWVyeShub2RlPDwxfDEsbWlkKzEscixsbCxycik7Cn0KCmludCB1cGRhdGUoaW50IG5vZGUsaW50IGwsaW50IHIsaW50IGxsLGludCBycil7CiAgICBpZiAobGF6eVtub2RlXSl7CiAgICAgICAgdHJlZVtub2RlXSA9IGxhenlbbm9kZV07CiAgICAgICAgaWYgKGwhPXIpewogICAgICAgICAgICBsYXp5W25vZGU8PDFdID0gbGF6eVtub2RlXTsKICAgICAgICAgICAgbGF6eVtub2RlPDwxfDFdID0gbGF6eVtub2RlXTsKICAgICAgICB9CiAgICAgICAgbGF6eVtub2RlXT0wOwogICAgfQogICAgaWYgKGw+cnIgfHwgcjxsbCkgcmV0dXJuIHRyZWVbbm9kZV07CiAgICBpZiAobD49bGwgJiYgcjw9cnIpewogICAgICAgIGlmIChsIT1yKXsKICAgICAgICAgICAgbGF6eVtub2RlPDwxXT0xOwogICAgICAgICAgICBsYXp5W25vZGU8PDF8MV09MTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHRyZWVbbm9kZV0gPSAxOwogICAgfQogICAgaW50IG1pZCA9IChsK3IpPj4xOwogICAgdHJlZVtub2RlXSA9IHVwZGF0ZShub2RlPDwxLGwsbWlkLGxsLHJyKSZ1cGRhdGUobm9kZTw8MXwxLG1pZCsxLHIsbGwscnIpOwp9CgoKCmludCBtYWluKCl7CiNpZm5kZWYgT05MSU5FX0pVREdFCiAgICByZWFkRmlsZTsKI2VuZGlmCiAgICBmYXN0SU87CiAgICBpbnQgdDsgY2luPj50OwogICAgd2hpbGUodC0tKXsKICAgICAgICBjaW4+Pm47CiAgICAgICAgaGFzaC5jbGVhcigpOwogICAgICAgIG1lbXNldChsYXp5LDAsc2l6ZW9mKGxhenkpKTsKICAgICAgICBtZW1zZXQodHJlZSwwLHNpemVvZih0cmVlKSk7CiAgICAgICAgdmVjdG9yPGludD4gdG1wOwogICAgICAgIGZvcihpbnQgaT0xO2k8PW47aSsrKXsKICAgICAgICAgICAgaW50IGEsYjsgY2luPj5hPj5iOwogICAgICAgICAgICBpZiAoYT5iKSBzd2FwKGEsYik7CiAgICAgICAgICAgIHF1ZXJpZXNbaV0gPSBtYWtlX3BhaXIoYSxiKTsKICAgICAgICAgICAgdG1wLnB1c2hfYmFjayhhKTsKICAgICAgICAgICAgdG1wLnB1c2hfYmFjayhiKTsKICAgICAgICB9CiAgICAgICAgc29ydCh0bXAuYmVnaW4oKSx0bXAuZW5kKCkpOwogICAgICAgIGludCBwb2ludGVyPTE7CiAgICAgICAgZm9yKGludCBpPTA7aTx0bXAuc2l6ZSgpO2krKyl7CiAgICAgICAgICAgIGlmICghaGFzaFt0bXBbaV1dKSAKICAgICAgICAgICAgICAgIGhhc2hbdG1wW2ldXSA9IHBvaW50ZXIrKzsKICAgICAgICB9CiAgICAgICAgaW50IHJlcz0wOwogICAgICAgIGZvcihpbnQgaT1uO2k+PTE7aS0tKXsKICAgICAgICAgICAgaWYgKCFxdWVyeSgxLDEscG9pbnRlcixoYXNoW3F1ZXJpZXNbaV0uZmlyc3RdLGhhc2hbcXVlcmllc1tpXS5zZWNvbmRdKSl7CiAgICAgICAgICAgICAgICByZXMrKzsKICAgICAgICAgICAgICAgIHVwZGF0ZSgxLDEscG9pbnRlcixoYXNoW3F1ZXJpZXNbaV0uZmlyc3RdLGhhc2hbcXVlcmllc1tpXS5zZWNvbmRdKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBjb3V0PDxyZXM8PCJcbiI7CiAgICB9Cn0=