#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
  t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
  t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
const long long MOD2 = (long long)1000000007 * (long long)1000000007;
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int& x, int y, int mod = 1000000007) {
  x += y;
  if (x >= mod) x -= mod;
  if (x < 0) x += mod;
  assert(x >= 0 && x < mod);
}
void et(int x = -1) {
  printf("%d\n", x);
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
int x[(3005)], y[(3005)];
vector<int> pos[(3005) * 2];
void fmain(int tid) {
  scanf("%d", &n);
  int cnt = 0;
  map<int, int> mp;
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    scanf("%d%d", x + i, y + i);
    mp[x[i]];
    mp[y[i]];
  }
  for (auto& p : mp) p.second = ++cnt;
  vector<vector<bool>> a(cnt + 1, vector<bool>(cnt + 1, 0));
  for (int(i) = 1; (i) <= (int)(cnt); (i)++) pos[i].clear();
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    x[i] = mp[x[i]];
    y[i] = mp[y[i]];
    a[x[i]][y[i]] = 1;
    pos[x[i]].push_back(y[i]);
  }
  vector<vector<int>> dp(cnt + 2, vector<int>(cnt + 2, 0));
  for (int len = 1; len <= cnt; len++) {
    for (int i = 1; i + len - 1 <= cnt; i++) {
      int j = i + len - 1;
      int sg = a[i][j];
      maxtt(dp[i][j], dp[i + 1][j] + sg);
      for (int z : pos[i])
        if (z < j) {
          maxtt(dp[i][j], dp[i][z] + dp[z + 1][j] + sg);
        }
    }
  }
  printf("%d\n", dp[1][cnt]);
}
int main() {
  int t = 1;
  scanf("%d", &t);
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
