#include <bits/stdc++.h>
struct Segment {
  int l, r;
  int value;
};
Segment seg[3005];
int map[6005];
int f[6005];
int in[3005];
std::vector<int> g[3005];
int queue[3005];
int value[3005];
std::vector<int> son[3005];
Segment t[3005];
int bit[6005];
int getId(int u, int n) {
  int L = 1, R = n;
  int res = -1;
  while (L <= R) {
    int M = (L + R) / 2;
    if (map[M] <= u) {
      res = M;
      L = M + 1;
    } else
      R = M - 1;
  }
  return res;
}
void addEdge(int u, int v) {
  in[v]++;
  g[u].push_back(v);
}
void preWork(int n, int size) {
  std::sort(map + 1, map + 1 + size);
  int p = 1;
  int mSize = 0;
  while (p <= size) {
    int np = p;
    while (np + 1 <= size && map[np + 1] == map[p]) np++;
    map[++mSize] = map[p];
    p = np + 1;
  }
  for (int i = 1; i <= n; i++) {
    seg[i].l = getId(seg[i].l, mSize);
    seg[i].r = getId(seg[i].r, mSize);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i != j) {
        if (seg[j].l >= seg[i].l && seg[j].r <= seg[i].r) {
          addEdge(j, i);
          son[i].push_back(j);
        }
      }
    }
  }
}
void initBit(int n) {
  for (int i = 1; i <= n; i++) bit[i] = 0;
}
int max(int a, int b) { return a > b ? a : b; }
void change(int p, int v, int n) {
  while (p <= n) {
    bit[p] = max(bit[p], v);
    p += p & -p;
  }
}
int ask(int p) {
  int res = 0;
  while (p != 0) {
    res = max(res, bit[p]);
    p -= p & -p;
  }
  return res;
}
void doDP(int u) {
  int size = 0;
  for (int v : son[u]) {
    t[++size] = seg[v];
    t[size].value = value[v];
  }
  std::sort(t + 1, t + 1 + size,
            [&](Segment& u, Segment& v) { return u.r < v.r; });
  int ans = 0;
  int upper = 6002;
  initBit(upper);
  for (int i = 1; i <= size; i++) {
    int l = t[i].l, r = t[i].r;
    int cnt = t[i].value;
    int maxn = ask(l - 1);
    int res = cnt + maxn;
    if (res > ans) ans = res;
    change(r, res, upper);
  }
  value[u] = ans + 1;
}
void topo(int n) {
  int h = 0, t = -1;
  for (int i = 1; i <= n; i++) {
    if (in[i] == 0) queue[++t] = i;
  }
  while (h <= t) {
    int u = queue[h++];
    doDP(u);
    for (int v : g[u]) {
      in[v]--;
      if (in[v] == 0) queue[++t] = v;
    }
  }
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    int size = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &seg[i].l, &seg[i].r);
      map[++size] = seg[i].l;
      map[++size] = seg[i].r;
    }
    n++;
    seg[n].l = 1, seg[n].r = 2000006;
    map[++size] = seg[n].l;
    map[++size] = seg[n].r;
    for (int i = 1; i <= n; i++) son[i].clear(), g[i].clear(), in[i] = 0;
    preWork(n, size);
    topo(n);
    printf("%d\n", value[n] - 1);
  }
  return 0;
}
