#include <bits/stdc++.h>
using namespace std;
void tempwj() {
  freopen("hash.in", "r", stdin);
  freopen("hash.out", "w", stdout);
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long qpow(long long a, long long b, long long mod) {
  a %= mod;
  long long ans = 1;
  while (b > 0) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
struct cmp {
  bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
    return a.second > b.second;
  }
};
int lb(int x) { return x & -x; }
const int inf = INT_MAX;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long mod = 2010 + 7;
const int maxn = 6005;
const int M = 1e6 + 10;
int dp[maxn][maxn];
bool num[maxn][maxn];
std::vector<int> vv[maxn];
pair<int, int> pp[maxn];
std::vector<int> v;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    v.clear();
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &pp[i].first, &pp[i].second);
      v.push_back(pp[i].first);
      v.push_back(pp[i].second);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    int m = v.size();
    for (int i = 0; i <= m; i++) {
      vv[i].clear();
    }
    for (int i = 0; i <= m; i++) {
      for (int j = 0; j <= m; j++) num[i][j] = dp[i][j] = 0;
    }
    for (int i = 1; i <= n; i++) {
      pp[i].first =
          lower_bound(v.begin(), v.end(), pp[i].first) - v.begin() + 1;
      pp[i].second =
          lower_bound(v.begin(), v.end(), pp[i].second) - v.begin() + 1;
    }
    for (int i = 1; i <= n; i++) {
      num[pp[i].first][pp[i].second] = 1;
      vv[pp[i].first].push_back(pp[i].second);
    }
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j + i - 1 <= m; j++) {
        int r = i + j - 1;
        dp[j][r] = max(dp[j + 1][r], dp[j][r - 1]);
        for (int k = 0; k < vv[j].size(); k++) {
          int ed = vv[j][k];
          if (ed > r) continue;
          dp[j][r] = max(dp[j][ed] + dp[ed + 1][r], dp[j][r]);
        }
        dp[j][r] += num[j][r];
      }
    }
    printf("%d\n", dp[1][m]);
  }
  return 0;
}
