#include <bits/stdc++.h>
using namespace std;
int dp[6002][6002];
vector<int> R[6002];
int l[3002], r[3002];
int dfs(int s, int e) {
  if (s > e) return 0;
  if (dp[s][e] != -1) return dp[s][e];
  int add = 0;
  for (int i = 0; i < R[e].size(); i++) {
    if (R[e][i] == s) add = 1;
  }
  dp[s][e] = max(dfs(s + 1, e) + add, dfs(s, e - 1) + add);
  for (int i = 0; i < R[e].size(); i++) {
    int l = R[e][i];
    if (l > s) {
      dp[s][e] = max(dp[s][e], dfs(s, l - 1) + dfs(l, e) + add);
    }
  }
  return dp[s][e];
}
int main() {
  memset(dp, -1, sizeof(dp));
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    vector<int> sum;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &l[i], &r[i]);
      sum.emplace_back(l[i]);
      sum.emplace_back(r[i]);
    }
    sort(sum.begin(), sum.end());
    sum.erase(unique(sum.begin(), sum.end()), sum.end());
    for (int i = 1; i <= n; i++) {
      l[i] = lower_bound(sum.begin(), sum.end(), l[i]) - sum.begin();
      r[i] = lower_bound(sum.begin(), sum.end(), r[i]) - sum.begin();
      R[r[i]].emplace_back(l[i]);
    }
    printf("%d\n", dfs(0, sum.size() - 1));
    for (int i = 0; i < sum.size(); i++) {
      R[i].clear();
      for (int j = i; j < sum.size(); j++) dp[i][j] = -1;
    }
  }
}
