#include <bits/stdc++.h>
using namespace std;
struct st {
  set<pair<int, int> > s;
  void add(pair<int, int> x) {
    auto it = s.lower_bound({x.first, 1e9});
    if (it != s.begin() && (--it)->second >= x.second) return;
    while ((it = s.lower_bound({x.first, 0})) != s.end()) {
      if (it->second <= x.second)
        s.erase(it);
      else
        break;
    }
    s.insert(x);
  }
  int querry(int x) {
    auto it = s.lower_bound({x, 0});
    if (it == s.begin()) return 0;
    return (--it)->second;
  }
};
pair<int, int> a[3009];
vector<int> g[3009];
int vis[3009];
int cnt[3009];
vector<int> ord;
void top(int v) {
  vis[v] = 1;
  for (auto u : g[v])
    if (vis[u] == 0) top(u);
  ord.push_back(v);
}
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i].first >> a[i].second;
    vis[i] = 0;
    g[i].clear();
  }
  ++n;
  a[n - 1] = {-1e9, 1e9};
  vis[n - 1] = 0;
  g[n - 1].clear();
  sort(a, a + n);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (i != j && a[i].first <= a[j].first && a[j].second <= a[i].second)
        g[i].push_back(j);
  ord.clear();
  for (int i = 0; i < n; ++i)
    if (vis[i] == 0) top(i);
  int ans = 0;
  for (auto v : ord) {
    st s;
    cnt[v] = 0;
    for (auto u : g[v]) {
      int cur = cnt[u] + s.querry(a[u].first);
      cnt[v] = max(cnt[v], cur);
      s.add({a[u].second, cur});
    }
    cnt[v]++;
    ans = max(ans, cnt[v]);
  }
  cout << ans - 1 << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
