#include <bits/stdc++.h>
using namespace std;
const int N = 3006;
vector<pair<int, int>> a(N);
map<int, int> m;
vector<int> v[2 * N];
vector<vector<int>> dp;
void test_case() {
  int n, i, j;
  cin >> n;
  m.clear();
  for (i = 0; i < 2 * n + 6; i++) {
    v[i].clear();
  }
  for (i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    m[a[i].first] = 0;
    m[a[i].second] = 0;
  }
  a[i].first = 0;
  a[i].second = 200005;
  m[a[i].first] = 0;
  m[a[i].second] = 0;
  n++;
  int cnt = 0;
  for (auto &k : m) {
    m[k.first] = cnt++;
  }
  for (i = 0; i < n; i++) {
    int l = m[a[i].first];
    int r = m[a[i].second];
    v[l].push_back(r);
  }
  for (i = 0; i < cnt; i++) {
    sort(v[i].begin(), v[i].end());
  }
  dp.clear();
  dp.resize(cnt, vector<int>(cnt, 0));
  for (i = cnt - 1; i >= 0; i--) {
    for (j = 0; j < cnt; j++) {
      if (i > j) continue;
      auto it = lower_bound(v[i].begin(), v[i].end(), j);
      if (it != v[i].end() && *it == j) dp[i][j] = 1;
      int tmp = 0;
      if (i + 1 <= cnt && i + 1 <= j) tmp = dp[i + 1][j];
      int ind = (int)(it - v[i].begin());
      int u;
      for (u = 0; u < ind; u++) {
        int k = v[i][u];
        tmp = max(tmp, dp[i][k] + dp[k + 1][j]);
      }
      dp[i][j] += tmp;
    }
  }
  int l = m[0];
  int r = m[200005];
  cout << dp[l][r] - 1 << "\n";
  return;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int t;
  t = 1;
  cin >> t;
  while (t--) {
    test_case();
  }
  return 0;
}
