#include <bits/stdc++.h>
using namespace std;
int dp[6002][6002];
int num[6002];
int cnt;
vector<int> clean;
vector<int> edge[200005];
map<int, int> mp;
int dfs(int l, int r) {
  if (dp[l][r] != -1) {
    return dp[l][r];
  } else {
    dp[l][r] = 0;
    for (int tp : edge[num[l]]) {
      int tr = mp[tp];
      if (tr == r) dp[l][r]++;
    }
    int tans = 0;
    for (int tp : edge[num[l]]) {
      int tr = mp[tp];
      if (tr > l && tr < r) {
        tans = max(tans, dfs(l, tr) + dfs(tr + 1, r));
      }
    }
    if (l != r) {
      tans = max(tans, dfs(l + 1, r));
    }
    dp[l][r] += tans;
    return dp[l][r];
  }
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    int pans = 0;
    scanf("%d", &n);
    for (int tp : clean) {
      edge[tp].clear();
    }
    clean.clear();
    for (int i = 0; i < 2 * n + 2; i++) {
      for (int j = 0; j < 2 * n + 2; j++) {
        dp[i][j] = -1;
      }
    }
    cnt = 0;
    for (int i = 0; i < n; i++) {
      int l, r;
      scanf("%d%d", &l, &r);
      if (l == r) {
        pans++;
        continue;
      }
      num[cnt++] = l;
      num[cnt++] = r;
      edge[l].push_back(r);
      clean.push_back(l);
    }
    sort(num, num + cnt);
    cnt = unique(num, num + cnt) - num;
    for (int i = 0; i < cnt; i++) {
      mp[num[i]] = i;
    }
    printf("%d\n", dfs(0, cnt - 1) + pans);
  }
  return 0;
}
