#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;
long long fac[1] = {1}, inv[1] = {1};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long mp(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return ret;
}
long long cmb(long long r, long long c) {
  return fac[r] * inv[c] % MOD * inv[r - c] % MOD;
}
vector<int> x;
pair<int, int> vec[3005];
int dp[3005][6005];
int nn;
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.first != b.first) return a.first < b.first;
  return a.second > b.second;
}
int go(int o, int r) {
  if (o >= nn) return 0;
  if (dp[o][r] != -1) return dp[o][r];
  int ret = 0;
  int ri = vec[o].second;
  int ne = lower_bound(vec, vec + nn, pair<int, int>(ri + 1, r), cmp) - vec;
  ret = max(ret, go(o + 1, r));
  if (ri <= r) ret = max(ret, go(o + 1, ri) + 1 + go(ne, r));
  return dp[o][r] = ret;
}
int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    scanf("%d", &nn);
    x.clear();
    x.push_back(200001);
    for (int i = 0; i < nn; i++) {
      int a, b;
      scanf("%d %d", &a, &b);
      vec[i] = {a, b};
      x.push_back(a);
      x.push_back(b);
    }
    sort((x).begin(), (x).end());
    x.erase(unique((x).begin(), (x).end()), x.end());
    for (int i = 0; i < nn; i++)
      for (int j = 0; j < (int)(x).size(); j++) dp[i][j] = -1;
    for (int i = 0; i < nn; i++) {
      int a = lower_bound((x).begin(), (x).end(), vec[i].first) - x.begin();
      int b = lower_bound((x).begin(), (x).end(), vec[i].second) - x.begin();
      vec[i] = {a, b};
    }
    sort(vec, vec + nn, cmp);
    printf("%d\n", go(0, (int)(x).size() - 1));
  }
}
