#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}
const int N = 6e3 + 5;
const int oo = 1e9 + 7;
pair<int, int> lr[N];
int dp[N][N];
bool cmp(pair<int, int> x, pair<int, int> y) {
  if (x.first != y.first)
    return x.first <= y.first;
  else
    return x.second >= y.second;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T, cs = 0;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<int> x;
    for (int i = 0; i < n; i++) {
      cin >> lr[i].first;
      cin >> lr[i].second;
      x.push_back(lr[i].first);
      x.push_back(lr[i].second);
    }
    sort(x.begin(), x.end());
    x.erase(unique(x.begin(), x.end()), x.end());
    for (int i = 0; i < n; i++) {
      lr[i].first =
          lower_bound(x.begin(), x.end(), lr[i].first) - x.begin() + 1;
      lr[i].second =
          lower_bound(x.begin(), x.end(), lr[i].second) - x.begin() + 1;
    }
    int mxr = x.size();
    sort(lr, lr + n, cmp);
    for (int r = 1; r <= mxr; r++) {
      int i = n - 1;
      for (int l = r; l >= 1; l--) {
        int jog = 0;
        while (i >= 0 and lr[i].first >= l) {
          if (lr[i].second >= r)
            jog += lr[i].second == r and lr[i].first == l;
          else if (lr[i].first == l) {
            dp[l][r] = max(dp[l][r], dp[lr[i].first][lr[i].second] +
                                         dp[lr[i].second + 1][r]);
          }
          i--;
        }
        dp[l][r] = max(dp[l][r], dp[l + 1][r]);
        dp[l][r] += jog;
      }
    }
    cout << dp[1][mxr] << "\n";
    for (int i = 1; i <= mxr; i++)
      for (int j = 1; j <= mxr; j++) dp[i][j] = 0;
  }
}
