#include <bits/stdc++.h>
using namespace std;
const long long N = 3333;
long long q, dp[N], n;
bool used[N];
vector<pair<long long, long long>> vec;
long long calc(vector<long long>& v) {
  long long sz = v.size();
  vector<long long> DP(sz + 1, 0);
  for (long long i = sz - 1; i >= 0; i--) {
    long long l = i, r = sz - 1, med;
    while (r - l > 1) {
      med = (r + l) / 2;
      if (vec[v[med]].first <= vec[v[i]].second)
        l = med;
      else
        r = med;
    }
    long long j = sz;
    if (vec[v[r]].first > vec[v[i]].second) j = r;
    if (vec[v[l]].first > vec[v[i]].second) j = l;
    DP[i] = dp[v[i]] + DP[j];
    DP[i] = max(DP[i], DP[i + 1]);
  }
  return DP[0];
}
long long rec(long long i) {
  if (used[i]) return dp[i];
  used[i] = 1;
  vector<long long> veci;
  for (long long j = 0; j <= n; ++j) {
    if (i != j && vec[i].first <= vec[j].first &&
        vec[j].second <= vec[i].second) {
      veci.push_back(j);
      rec(j);
    }
  }
  dp[i] = calc(veci) + 1;
  return dp[i];
}
signed main() {
  ios_base::sync_with_stdio(0);
  cout.precision(40);
  cin >> q;
  while (q--) {
    cin >> n;
    vec.resize(n + 1);
    for (long long i = 0; i < n; ++i) cin >> vec[i].first >> vec[i].second;
    vec[n].first = 0;
    vec[n].second = 2e5 + 1;
    sort(vec.begin(), vec.end());
    long long res = 0;
    for (long long i = 0; i <= n; ++i) res = max(res, rec(i));
    cout << res - 1 << '\n';
    fill(used, used + n + 1, 0);
    vec.clear();
  }
}
