#include <bits/stdc++.h>
const int MAXN = 3e3 + 19;
int n, l[MAXN], r[MAXN];
int f[MAXN << 1], t[MAXN];
int tmp[MAXN << 1], tot, len[MAXN], cor[MAXN];
inline bool cmpLen(int a, int b) { return r[a] - l[a] < r[b] - l[b]; }
inline bool cmpCor(int a, int b) { return r[a] < r[b]; }
int main() {
  std::ios::sync_with_stdio(0);
  int T;
  std::cin >> T;
  while (T--) {
    std::cin >> n;
    tot = 0;
    for (int i = 1; i <= n; ++i) {
      std::cin >> l[i] >> r[i];
      tmp[++tot] = l[i], tmp[++tot] = r[i];
      len[i] = i, cor[i] = i;
      t[i] = 0;
    }
    std::sort(tmp + 1, tmp + 1 + tot);
    tot = std::unique(tmp + 1, tmp + 1 + tot) - tmp - 1;
    for (int i = 1; i <= n; ++i) {
      l[i] = std::lower_bound(tmp + 1, tmp + 1 + tot, l[i]) - tmp;
      r[i] = std::lower_bound(tmp + 1, tmp + 1 + tot, r[i]) - tmp;
    }
    std::sort(len + 1, len + 1 + n, cmpLen);
    std::sort(cor + 1, cor + 1 + n, cmpCor);
    ++n;
    l[n] = 1, r[n] = tot;
    len[n] = n, cor[n] = n, t[n] = 0;
    for (int i = 1; i <= n; ++i) {
      f[l[len[i]] - 1] = 0;
      int p = 1;
      while (p <= n && r[cor[p]] < l[len[i]]) ++p;
      for (int j = l[len[i]]; j <= r[len[i]]; ++j) {
        f[j] = f[j - 1];
        while (p <= n && r[cor[p]] == j) {
          if (cor[p] != len[i] && l[cor[p]] >= l[len[i]])
            f[j] = std::max(f[j], f[l[cor[p]] - 1] + t[cor[p]]);
          ++p;
        }
      }
      t[len[i]] = f[r[len[i]]] + 1;
    }
    std::cout << t[n] - 1 << std::endl;
  }
  return 0;
}
