#include <bits/stdc++.h>
using namespace std;
int t;
int n, m;
int l[3005], r[3005];
vector<int> pos;
vector<vector<int>> dp, kiri;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    pos.clear();
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> l[i] >> r[i];
      pos.push_back(l[i]);
      pos.push_back(r[i]);
    }
    sort(pos.begin(), pos.end());
    pos.resize(unique(pos.begin(), pos.end()) - pos.begin());
    m = pos.size();
    dp.assign(m + 1, vector<int>(m + 1, 0));
    kiri.assign(m + 1, vector<int>());
    for (int i = 0; i < n; i++) {
      l[i] = lower_bound(pos.begin(), pos.end(), l[i]) - pos.begin();
      r[i] = lower_bound(pos.begin(), pos.end(), r[i]) - pos.begin();
      dp[l[i]][r[i]]++;
      kiri[l[i]].push_back(r[i]);
    }
    for (int i = 0; i < m; i++) {
      kiri[i].push_back(i);
      sort(kiri[i].begin(), kiri[i].end());
    }
    for (int len = 1; len <= m; len++) {
      for (int i = 0; i + len - 1 < m; i++) {
        int j = i + len - 1;
        int best = 0;
        for (int mid = 0; mid < kiri[i].size() && kiri[i][mid] < j; mid++) {
          best = max(best, dp[i][kiri[i][mid]] + dp[kiri[i][mid] + 1][j]);
        }
        dp[i][j] += best;
      }
    }
    cout << dp[0][m - 1] << "\n";
  }
  return 0;
}
