#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
const int chtholly = 0;
int T;
void dfs(int mini, int maxi, vector<int> dp[], vector<int> gr[]) {
  int add = 0;
  dp[mini][maxi] = 0;
  if (maxi != mini) {
    if (dp[mini][maxi - 1] == -1) dfs(mini, maxi - 1, dp, gr);
    dp[mini][maxi] = dp[mini][maxi - 1];
  }
  for (auto it : gr[maxi]) {
    if (it == mini) add = 1;
    if (it <= mini) continue;
    int can = 0;
    if (dp[mini][it - 1] == -1) dfs(mini, it - 1, dp, gr);
    can += dp[mini][it - 1];
    if (dp[it][maxi] == -1) dfs(it, maxi, dp, gr);
    can += dp[it][maxi];
    dp[mini][maxi] = max(dp[mini][maxi], can);
  }
  dp[mini][maxi] += add;
}
int main() {
  cin >> T;
  while (T--) {
    int n, i, j, k;
    cin >> n;
    vector<pii> seg;
    vector<int> pl;
    map<int, int> mp;
    for (i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      seg.push_back({a, b});
      pl.push_back(a);
      pl.push_back(b);
    }
    sort(pl.begin(), pl.end());
    int counti = 0;
    for (i = 0; i < pl.size(); i++) {
      if (!mp.count(pl[i])) mp.insert({pl[i], ++counti});
    }
    int real_siz = counti;
    vector<int> dp[real_siz + 1];
    vector<int> gr[real_siz + 1];
    for (i = 0; i <= real_siz; i++)
      for (j = 0; j <= real_siz; j++) dp[i].push_back(-1);
    for (i = 0; i < n; i++) {
      int a = mp[seg[i].first];
      int b = mp[seg[i].second];
      gr[b].push_back(a);
    }
    dfs(0, real_siz, dp, gr);
    int maxi = 1;
    cout << dp[0][real_siz] << "\n";
  }
  return chtholly;
}
