#include <bits/stdc++.h>
using namespace std;
int dp[6005][6005];
vector<vector<int>> seg;
int solve(int l, int r) {
  if (l > r) return 0;
  int &ret = dp[l][r];
  if (~ret) return ret;
  int cnt = 0;
  ret = max(solve(l + 1, r), solve(l, r - 1));
  for (auto e : seg[l]) {
    if (e < r) ret = max(ret, solve(l, e) + solve(e + 1, r));
    if (e == r) ++cnt;
  }
  ret += cnt;
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    int n, tmp = 0;
    cin >> n;
    map<int, int> cc;
    vector<pair<int, int>> p(n);
    for (int i = 0; i < n; ++i) {
      cin >> p[i].first >> p[i].second;
      cc[p[i].first];
      cc[p[i].second];
    }
    for (auto &i : cc) i.second = ++tmp;
    seg.assign(tmp + 5, vector<int>());
    for (int i = 0; i < n; ++i) {
      int l = cc[p[i].first], r = cc[p[i].second];
      seg[l].push_back(r);
    }
    for (int i = 0; i <= tmp; ++i) {
      for (int j = 0; j <= tmp; ++j) {
        dp[i][j] = -1;
      }
    }
    cout << solve(1, tmp) << '\n';
  }
}
