#include <bits/stdc++.h>
using namespace std;
int dp[6005][6005];
bool mark[6005][6005];
int n, tc, N;
set<int> S;
vector<int> comp;
vector<int> Q[6005];
int L[3005], R[3005];
int gv(int r) {
  return lower_bound(comp.begin(), comp.end(), r) - comp.begin();
}
int f(int l, int r) {
  if (l > r) return 0;
  int &ans = dp[l][r];
  if (ans != -1) return ans;
  ans = 0;
  ans = max(f(l + 1, r), f(l, r - 1));
  if (mark[l][r]) {
    ans = max(ans, max(f(l + 1, r), f(l, r - 1)) + 1);
    for (int v : Q[r]) {
      if (L[v] <= l) continue;
      ans = max(ans, 1 + f(l, L[v] - 1) + f(L[v], r));
    }
  }
  for (int v : Q[r]) {
    if (L[v] < l) continue;
    ans = max(ans, f(l, L[v] - 1) + f(L[v], r));
  }
  return ans;
}
int main() {
  scanf("%d", &tc);
  while (tc--) {
    S.clear();
    comp.clear();
    S.insert(0);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &L[i], &R[i]);
      S.insert(L[i]);
      S.insert(R[i]);
    }
    for (int v : S) comp.push_back(v);
    N = comp.size();
    for (int i = 0; i < N; i++)
      for (int j = i; j < N; j++) {
        dp[i][j] = -1;
        mark[i][j] = false;
      }
    for (int i = 0; i < N; i++) {
      Q[i].clear();
    }
    for (int i = 0; i < n; i++) {
      Q[gv(R[i])].push_back(i);
      mark[gv(L[i])][gv(R[i])] = true;
      L[i] = gv(L[i]), R[i] = gv(R[i]);
    }
    int ans = f(0, N - 1);
    printf("%d\n", ans);
  }
  return 0;
}
