#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T>
ostream &operator<<(ostream &os, vector<T> &v);
template <class T>
ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2>
ostream &operator<<(ostream &os, map<T1, T2> &v);
const long long mod = 1e9 + 7;
const int N = 2e5 + 5;
vector<vector<int> > dp;
vector<vector<int> > rp;
int solve(int l, int r) {
  if (l > r) return 0;
  int &ret = dp[l][r];
  if (ret != -1) return ret;
  int ans = 0;
  int ache = 0;
  if (find(rp[l].begin(), rp[l].end(), r) != rp[l].end()) {
    ache = 1;
  }
  ans = max(ans, solve(l + 1, r));
  ans = max(ans, solve(l, r - 1));
  for (int i : rp[l]) {
    if (i >= r) continue;
    ans = max(ans, solve(l, i) + solve(i + 1, r));
  }
  return ret = ache + ans;
}
void TEST_CASES(int cas) {
  int n;
  scanf("%d", &n);
  dp.clear();
  rp.clear();
  dp.resize(2 * n, vector<int>(2 * n, -1));
  rp.resize(2 * n);
  vector<pair<int, int> > a(n);
  vector<int> vals;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a[i].first, &a[i].second);
    vals.push_back(a[i].first);
    vals.push_back(a[i].second);
  }
  sort(vals.begin(), vals.end());
  vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
  for (pair<int, int> &i : a) {
    i.first = lower_bound(vals.begin(), vals.end(), i.first) - vals.begin();
    i.second = lower_bound(vals.begin(), vals.end(), i.second) - vals.begin();
  }
  for (pair<int, int> &i : a) {
    rp[i.first].push_back(i.second);
  }
  printf("%d\n", solve(0, vals.size() - 1));
}
int main() {
  int t = 1, cas = 0;
  scanf("%d", &t);
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}
