#include <bits/stdc++.h>
using namespace std;
void faster() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
const long long INF = 1e9;
const long long MAXN = 3 << 11;
const long long MOD = 1e9 + 7;
const long long LOG = ceil(log(MAXN) / log(2));
const long double EPS = 1e-9;
const int dx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dy[8] = {0, -1, 0, 1, 1, -1, 1, -1};
int n, dp[MAXN], t[MAXN * 5], z[MAXN * 5];
pair<int, int> s[MAXN];
vector<int> val, g[MAXN];
void push(int v, int tl, int tr) {
  t[v] = max(z[v], t[v]);
  if (tl != tr) {
    z[v + v] = max(z[v + v], z[v]);
    z[v + v + 1] = max(z[v + v + 1], z[v]);
  }
  z[v] = 0;
}
int get_max(int l, int r, int v = 1, int tl = 0, int tr = val.size() - 1) {
  push(v, tl, tr);
  if (tr < l || r < tl) return 0;
  if (l <= tl && tr <= r) return t[v];
  int tm = (tl + tr) >> 1;
  return max(get_max(l, r, v + v, tl, tm),
             get_max(l, r, v + v + 1, tm + 1, tr));
}
void upd(int l, int r, int valu, int v = 1, int tl = 0,
         int tr = val.size() - 1) {
  push(v, tl, tr);
  if (tr < l || r < tl) return;
  if (l <= tl && tr <= r) {
    z[v] = max(z[v], valu);
    push(v, tl, tr);
    return;
  }
  int tm = (tl + tr) >> 1;
  upd(l, r, valu, v + v, tl, tm);
  upd(l, r, valu, v + v + 1, tm + 1, tr);
  t[v] = max(t[v + v], t[v + v + 1]);
}
void clear(int l, int r, int v = 1, int tl = 0, int tr = val.size() - 1) {
  if (tr < l || r < tl) return;
  z[v] = t[v] = 0;
  if (tl == tr) return;
  int tm = (tl + tr) >> 1;
  clear(l, r, v + v, tl, tm);
  clear(l, r, v + v + 1, tm + 1, tr);
}
int rec(int v) {
  for (int to : g[v])
    if (dp[to] == 0) {
      rec(to);
    }
  clear(s[v].first, s[v].second);
  for (int to : g[v]) {
    int x = get_max(s[v].first, s[to].first - 1) + dp[to];
    upd(s[to].second, s[to].second, x);
  }
  return dp[v] = (get_max(s[v].first, s[v].second) + 1);
}
void solve() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s[i].first >> s[i].second;
    val.push_back(s[i].first);
    val.push_back(s[i].second);
  }
  s[n].first = 0;
  s[n].second = 2e5 + 1;
  val.push_back(s[n].first);
  val.push_back(s[n].second);
  n++;
  sort(val.begin(), val.end());
  val.resize(unique(val.begin(), val.end()) - val.begin());
  for (int i = 0; i < n; ++i) {
    dp[i] = 0;
    s[i].first = lower_bound(val.begin(), val.end(), s[i].first) - val.begin();
    s[i].second =
        lower_bound(val.begin(), val.end(), s[i].second) - val.begin();
  }
  sort(s, s + n, [&](const pair<int, int> &a, const pair<int, int> &b) {
    return (a.first != b.first ? a.first < b.first : a.second > b.second);
  });
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j)
      if (i != j) {
        if (s[j].second <= s[i].second) {
          g[i].push_back(j);
        }
      }
  }
  cout << rec(0) - 1 << '\n';
  val.clear();
  for (int i = 0; i < n; ++i) {
    dp[i] = 0;
    g[i].clear();
  }
}
int main() {
  faster();
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  cerr << "\nTime elapsed " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  return 0;
}
