#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int nax = 6e3 + 2;
vector<int> rb[nax];
int dp[nax][nax];
int go(int l, int r) {
  if (dp[l][r] > -1) {
    return dp[l][r];
  } else {
    bool over = false;
    int ans = 0;
    for (auto& v : rb[l]) {
      if (v == r) over = true;
    }
    if (over) {
      ans = 1 + go(l + 1, r);
    } else {
      ans = go(l + 1, r);
    }
    for (auto& v : rb[l]) {
      if (v < r) ans = max(ans, over + go(l, v) + go(v + 1, r));
    }
    return dp[l][r] = ans;
  }
}
void solve() {
  int n;
  cin >> n;
  vector<int> l(n);
  vector<int> r(n);
  vector<int> val;
  for (int i = (0); i < (n); ++i) {
    cin >> l[i] >> r[i];
    val.push_back(l[i]);
    val.push_back(r[i]);
  }
  sort((val).begin(), (val).end());
  val.resize(unique((val).begin(), (val).end()) - val.begin());
  for (int i = 0; i < n; i++) {
    l[i] = lower_bound((val).begin(), (val).end(), l[i]) - val.begin();
    r[i] = lower_bound((val).begin(), (val).end(), r[i]) - val.begin();
  }
  n = ((int)(val).size());
  for (int i = (0); i < (n); ++i) {
    rb[i].clear();
    for (int j = (0); j < (n); ++j) {
      dp[i][j] = -1;
    }
  }
  for (int i = 0; i < ((int)(l).size()); i++) {
    rb[l[i]].push_back(r[i]);
  }
  cout << go(0, n - 1) << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
