#include <bits/stdc++.h>
using namespace std;
const int maxn = 3100;
const int inf = 1e9 + 100;
struct node {
  int a, b;
} l[maxn];
int s[maxn * 2 + 10], dp[maxn][maxn * 2 + 10], to[maxn];
int cmp(node a, node b) {
  if (a.b != b.b)
    return a.b < b.b;
  else {
    return a.a > b.a;
  }
}
int main() {
  int num, T, top, n;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    top = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &l[i].a, &l[i].b);
      s[++top] = l[i].a;
      s[++top] = l[i].b;
    }
    sort(s + 1, s + top + 1);
    num = unique(s + 1, s + top + 1) - s - 1;
    sort(l + 1, l + n + 1, cmp);
    for (int i = 1; i <= n; i++) {
      l[i].a = lower_bound(s + 1, s + num + 1, l[i].a) - s;
      l[i].b = lower_bound(s + 1, s + num + 1, l[i].b) - s;
    }
    for (int i = 1; i <= n; i++) {
      to[i] = 0;
      for (int j = i - 1; j >= 1; j--) {
        if (l[j].b < l[i].a) {
          to[i] = j;
          break;
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= num; j++) {
        dp[i][j] = 0;
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = l[i].b; j >= 1; j--) {
        dp[i][j] = dp[i - 1][j];
        if (l[i].a >= j) {
          dp[i][j] = max(dp[i][j], dp[to[i]][j] + dp[i - 1][l[i].a] + 1);
        }
      }
    }
    printf("%d\n", dp[n][1]);
  }
  return 0;
}
