#include <bits/stdc++.h>
using namespace std;
vector<vector<short>> g;
vector<vector<short>> dp;
short rec(int l, int r) {
  if (l > r) return 0;
  if (dp[l][r] != -1) return dp[l][r];
  short whole = binary_search(begin(g[l]), end(g[l]), r);
  short ret = whole + rec(l + 1, r);
  for (auto p : g[l]) {
    if (p >= r) break;
    int val = whole + rec(l, p) + rec(p + 1, r);
    if (val > ret) {
      ret = val;
    }
  }
  return dp[l][r] = ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    vector<pair<int, int>> arr(n);
    vector<int> val;
    for (auto& pp : arr) {
      cin >> pp.first >> pp.second;
      val.push_back(pp.first);
      val.push_back(pp.second);
    }
    sort(begin(val), end(val));
    val.erase(unique(begin(val), end(val)), end(val));
    int m = int(val.size());
    g = vector<vector<short>>(m);
    vector<short> cnt(m + 1, 0);
    for (auto& pp : arr) {
      pp.first = lower_bound(begin(val), end(val), pp.first) - begin(val);
      pp.second = lower_bound(begin(val), end(val), pp.second) - begin(val);
      cnt[pp.first]++;
    }
    for (int i = 0; i < m; i++) {
      g[i].resize(cnt[i]);
    }
    fill(begin(cnt), end(cnt), 0);
    for (auto& pp : arr) {
      g[pp.first][cnt[pp.first]++] = pp.second;
    }
    cnt.clear();
    val.clear();
    for (auto& row : g) {
      sort(begin(row), end(row));
      row.erase(unique(begin(row), end(row)), end(row));
    }
    dp = vector<vector<short>>(m, vector<short>(m, -1));
    cout << rec(0, m - 1) << '\n';
  }
  return (0);
}
