#define ALL(v) v.begin(), v.end()
#define CLR(array) memset(array, 0, sizeof(array))
#define DEBUG cout << "bug "
#define Fr first
#define FOR(i, n) for(int i = 0; i < n; i++)
#define MEM(array) memset(array, 0x3f, sizeof(array))
#define ls L(id)
#define lson id << 1, l, mid
#define OFF(array) memset(array, -1, sizeof(array))
#define PR(x) cout << #x << " = " << x << " "
#define PRLN(x) cout << #x << " = " << x << endl
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define root 1, 1, n
#define rs R(id)
#define rson id << 1 | 1, mid, r
#define Rep(i, a, b) for(int i = a; i >= b; i--)
#define repk(i, a, b) for(i = a; i <= b; i++)
#define Repk(i, a, b) for(i = a; i >= b; i--)
#define Sc second
#define SZ size()
using namespace std;
inline int L(int x){ return x << 1; }
inline int R(int x){ return x << 1 | 1; }
inline int MID(int l, int r){ return l + r >> 1; }
typedef long long ll;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f, maxn = 2e4 + 5;
int color[maxn << 2];
int x[maxn >> 1], y[maxn >> 1];
int vis[maxn >> 1];
void push_down(int id) {
color[ls] = color[rs] = color[id];
color[id]= -1;
}
void update(int id, int l, int r, int a, int b, int val) {
if(a <= l && r <= b) {
color[id] = val;
return;
}
if(r > l && ~color[id]) push_down(id);
int mid = MID(l, r);
if(mid > a) update(lson, a, b, val);
if(mid < b) update(rson, a, b, val);
}
void query(int id, int l ,int r) {
if(~color[id]) {
vis[color[id]] = 1;
return;
}
if(l + 1 == r) return;
int mid = MID(l, r);
query(lson);
query(rson);
}
int compress(int m) {
vector<int> v;
FOR(i, m) {
scanf("%d%d", x + i, y + i);
y[i]++;
v.push_back(x[i]);
v.push_back(y[i]);
}
sort(ALL(v));
v.resize(unique(ALL(v)) - v.begin());
FOR(i, m) {
x[i] = 1 + lower_bound(ALL(v), x[i]) - v.begin();
y[i] = 1 + lower_bound(ALL(v), y[i]) - v.begin();
}
return v.SZ;
}
int solve(void) {
int n, m, ans = 0;
scanf("%d", &m);
n = compress(m);
color[1] == m;
FOR(i, m) update(root, x[i], y[i], i);
CLR(vis);
query(root);
FOR(i, m) if(vis[i]) ans++;
return ans;
}
int main(void) {
int t; scanf("%d", &t);
while(t--) printf("%d\n", solve());
return 0;
}