#include <bits/stdc++.h>
using namespace std;
constexpr int kN = int(3E3 + 10), lim = int(2E5 + 10);
pair<int, int> p[kN];
vector<int> v[kN];
int val[kN];
bool cmp(pair<int, int> a, pair<int, int> b) {
  return a.second - a.first < b.second - b.first;
}
void solve() {
  int n, nv;
  vector<tuple<int, int, int>> x;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].first, &p[i].second);
  p[++n] = {1, lim};
  for (int i = 1; i <= n; i++) v[i].clear();
  sort(p + 1, p + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++)
      if (p[i].first <= p[j].first && p[j].second <= p[i].second)
        v[i].push_back(j);
  for (int i = 1; i <= n; i++) {
    x.clear();
    for (int j : v[i]) x.push_back({p[j].first, p[j].second, val[j]});
    sort(x.begin(), x.end());
    while (!pq.empty()) pq.pop();
    nv = 0;
    for (tuple<int, int, int> j : x) {
      while (!pq.empty()) {
        if (pq.top().first <= get<0>(j)) {
          nv = max(nv, pq.top().second);
          pq.pop();
        } else
          break;
      }
      pq.push({get<1>(j) + 1, nv + get<2>(j)});
    }
    while (!pq.empty()) {
      nv = max(nv, pq.top().second);
      pq.pop();
    }
    val[i] = nv + 1;
  }
  printf("%d\n", val[n] - 1);
  return;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
}
