#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e6 + 5, M = 1e9 + 7;
pair<int, int> a[N];
int dp[N];
int d[N];
int res[N];
int len;
int f[N];
void modify(int x, int val) {
  while (x <= len) {
    f[x] = max(f[x], val);
    x |= x + 1;
  }
}
int get(int x) {
  int ret = 0;
  while (x >= 0) {
    ret = max(ret, f[x]);
    x = (x & (x + 1)) - 1;
  }
  return ret;
}
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].second >> a[i].first;
    a[i].second = -a[i].second;
  }
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; ++i) swap(a[i].first, a[i].second);
  for (int i = 1; i <= n; ++i) {
    a[i].first = -a[i].first;
    dp[i] = 1;
    vector<int> pos, daun;
    for (int j = 1; j < i; ++j) {
      if (a[j].first >= a[i].first && a[j].second <= a[i].second) {
        dp[i] = max(dp[i], dp[j] + 1);
        pos.push_back(j);
        daun.push_back(a[j].second);
      }
    }
    len = pos.size() - 1;
    int mx = 0;
    for (int j = 0; j < pos.size(); ++j) {
      d[j] = dp[pos[j]];
      int nd = dp[pos[j]];
      int lol = lower_bound(daun.begin(), daun.end(), a[pos[j]].first) -
                daun.begin() - 1;
      d[j] = max(d[j], get(lol) + nd);
      mx = max(mx, d[j]);
      modify(j, d[j]);
    }
    dp[i] = max(dp[i], mx + 1);
    for (int j = 0; j < pos.size(); ++j) f[j] = 0;
    dp[i] = max(dp[i], mx + 1);
    res[i] = dp[i];
    for (int j = 1; j < i; ++j) {
      if (a[j].second < a[i].first) {
        res[i] = max(res[i], res[j] + dp[i]);
      }
    }
  }
  cout << *max_element(res + 1, res + 1 + n);
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  int tt = 1;
  cin >> tt;
  while (tt--) {
    solve();
    cout << '\n';
  }
  return 0;
}
