#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vector<T>>;
constexpr char ln = '\n';
istream& operator>>(istream& is, __int128_t& x) {
  x = 0;
  string s;
  is >> s;
  int n = int(s.size()), it = 0;
  if (s[0] == '-') it++;
  for (; it < n; it++) x = (x * 10 + s[it] - '0');
  if (s[0] == '-') x = -x;
  return is;
}
ostream& operator<<(ostream& os, __int128_t x) {
  if (x == 0) return os << 0;
  if (x < 0) os << '-', x = -x;
  deque<int> deq;
  while (x) deq.emplace_front(x % 10), x /= 10;
  for (int e : deq) os << e;
  return os;
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (int i = 0; i < int(v.size()); i++) {
    if (i) os << ", ";
    os << v[i];
  }
  return os << "}";
}
template <class Container>
inline int SZ(Container& v) {
  return int(v.size());
}
template <class T>
inline void UNIQUE(vector<T>& v) {
  v.erase(unique(v.begin(), v.end()), v.end());
}
template <class T1, class T2>
inline bool chmax(T1& a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2>
inline bool chmin(T1& a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
inline int topbit(ull x) { return x == 0 ? -1 : 63 - __builtin_clzll(x); }
inline int botbit(ull x) { return x == 0 ? 64 : __builtin_ctzll(x); }
inline int popcount(ull x) { return __builtin_popcountll(x); }
inline int kthbit(ull x, int k) { return (x >> k) & 1; }
inline constexpr long long TEN(int x) { return x == 0 ? 1 : TEN(x - 1) * 10; }
inline void print() { cout << "\n"; }
template <class T>
inline void print(const vector<T>& v) {
  for (int i = 0; i < int(v.size()); i++) {
    if (i) cout << " ";
    cout << v[i];
  }
  print();
}
template <class T, class... Args>
inline void print(const T& x, const Args&... args) {
  cout << x << " ";
  print(args...);
}
struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T>
struct Compress {
  vector<T> xs;
  Compress(){};
  Compress(const vector<T>& vs) { add(vs); }
  Compress(const initializer_list<vector<T>>& vs) {
    for (auto& p : vs) add(p);
  }
  void add(const vector<T>& vs) { copy(begin(vs), end(vs), back_inserter(xs)); }
  void add(const T& x) { xs.emplace_back(x); }
  int build() {
    sort(begin(xs), end(xs));
    xs.erase(unique(begin(xs), end(xs)), end(xs));
    return (int)xs.size();
  }
  vector<int> get(const vector<T>& vs) const {
    vector<int> ret;
    transform(begin(vs), end(vs), back_inserter(ret), [&](const T& x) {
      return lower_bound(begin(xs), end(xs), x) - begin(xs);
    });
    return ret;
  }
  int get(const T& x) const {
    return lower_bound(begin(xs), end(xs), x) - begin(xs);
  }
  const T& operator[](int k) const { return xs[k]; }
};
constexpr int MAX = 2e5;
void solve() {
  int N;
  cin >> N;
  vec<int> L_(N), R_(N);
  for (int i = 0; i < (N); i++) cin >> L_[i] >> R_[i], L_[i]--;
  L_.emplace_back(0);
  R_.emplace_back(MAX + 1);
  Compress<int> Com;
  Com.add(L_);
  Com.add(R_);
  int M = Com.build();
  auto L = Com.get(L_);
  auto R = Com.get(R_);
  N++;
  vec<int> idx1(N), idx2(N);
  iota((idx1).begin(), (idx1).end(), 0);
  iota((idx2).begin(), (idx2).end(), 0);
  sort((idx1).begin(), (idx1).end(),
       [&](int i, int j) { return R[i] - L[i] < R[j] - L[j]; });
  sort((idx2).begin(), (idx2).end(), [&](int i, int j) { return R[i] < R[j]; });
  vec<int> dp(N);
  for (int i = 0; i < (N); i++) {
    int u = idx1[i];
    vec<int> dp2(M);
    int cur = 0;
    for (int j = 0; j < (N); j++) {
      int v = idx2[j];
      if (v == u) continue;
      if (L[v] < L[u] or R[u] < R[v]) continue;
      while (cur < R[v]) {
        dp2[cur + 1] = dp2[cur];
        cur++;
      }
      chmax(dp2[cur], dp2[L[v]] + dp[v]);
    }
    dp[u] = dp2[cur] + 1;
  }
  cout << dp[N - 1] - 1 << ln;
}
int main() {
  int Q;
  cin >> Q;
  while (Q--) {
    solve();
  }
}
