#include <bits/stdc++.h>
using namespace std;
const int Mod = (int)1e9 + 7;
int CaseN = 1;
const long double PI = 22 / 7.0;
int tst() {
  int ans = 0;
  return ans;
}
int f(int l, int r, vector<vector<int>> &dp, vector<vector<int>> &en) {
  if (dp[l][r] != -1) {
    return dp[l][r];
  }
  dp[l][r] = 0;
  if (l > r) {
    return dp[l][r];
  }
  int cnt = 0;
  if (count(en[l].begin(), en[l].end(), r)) {
    cnt++;
  }
  dp[l][r] = max(dp[l][r], cnt + (l + 1 <= r ? f(l + 1, r, dp, en) : 0));
  for (auto ng : en[l]) {
    if (ng >= r) {
      continue;
    }
    dp[l][r] = max(dp[l][r], cnt + f(l, ng, dp, en) +
                                 ((ng + 1 <= r) ? f(ng + 1, r, dp, en) : 0));
  }
  return dp[l][r];
}
void solve() {
  int n;
  cin >> n;
  int l[n + 1], r[n + 1];
  vector<int> v;
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i];
    v.push_back(l[i]);
    v.push_back(r[i]);
  }
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  for (int i = 1; i <= n; i++) {
    l[i] = lower_bound(v.begin(), v.end(), l[i]) - v.begin();
    r[i] = lower_bound(v.begin(), v.end(), r[i]) - v.begin();
  }
  int sz = v.size();
  vector<vector<int>> dp(sz + 1, vector<int>(sz + 1, -1));
  vector<vector<int>> en(sz + 1);
  for (int i = 1; i <= n; i++) {
    en[l[i]].push_back(r[i]);
  }
  int ans = f(0, sz - 1, dp, en);
  cout << ans << endl;
  CaseN++;
  return;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  while (t-- > 0) {
    solve();
  }
}
