#include <bits/stdc++.h>
#pragma GCC optimize("O3")
const double PI = acos(-1.0);
using namespace std;
const double EPS = 1e-9;
const int N = 5e5 + 9;
int n, mem[3009][2 * 3009], nxt[3009];
pair<int, int> a[3009];
bool com(pair<int, int> a, pair<int, int> b) {
  if (a.first < b.first)
    return 1;
  else if (a.first == b.first) {
    if (a.second > b.second)
      return 1;
    else
      return 0;
  } else
    return 0;
}
int dp(int l, int mx) {
  if (l > n || a[l].first > mx) return 0;
  int& ret = mem[l][mx];
  if (~ret) return ret;
  ret = dp(l + 1, mx);
  if (a[l].second <= mx)
    ret = max(ret, 1 + dp(l + 1, a[l].second) + dp(nxt[l], mx));
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    map<int, int> mp;
    for (int i = 1; i <= n; i++) {
      cin >> a[i].first >> a[i].second;
      mp[a[i].first];
      mp[a[i].second];
    }
    int id = 0;
    for (auto& p : mp) p.second = ++id;
    for (int i = 1; i <= n; i++) {
      a[i].first = mp[a[i].first];
      a[i].second = mp[a[i].second];
    }
    sort(a + 1, a + 1 + n, com);
    for (int i = 1; i <= n; i++) {
      nxt[i] = n + 1;
      for (int j = i + 1; j <= n; j++) {
        if (a[j].first > a[i].second) {
          nxt[i] = j;
          break;
        }
      }
    }
    for (int i = 0; i <= n + 2; i++)
      for (int j = 0; j <= n * 2 + 9; j++) mem[i][j] = -1;
    cout << dp(1, n * 2 + 2) << endl;
  }
  return 0;
}
