#include <bits/stdc++.h>
using namespace std;
int T;
int N;
int L[3030], R[3030];
int dp[6060];
vector<pair<int, int>> ev[6060];
int num[6060];
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> T;
  while (T--) {
    cin >> N;
    vector<int> Xs;
    for (i = 0; i < (N); i++) {
      cin >> L[i] >> R[i];
      L[i] = L[i] * 2;
      R[i] = R[i] * 2 + 1;
      Xs.push_back(L[i]);
      Xs.push_back(R[i]);
    }
    Xs.push_back(0);
    L[N] = 0;
    R[N] = 502020;
    Xs.push_back(502020);
    N++;
    sort((Xs.begin()), (Xs.end()));
    Xs.erase(unique((Xs.begin()), (Xs.end())), Xs.end());
    for (i = 0; i < (Xs.size() + 10); i++) ev[i].clear();
    vector<pair<int, int>> cand;
    for (i = 0; i < (N); i++) {
      L[i] = lower_bound((Xs.begin()), (Xs.end()), L[i]) - Xs.begin();
      R[i] = lower_bound((Xs.begin()), (Xs.end()), R[i]) - Xs.begin();
      cand.push_back({R[i] - L[i], i});
    }
    sort((cand.begin()), (cand.end()));
    for (auto& c : cand) {
      x = c.second;
      memset(dp, 0, sizeof(dp));
      int cur = 0;
      for (i = L[x]; i <= R[x]; i++) {
        cur = max(cur, dp[i]);
        for (auto& e : ev[i]) {
          dp[e.first] = max(dp[e.first], cur + e.second);
        }
      }
      num[x] = cur + 1;
      ev[L[x]].push_back({R[x], num[x]});
    }
    cout << num[N - 1] - 1 << endl;
  }
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
