#include <bits/stdc++.h>
using namespace std;
int n, dp[10005], x[10005];
pair<int, int> p[10005];
vector<int> tmp;
map<pair<int, int>, int> mp;
bool inside(pair<int, int> &x, int l, int r) {
  if (x.first == l && x.second == r) return false;
  return l <= x.first && x.second <= r;
}
int dfs(int l, int r) {
  vector<int> son;
  for (int i = 1; i <= n; i++)
    if (inside(p[i], l, r)) {
      if (x[i] == -1) x[i] = dfs(p[i].first, p[i].second);
      son.push_back(i);
    }
  for (int i = l - 1; i <= r; i++) dp[i] = 0;
  int cur = l;
  for (auto it : son) {
    while (cur + 1 < p[it].first && cur < r)
      cur++, dp[cur] = max(dp[cur], dp[cur - 1]);
    dp[p[it].second] = max(dp[p[it].second], dp[p[it].first - 1] + x[it]);
  }
  while (cur < r) cur++, dp[cur] = max(dp[cur], dp[cur - 1]);
  return dp[r] + mp[make_pair(l, r)];
}
void solve() {
  scanf("%d", &n);
  tmp.clear();
  mp.clear();
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &p[i].first, &p[i].second);
    tmp.push_back(p[i].first);
    tmp.push_back(p[i].second);
    x[i] = -1;
  }
  sort(tmp.begin(), tmp.end());
  for (int i = 1; i <= n; i++) {
    p[i].first =
        lower_bound(tmp.begin(), tmp.end(), p[i].first) - tmp.begin() + 1;
    p[i].second =
        lower_bound(tmp.begin(), tmp.end(), p[i].second) - tmp.begin() + 1;
    mp[p[i]]++;
  }
  sort(p + 1, p + n + 1);
  n = unique(p + 1, p + n + 1) - p - 1;
  printf("%d\n", dfs(1, tmp.size() + 1));
}
int main() {
  int _;
  scanf("%d", &_);
  while (_--) solve();
  return 0;
}
