#include <bits/stdc++.h>
using namespace std;
const int N = 6005;
int n, q[N], l[N], r[N];
int f[N], g[N];
vector<pair<int, int> > e[N];
void solve() {
  *q = 0;
  scanf("%d", &n);
  for (int i = (int)(1); i <= (int)(n); i++) {
    scanf("%d%d", &l[i], &r[i]);
    l[i] = l[i] * 2;
    r[i] = r[i] * 2 + 1;
  }
  l[++n] = 0;
  r[n] = 1e9;
  for (int i = (int)(1); i <= (int)(n); i++) q[++*q] = l[i], q[++*q] = r[i];
  for (int i = (int)(1); i <= (int)(n * 2); i++) e[i].resize(0);
  sort(q + 1, q + *q + 1);
  for (int i = (int)(1); i <= (int)(n); i++) {
    l[i] = lower_bound(q + 1, q + *q + 1, l[i]) - q;
    r[i] = lower_bound(q + 1, q + *q + 1, r[i]) - q;
    e[r[i]].push_back(pair<int, int>(l[i], i));
    g[i] = 0;
  }
  int ans = 0;
  for (int i = (int)(1); i <= (int)(*q); i++) {
    sort(e[i].begin(), e[i].end());
    reverse(e[i].begin(), e[i].end());
    for (auto j : e[i]) {
      f[j.first - 1] = 0;
      for (int k = (int)(j.first); k <= (int)(i); k++) {
        f[k] = f[k - 1];
        for (auto l : e[k])
          if (l.first >= j.first) f[k] = max(f[k], f[l.first] + g[l.second]);
      }
      g[j.second] = f[i] + 1;
      ans = max(ans, f[i] + 1);
    }
  }
  cout << ans - 1 << endl;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
}
