#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]);
            }
        }
        cout<<res<<"\n";
    }
}