#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
int dp[6010][6010];
int go(vector<pii> &v, int l, int r) {
  if (dp[l][r] != -1) return dp[l][r];
  if (l > r) return 0;
  dp[l][r] = go(v, l + 1, r);
  auto s = lower_bound(v.begin(), v.end(), make_pair(l, 0));
  auto e = lower_bound(v.begin(), v.end(), make_pair(l, r));
  for (auto i = s; i < e; i++)
    dp[l][r] = max(dp[l][r], go(v, l, i->second) + go(v, i->second + 1, r));
  auto it = lower_bound(v.begin(), v.end(), make_pair(l, r));
  if (it != v.end() && *it == make_pair(l, r)) dp[l][r]++;
  return dp[l][r];
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  while (N--) {
    int n;
    cin >> n;
    vector<pii> v;
    vector<int> allx;
    int a, b;
    for (int i = 0; i <= n - 1; i++) {
      cin >> a >> b;
      v.emplace_back(a, b);
      allx.emplace_back(a);
      allx.emplace_back(b);
    }
    sort(allx.begin(), allx.end());
    allx.erase(unique(allx.begin(), allx.end()), allx.end());
    for (auto &i : v) {
      i.first = lower_bound(allx.begin(), allx.end(), i.first) - allx.begin();
      i.second = lower_bound(allx.begin(), allx.end(), i.second) - allx.begin();
    }
    sort(v.begin(), v.end());
    for (int i = 0; i <= ((ll)allx.size()) - 1; i++)
      for (int j = 0; j <= ((ll)allx.size()) - 1; j++) dp[i][j] = -1;
    cout << go(v, 0, ((ll)allx.size()) - 1) << "\n";
  }
  return 0;
}
