#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    vector<int> b;
    for (int i = 0; i < n; i++) {
      cin >> a[i].first >> a[i].second;
      b.emplace_back(a[i].first);
      b.emplace_back(a[i].second);
    }
    sort(b.begin(), b.end());
    b.resize(unique(b.begin(), b.end()) - b.begin());
    vector<vector<int>> line((int)b.size());
    for (int i = 0; i < n; i++) {
      a[i].first = lower_bound(b.begin(), b.end(), a[i].first) - b.begin();
      a[i].second = lower_bound(b.begin(), b.end(), a[i].second) - b.begin();
      line[a[i].first].emplace_back(a[i].second);
    }
    vector<vector<int>> dp((int)b.size(), vector<int>((int)b.size()));
    for (int i = 0; i < (int)b.size(); i++) {
      sort(line[i].begin(), line[i].end());
    }
    for (int i = (int)b.size() - 1; i >= 0; i--) {
      for (int j = i; j < (int)b.size(); j++) {
        bool all = false;
        if (i != j) dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
        for (auto& x : line[i]) {
          if (x > j) break;
          if (x == j) {
            all = true;
          } else
            dp[i][j] = max(dp[i][j], dp[i][x] + dp[x + 1][j]);
        }
        if (all) dp[i][j]++;
      }
    }
    cout << dp[0][(int)b.size() - 1] << "\n";
  }
}
