#include <bits/stdc++.h>
using namespace std;
int T, n;
struct node {
  int l, r;
} seg[3101];
int lis[10001], lisn;
vector<int> s[6101];
int f[6101][6101];
int work(int l, int r) {
  if (l > r) return 0;
  if (f[l][r] != -1) return f[l][r];
  int cnt = 0;
  for (auto x : s[l])
    if (x == r) cnt++;
  f[l][r] = cnt + work(l + 1, r);
  for (auto x : s[l])
    if (x < r) f[l][r] = max(f[l][r], cnt + work(l, x) + work(x + 1, r));
  return f[l][r];
}
void solve() {
  lisn = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &seg[i].l, &seg[i].r);
    lis[++lisn] = seg[i].l;
    lis[++lisn] = seg[i].r;
  }
  sort(lis + 1, lis + 1 + lisn);
  lisn = unique(lis + 1, lis + 1 + lisn) - lis - 1;
  for (int i = 0; i <= lisn; i++) {
    s[i].clear();
    for (int j = 0; j <= lisn; j++) f[i][j] = -1;
  }
  for (int i = 1; i <= n; i++) {
    seg[i].l = lower_bound(lis + 1, lis + 1 + lisn, seg[i].l) - lis;
    seg[i].r = lower_bound(lis + 1, lis + 1 + lisn, seg[i].r) - lis;
    s[seg[i].l].push_back(seg[i].r);
  }
  printf("%d\n", work(1, lisn));
}
int main() {
  scanf("%d", &T);
  while (T--) {
    solve();
  }
}
