#include <bits/stdc++.h>
#pragma optimize("SEX_ON_THE_BEACH")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#pragma GCC optimize("fast-math")
using ll = long long int;
using ull = long long int;
using dd = double;
using ldd = long double;
namespace Hashes {
const int mod197 = 1e9 + 7;
const int mod199 = 1e9 + 9;
const int modfft = 998244353;
int MOD = mod197;
template <typename T = int>
struct Hash {
  T me, mod;
  Hash() {}
  Hash(T _me, T _mod = MOD) {
    me = _me, mod = _mod;
    if (me >= mod) me %= mod;
  }
  Hash operator+(Hash to) {
    to.me += me;
    if (to.me >= to.mod) to.me -= to.mod;
    return to;
  }
  Hash operator-(Hash to) {
    to.me -= me;
    to.me *= -1;
    if (to.me < 0) to.me += to.mod;
    return to;
  }
  Hash operator*(Hash to) {
    to.me *= me;
    to.me %= to.mod;
    return to;
  }
  Hash& operator+=(Hash to) {
    me += to.me;
    if (me >= mod) me -= mod;
    return *this;
  }
  Hash& operator-=(Hash to) {
    me -= to.me;
    if (me < 0) me += mod;
    return *this;
  }
  Hash& operator*=(Hash to) {
    me *= to.me;
    me %= mod;
    return *this;
  }
  bool operator==(Hash to) { return me == to.me; }
  T operator*() { return me; }
};
}  // namespace Hashes
namespace someUsefull {
template <typename T1, typename T2>
inline void checkMin(T1& a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void checkMax(T1& a, T2 b) {
  if (a < b) a = b;
}
const int _popcount_size_ = 16;
char _bits_count_[1 << _popcount_size_];
inline void _build_bits_count_() {
  for (int i = 1, j = 0; i < (1 << _popcount_size_); ++i) {
    if (i & (1 << (j + 1))) ++j;
    _bits_count_[i] = _bits_count_[i ^ (1 << j)] + 1;
  }
}
template <typename T1 = int, typename T2 = int>
inline T1 popcnt(T2 x) {
  T1 ans = 0;
  while (x) {
    ans += _bits_count_[x & (1 << _popcount_size_) - 1];
    x >>= _popcount_size_;
  }
  return ans;
}
}  // namespace someUsefull
using namespace std;
using namespace Hashes;
using namespace someUsefull;
bool in(pair<int, int> a, pair<int, int> b) {
  if (a == b) return 0;
  return a.first >= b.first && a.second <= b.second;
}
void solve(int test) {
  int n;
  cin >> n;
  vector<pair<int, int>> arr(n);
  map<int, int> ord;
  for (int i = 0; i < n; ++i) cin >> arr[i].first >> arr[i].second;
  for (int i = 0; i < n; ++i) ord[arr[i].first] = ord[arr[i].second] = 0;
  int x = 1;
  for (auto& i : ord) i.second = x++;
  for (int i = 0; i < n; ++i)
    arr[i].first = ord[arr[i].first], arr[i].second = ord[arr[i].second];
  vector<pair<int, int>> rSort = arr;
  sort((rSort).begin(), (rSort).end(),
       [&](pair<int, int> a, pair<int, int> b) { return a.second < b.second; });
  vector<pair<int, int>> sSort = arr;
  sort((sSort).begin(), (sSort).end(), [&](pair<int, int> a, pair<int, int> b) {
    return a.second - a.first < b.second - b.first;
  });
  vector<int> dp(x, 0);
  map<pair<int, int>, int> az;
  sSort.push_back({0, 1e9});
  for (auto i : sSort) az[i] = 1;
  for (int i = 0; i < n + 1; ++i) {
    dp.assign(x, 0);
    int l = 0;
    for (int t = 0; t < n; ++t) {
      if (!in(rSort[t], sSort[i])) continue;
      while (l < rSort[t].second) {
        if (l) checkMax(dp[l], dp[l - 1]);
        ++l;
      }
      checkMax(dp[l], dp[rSort[t].first - 1] + az[rSort[t]]);
    }
    while (l < dp.size()) {
      if (l) checkMax(dp[l], dp[l - 1]);
      ++l;
    }
    az[sSort[i]] += dp.back();
  }
  cout << az[sSort.back()] - 1 << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  _build_bits_count_();
  int t = 1;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    solve(i + 1);
  }
  return 0;
}
