#include <bits/stdc++.h>
using namespace std;
long long MOD = 998244353;
const long long INF = 1e18;
const double E = 1e-18;
const double PI = acos(-1);
mt19937_64 rnd(time(0));
long long p1 = 31, p2 = 37, p3 = 7, p4 = rnd() % 100 + 1, md1 = MOD,
          md2 = 998244353, md3 = 998244357, md4 = rnd() % 1000000000;
long long n, m, k, T, P, dp[10001], a[200001], dpp[10001];
vector<pair<long long, long long> > pp[10001];
long long binpow(long long h, long long r, long long md = MOD) {
  long long l = 1;
  while (r) {
    if (r & 1) l *= h, l %= md;
    h *= h;
    h %= md;
    r /= 2;
  }
  return l;
}
long long gcd(long long x, long long y) {
  if (x < y) swap(x, y);
  while (x && y) {
    x %= y;
    swap(x, y);
  }
  return x + y;
}
void solve() {
  cin >> n;
  vector<pair<int, pair<int, int> > > v;
  vector<pair<int, int> > vv;
  vector<int> uu;
  for (int i = 1; i <= n; i++) {
    long long x, y;
    cin >> x >> y;
    vv.push_back(make_pair(x, y));
    uu.push_back(x);
    uu.push_back(y);
  }
  sort(uu.begin(), uu.end());
  long long k = 0;
  for (int i = 0; i < uu.size(); i++) {
    if (i == 0 || uu[i] != uu[i - 1]) k++;
    a[uu[i]] = k;
  }
  for (int i = 0; i < n; i++) {
    long long x = a[vv[i].first], y = a[vv[i].second];
    v.push_back(make_pair(y - x, make_pair(x, y)));
  }
  v.push_back(make_pair(2 * n + 1, make_pair(0, 2 * n + 1)));
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    for (int j = 0; j < i; j++)
      if (v[i].second.first <= v[j].second.first &&
          v[i].second.second >= v[j].second.second) {
        pp[v[j].second.first].push_back(make_pair(v[j].second.second, dp[j]));
      }
    long long ans = 0;
    for (int u = v[i].second.first; u <= v[i].second.second; u++) {
      ans = max(ans, dpp[u]);
      dpp[u + 1] = max(dpp[u + 1], dpp[u]);
      for (int j = 0; j < pp[u].size(); j++)
        dpp[pp[u][j].first + 1] =
            max(dpp[u] + pp[u][j].second, dpp[pp[u][j].first + 1]);
      dpp[u] = 0;
      pp[u].clear();
    }
    dp[i] = max(ans, dpp[v[i].second.second + 1]) + (i + 1 != v.size());
    dpp[v[i].second.second + 1] = 0;
  }
  cout << dp[v.size() - 1] << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
