#include <bits/stdc++.h>
using namespace std;
const int N = 6005;
int dp[N][N];
int tmp[N], ml[N], mr[N];
int f(int x, int n) {
  int ind = lower_bound(tmp, tmp + n, x) - tmp;
  assert(tmp[ind] == x);
  return ind;
}
vector<int> rlist[N];
int solveDp(int l, int r) {
  if (l > r) {
    return 0;
  }
  int &ret = dp[l][r];
  if (ret != -1) {
    return ret;
  }
  ret = 0;
  if (l + 1 <= r) {
    ret = solveDp(l + 1, r);
  }
  bool isBoard = false;
  for (int cand_r : rlist[l]) {
    if (cand_r > r) {
      continue;
    }
    if (cand_r == r) {
      isBoard = true;
    } else {
      ret = max(ret, solveDp(l, cand_r) + solveDp(cand_r + 1, r));
    }
  }
  if (isBoard) {
    ++ret;
  }
  return ret;
}
void solve2() {
  int n, pos = 0;
  scanf("%d ", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d %d ", &ml[i], &mr[i]);
    tmp[pos++] = ml[i];
    tmp[pos++] = mr[i];
  }
  sort(tmp, tmp + pos);
  for (int i = 0; i < pos; ++i) {
    for (int j = 0; j < pos; ++j) {
      dp[i][j] = -1;
    }
    rlist[i].clear();
  }
  for (int i = 0; i < n; ++i) {
    ml[i] = f(ml[i], pos);
    mr[i] = f(mr[i], pos);
    rlist[ml[i]].push_back(mr[i]);
  }
  printf("%d\n", solveDp(0, pos - 1));
}
void solve() {
  int t;
  scanf("%d", &t);
  while (t--) {
    solve2();
  }
}
int main() {
  solve();
  return 0;
}
