#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 1000000007;
const ll root = 62;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll modInv(ll a) { return binpow(a, MOD - 2); }
const double PI = acos(-1);
const double eps = -1e6;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 3001;
int T;
int n;
pair<int, int> arr[mxN];
int dp[2 * mxN][2 * mxN];
bool here[2 * mxN][2 * mxN];
vector<int> st[2 * mxN];
set<int> stt;
int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin.tie(0)->sync_with_stdio(0);
  cin >> T;
  while (T--) {
    cin >> n;
    stt.clear();
    for (int i = (0); i < (n); ++i) {
      cin >> arr[i].first >> arr[i].second;
      stt.insert(arr[i].first);
      stt.insert(arr[i].second);
    }
    for (int i = (0); i < (2 * n); ++i) {
      for (int j = (0); j < (2 * n); ++j) {
        dp[i][j] = 0;
        here[i][j] = false;
      }
      st[i].clear();
    }
    vector<int> coord(stt.begin(), stt.end());
    int mx = stt.size();
    for (int i = (0); i < (n); ++i) {
      arr[i].first =
          lower_bound(coord.begin(), coord.end(), arr[i].first) - coord.begin();
      arr[i].second = lower_bound(coord.begin(), coord.end(), arr[i].second) -
                      coord.begin();
      st[arr[i].first].push_back(arr[i].second);
      here[arr[i].first][arr[i].second] = true;
    }
    for (int i = (0); i < (mx); ++i) {
      sort(st[i].begin(), st[i].end());
    }
    for (int len = (0); len < (mx); ++len) {
      for (int s = (0); s < (mx - len); ++s) {
        int e = s + len;
        for (int p : st[s]) {
          if (p >= e) break;
          int k1 = dp[s][p];
          if (here[s][p]) k1--;
          int k2 = 0;
          if (e - p >= 1) {
            k2 = dp[p + 1][e];
          }
          dp[s][e] = max(dp[s][e], k1 + k2 + 1);
        }
        dp[s][e] = max(dp[s][e], dp[s + 1][e]);
        if (here[s][e]) dp[s][e]++;
      }
    }
    cout << dp[0][mx - 1] << "\n";
  }
  return 0;
}
