#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> dp;
vector<vector<int>> R;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<pair<int, int>> s(n);
    set<int> x;
    dp.clear();
    R.clear();
    for (int i = 0; i < n; ++i) {
      int l, r;
      cin >> l >> r;
      x.insert(l);
      x.insert(r);
      s[i] = {l, r};
    }
    map<int, int> mp;
    int it = 0;
    for (int y : x) {
      mp[y] = it;
      ++it;
    }
    dp.resize(it + 1, vector<int>(it + 1, 0));
    for (int i = 0; i < n; ++i) {
      s[i] = {mp[s[i].first], mp[s[i].second]};
    }
    R.resize(it);
    for (int i = 0; i < n; ++i) R[s[i].first].push_back(s[i].second);
    for (int l = it - 1; l >= 0; --l) {
      for (int r = l; r < it; ++r) {
        dp[l][r] = dp[l + 1][r];
        int add = 0;
        for (int tr : R[l]) {
          if (tr < r) dp[l][r] = max(dp[l][r], dp[l][tr] + dp[tr + 1][r]);
          if (tr == r) add = 1;
        }
        dp[l][r] += add;
      }
    }
    int res = 0;
    for (int i = 0; i < it; ++i) {
      for (int j = i; j < it; ++j) {
        res = max(res, dp[i][j]);
      }
    }
    cout << res << endl;
  }
}
