#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n, m, cas, k, cnt, tot, ans;
struct node {
  int l, r, len, id;
} a[N];
vector<node> b[N];
int num[N], f[N], dp[N];
char s[N];
bool in[N];
inline bool cmp(const node &a, const node &b) { return a.len < b.len; }
void ini() {
  scanf("%d", &n);
  tot = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a[i].l, &a[i].r);
    a[i].id = i;
    num[++tot] = a[i].l;
    num[++tot] = a[i].r;
  }
  sort(num + 1, num + 1 + tot);
  tot = unique(num + 1, num + 1 + tot) - num - 1;
  for (int i = 1; i <= n; i++) {
    a[i].l = lower_bound(num + 1, num + 1 + tot, a[i].l) - num;
    a[i].r = lower_bound(num + 1, num + 1 + tot, a[i].r) - num;
    a[i].len = a[i].r - a[i].l + 1;
  }
}
inline void solve() {
  ++n;
  a[n] = node{1, tot, tot, n};
  sort(a + 1, a + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    for (int j = a[i].l - 1; j <= a[i].r; j++) dp[j] = 0, b[j].clear();
    for (int j = 1; j < i; j++)
      if (a[i].l <= a[j].l && a[j].r <= a[i].r) b[a[j].r].push_back(a[j]);
    for (int j = a[i].l; j <= a[i].r; j++) {
      dp[j] = dp[j - 1];
      for (node d : b[j]) dp[j] = max(dp[d.l - 1] + f[d.id], dp[j]);
    }
    f[a[i].id] = dp[a[i].r] + 1;
  }
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    ini();
    solve();
    printf("%d\n", f[n] - 1);
  }
  return 0;
}
