#include <bits/stdc++.h>
using namespace std;
void debug() { cout << endl; }
template <typename T, typename... Args>
void debug(T x, Args... args) {
  cout << x << " ";
  debug(args...);
}
vector<vector<int>> dp;
vector<pair<int, int>> v;
int n;
int f(int i, int j) {
  if (i == n) return 0;
  int &r = dp[i][j];
  if (r != -1) return r;
  r = f(i + 1, j);
  if (v[i].second <= j) {
    int ni =
        lower_bound(v.begin(), v.end(), pair<int, int>(v[i].second + 1, 0)) -
        v.begin();
    r = max(r, 1 + f(i + 1, v[i].second) + f(ni, j));
  }
  return r;
}
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first == b.first) return a.second > b.second;
  return a.first < b.first;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    v.resize(n);
    int sz = 2 * n;
    int ind[sz];
    for (int i = 0; i < n; ++i) {
      int a, b;
      cin >> a >> b;
      v[i] = {a, b};
      ind[i] = a;
      ind[i + n] = b;
    }
    sort(v.begin(), v.end(), cmp);
    sort(ind, ind + sz);
    sz = unique(ind, ind + sz) - ind;
    for (int i = 0; i < n; ++i) {
      v[i].first = lower_bound(ind, ind + sz, v[i].first) - ind;
      v[i].second = lower_bound(ind, ind + sz, v[i].second) - ind;
    }
    dp.assign(n, vector<int>(sz, -1));
    cout << f(0, sz - 1) << "\n";
  }
}
