#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
vector<vector<int>> dp, pos;
int fun(int l, int r) {
  if (dp[l][r] != -1) return dp[l][r];
  dp[l][r] = 0;
  int cur = 0;
  for (auto it : pos[l])
    if (it == r) cur = 1;
  dp[l][r] = max(dp[l][r], cur + (l + 1 > r ? 0 : fun(l + 1, r)));
  for (auto it : pos[l]) {
    if (it >= r) continue;
    dp[l][r] =
        max(dp[l][r], cur + fun(l, it) + (it + 1 > r ? 0 : fun(it + 1, r)));
  }
  return dp[l][r];
}
void solve() {
  int n;
  cin >> n;
  int left[n], right[n];
  set<int> st;
  for (int i = 0; i < n; i++) {
    cin >> left[i] >> right[i];
    st.insert(left[i]);
    st.insert(right[i]);
  }
  vector<int> v;
  for (auto it : st) v.push_back(it);
  dp = vector<vector<int>>((int)v.size(), vector<int>((int)v.size(), -1));
  pos = vector<vector<int>>((int)v.size());
  for (int i = 0; i < n; i++) {
    left[i] = lower_bound(v.begin(), v.end(), left[i]) - v.begin();
    right[i] = lower_bound(v.begin(), v.end(), right[i]) - v.begin();
  }
  for (int i = 0; i < n; i++) {
    pos[left[i]].push_back(right[i]);
  }
  cout << fun(0, (int)v.size() - 1) << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t;
  cin >> t;
  for (int test = 1; test <= t; test++) {
    solve();
  }
  return 0;
}
