#include <bits/stdc++.h>
using namespace std;
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
struct fenwick_tree {
  const int n = 2e5;
  vector<int> BIT;
  fenwick_tree() : BIT(n + 1) {}
  int query(int idx) {
    int mx = 0;
    while (idx) {
      mx = max(mx, BIT[idx]);
      idx -= (idx & -idx);
    }
    return mx;
  }
  void update(int idx, int val) {
    assert(idx != 0);
    while (idx <= n) {
      BIT[idx] = max(BIT[idx], val);
      idx += (idx & -idx);
    }
  }
};
int main() {
  run();
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int>> v(n);
    for (auto &it : v) cin >> it.first >> it.second;
    v.push_back({1, 2e5});
    sort(v.begin(), v.end(), [&](pair<int, int> &a, pair<int, int> &b) {
      if (a.first != b.first) return a.first > b.first;
      return a.second < b.second;
    });
    vector<int> dp(n + 1, 1);
    fenwick_tree seg;
    for (int i = 0; i <= n; i++) {
      for (int j = i - 1; j >= 0; j--)
        if (v[i].first <= v[j].first && v[j].second <= v[i].second) {
          int val = dp[j] + seg.query(v[j].first - 1);
          dp[i] = max(dp[i], val + 1);
          seg.update(v[j].second, val);
        }
    }
    cout << *max_element(dp.begin(), dp.end()) - 1 << "\n";
  }
}
