#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v;
    multiset<pair<int, int>> s, s2;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      v.push_back(a);
      v.push_back(b);
      s.insert({a, b});
    }
    sort(v.begin(), v.end());
    v.resize(unique(v.begin(), v.end()) - v.begin());
    int m = v.size();
    vector<vector<int>> dp(m, vector<int>(m, 0));
    vector<int> p[m];
    for (auto u : s) {
      int a = u.first, b = u.second;
      a = lower_bound(v.begin(), v.end(), a) - v.begin();
      b = lower_bound(v.begin(), v.end(), b) - v.begin();
      if (a == b) dp[a][a]++;
      s2.insert({a, b});
      p[a].push_back(b);
    }
    for (int len = 1; len <= m - 1; len++) {
      for (int i = 0; i < m; i++) {
        if (i + len >= m) continue;
        int j = i + len;
        dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]);
        for (int u : p[i]) {
          if (u > j - 1) break;
          dp[i][j] = max(dp[i][j], dp[i][u] + dp[u + 1][j]);
        }
        dp[i][j] += s2.count({i, j});
      }
    }
    cout << dp[0][m - 1] << endl;
  }
}
