#include <bits/stdc++.h>
using namespace std;
const int N = 5000 + 5;
int T, n;
pair<int, int> a[N];
int b[N], cnt;
int g[N];
inline bool cmplen(const pair<int, int>& a, const pair<int, int>& b) {
  return a.second - a.first < b.second - b.first;
}
inline bool cmps(const int& x, const int& y) { return a[x].first < a[y].first; }
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    q;
int main() {
  int i, j;
  scanf("%d", &T);
  while (T--) {
    memset(g, 0, sizeof g);
    scanf("%d", &n);
    for (i = 1; i <= n; i++) scanf("%d%d", &a[i].first, &a[i].second);
    a[++n] = pair<int, int>(0, 2e5 + 1);
    sort(a + 1, a + n + 1, cmplen);
    for (i = 1; i <= n; i++) {
      cnt = 0;
      for (j = 1; j < i; j++) {
        if (a[i].first <= a[j].first && a[j].second <= a[i].second)
          b[++cnt] = j;
      }
      sort(b + 1, b + cnt + 1, cmps);
      int cmax = 0;
      for (j = 1; j <= cnt; j++) {
        for (; q.size() && q.top().first < a[b[j]].first; q.pop())
          cmax = max(cmax, q.top().second);
        q.push(pair<int, int>(a[b[j]].second, cmax + g[b[j]]));
      }
      g[i] = 1;
      for (; q.size(); q.pop()) g[i] = max(g[i], q.top().second + 1);
    }
    printf("%d\n", g[n] - 1);
  }
  return 0;
}
