#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
ostream& operator<<(ostream& cout, pair<A, B> const& p) {
  return cout << "(" << p.first << ", " << p.second << ")";
}
template <typename A>
ostream& operator<<(ostream& cout, vector<A> const& v) {
  cout << "[";
  for (int i = 0; i < v.size(); i++) {
    if (i) cout << ", ";
    cout << v[i];
  }
  return cout << "]";
}
void fast_io() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
void file_io(string taskname) {
  string fin = taskname + ".in";
  string fout = taskname + ".out";
  const char* FIN = fin.c_str();
  const char* FOUT = fout.c_str();
  freopen(FIN, "r", stdin);
  freopen(FOUT, "w", stdout);
  fast_io();
}
const int MAX = 2e5 + 5;
int main() {
  fast_io();
  int t;
  cin >> t;
  for (int ti = 0; ti < (t); ti++) {
    int n;
    cin >> n;
    vector<pair<int, int> > v(n);
    vector<int> ints;
    for (int i = 0; i < (n); i++) {
      int x, y;
      cin >> x >> y;
      v[i] = make_pair(x, y);
      ints.push_back(x);
      ints.push_back(y);
    }
    v.push_back(make_pair(1, 200000));
    ints.push_back(1);
    ints.push_back(200000);
    n++;
    sort(v.begin(), v.end(), [](pair<int, int> a, pair<int, int> b) {
      if (a.second != b.second) {
        return a.second < b.second;
      }
      return a.first > b.first;
    });
    sort(ints.begin(), ints.end());
    int ind[MAX];
    vector<vector<pair<int, int> > > lefts(2 * n + 5);
    for (int i = 0; i < (ints.size()); i++) {
      ind[ints[i]] = i + 1;
    }
    for (int i = 0; i < (n); i++) {
      v[i].first = ind[v[i].first];
      v[i].second = ind[v[i].second];
      lefts[v[i].second].push_back(make_pair(v[i].first, i));
    }
    vector<int> dp(2 * n + 5);
    vector<int> val(n);
    int tot = 0;
    for (int i = 0; i < (n); i++) {
      int ans = 0;
      dp[v[i].first - 1] = 0;
      for (int j = v[i].first; j <= v[i].second; j++) {
        int x = dp[j - 1];
        for (pair<int, int> l : lefts[j]) {
          if (l.first >= v[i].first)
            x = max(x, val[l.second] + dp[l.first - 1]);
        }
        dp[j] = x;
        ans = max(ans, x);
      }
      val[i] = ans + 1;
      tot = max(tot, ans);
    }
    cout << tot << "\n";
  }
}
