#include <bits/stdc++.h>
using namespace std;
const int N = 3003;
int nxt[N], n, dp[N][N];
vector<int> adj[N];
int f(int i, int j) {
  if (j >= adj[i].size()) {
    return 0;
  }
  if (~dp[i][j]) {
    return dp[i][j];
  }
  int res = f(i, j + 1);
  int l = lower_bound(adj[i].begin(), adj[i].end(), nxt[adj[i][j]]) -
          adj[i].begin(),
      kk = 0;
  if (l < adj[i].size()) {
    kk = f(i, l);
  }
  res = max(res, 1 + f(adj[i][j], 0) + kk);
  return dp[i][j] = res;
}
void solve() {
  cin >> n;
  vector<pair<int, int> > seg;
  adj[0].clear();
  for (int i = 1; i <= n; i++) {
    int l, r;
    cin >> l >> r;
    seg.push_back(make_pair(l, r));
    adj[i].clear();
    adj[0].push_back(i);
  }
  sort(seg.begin(), seg.end());
  for (int i = 0; i < seg.size(); i++) {
    nxt[i + 1] = n + 1;
    for (int j = i + 1; j < seg.size(); j++) {
      if (seg[i].second < seg[j].first) {
        nxt[i + 1] = j + 1;
        break;
      }
    }
    for (int j = 0; j < seg.size(); j++)
      if ((i ^ j) && seg[i].first <= seg[j].first &&
          seg[i].second >= seg[j].second) {
        adj[i + 1].push_back(j + 1);
      }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = -1;
    }
  }
  cout << f(0, 0) << endl;
}
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int test, tc = 0;
  cin >> test;
  while (test--) {
    solve();
  }
}
