#include <bits/stdc++.h>
using namespace std;
const int maxn = 6005;
int val[maxn], dp[maxn], x[maxn], tot;
struct Node {
  int l, r, id;
  bool operator<(const Node a) const { return r - l < (a.r - a.l); }
} a[maxn];
vector<Node> g[maxn];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    tot = 0;
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", &a[i].l, &a[i].r);
      a[i].id = i;
      val[i] = 1;
      x[++tot] = a[i].l;
      x[++tot] = a[i].r;
    }
    sort(x + 1, x + 1 + tot);
    tot = unique(x + 1, x + 1 + tot) - x - 1;
    for (int i = 1; i <= n; ++i) {
      a[i].l = lower_bound(x + 1, x + 1 + tot, a[i].l) - x;
      a[i].r = lower_bound(x + 1, x + 1 + tot, a[i].r) - x;
    }
    ++n;
    a[n] = Node{1, tot + 1, n};
    val[n] = 0;
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n; ++i) {
      for (int j = a[i].l - 1; j <= a[i].r; ++j) {
        dp[j] = 0;
        g[j].clear();
      }
      for (int j = 1; j <= n; ++j) {
        if (j == i) continue;
        if (a[j].r - a[j].l > a[i].r - a[i].l) break;
        if (a[j].l >= a[i].l && a[j].r <= a[i].r) {
          g[a[j].r].push_back(a[j]);
        }
      }
      for (int j = a[i].l; j <= a[i].r; ++j) {
        for (int k = 0; k < g[j].size(); ++k) {
          dp[j] = max(dp[j], dp[g[j][k].l - 1] + val[g[j][k].id]);
        }
        dp[j] = max(dp[j], dp[j - 1]);
      }
      val[a[i].id] = dp[a[i].r] + 1;
    }
    printf("%d\n", val[n] - 1);
  }
}
