#include <bits/stdc++.h>
using namespace std;
int t, n;
pair<int, int> v[3005];
vector<int> g[6005];
map<int, int> ind, f;
int dp[6005][6005];
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  for (; t; t--) {
    cin >> n;
    ind.clear();
    f.clear();
    for (int i = 1; i <= n; i++)
      cin >> v[i].first >> v[i].second, f[v[i].first] = 1, f[v[i].second] = 1;
    int m = 0;
    for (auto &it : f) ind[it.first] = ++m;
    for (int i = 1; i <= n; i++)
      v[i].first = ind[v[i].first], v[i].second = ind[v[i].second],
      g[v[i].first].push_back(v[i].second);
    for (int i = 1; i <= m + 1; i++) {
      sort(g[i].begin(), g[i].end());
      for (int j = 1; j <= m + 1; j++) dp[i][j] = 0;
    }
    for (int i = m; i >= 1; i--) {
      for (int j = i; j <= m; j++) {
        dp[i][j] = dp[i + 1][j];
        int cnt = 0;
        for (auto &k : g[i]) {
          if (k > j) break;
          if (k == j)
            cnt = 1;
          else
            dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
        }
        dp[i][j] += cnt;
      }
    }
    for (int i = 1; i <= m; i++) g[i].clear();
    cout << dp[1][m] << "\n";
  }
  return 0;
}
