#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > dp(6005, vector<int>(6005));
vector<vector<int> > beg(6005);
int solve(int x, int y) {
  if (dp[x][y] != -1) return dp[x][y];
  int p1 = x;
  int cur = 0, ad = 0;
  for (int e : beg[x]) {
    if (e < y)
      cur = max(cur, solve(p1, e) + solve(e + 1, y));
    else if (e == y)
      ad = 1;
  }
  if (x + 1 <= y) cur = max(cur, solve(x + 1, y));
  cur += ad;
  dp[x][y] = cur;
  return cur;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    map<int, int> mp;
    vector<pair<int, int> > vp(n);
    for (int i = 0; i < n; i++) {
      cin >> vp[i].first >> vp[i].second;
      mp[vp[i].first]++, mp[vp[i].second]++;
    }
    int m = 0;
    for (auto it : mp) {
      mp[it.first] = m++;
    }
    for (int i = 0; i < m; i++) {
      beg[i].clear();
      for (int j = 0; j < m; j++) dp[i][j] = -1;
    }
    int ans = 1;
    for (pair<int, int> pr : vp) {
      beg[mp[pr.first]].push_back(mp[pr.second]);
    }
    ans = max(ans, solve(0, m - 1));
    cout << ans << "\n";
  }
  return 0;
}
