#include <bits/stdc++.h>
using namespace std;
long long N = 1000000007;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long binpow(long long a, int n) {
  if (!n) return 1;
  if (n % 2) return binpow(a, n - 1) * a % N;
  long long bp = binpow(a, n / 2);
  return bp * bp % N;
}
vector<pair<long long, long long> > segtree;
long long segt(int v, long long a, int l, int r, int tl, int tr) {
  if (l > r) return N;
  if (l == tl && r == tr) {
    segtree[v].second += a;
    return segtree[v].first + segtree[v].second;
  }
  int tm = (tl + tr) / 2;
  segtree[2 * v + 1].second += segtree[v].second;
  segtree[2 * v + 2].second += segtree[v].second;
  long long lef = segt(2 * v + 1, a, l, min(r, tm), tl, tm);
  long long rig = segt(2 * v + 2, a, max(l, tm + 1), r, tm + 1, tr);
  segtree[v].first = min(segtree[2 * v + 1].first + segtree[2 * v + 1].second,
                         segtree[2 * v + 2].first + segtree[2 * v + 2].second);
  segtree[v].second = 0;
  return min(lef, rig);
}
struct ed {
  int p, t, i;
  bool operator<(const ed &other) const {
    return (p < other.p) || (p == other.p && t < other.t);
  }
};
int dyn(const vector<int> &nums, const vector<vector<int> > &nw,
        const vector<int> &eq, vector<int> &dep,
        const vector<pair<int, int> > &a) {
  if (nums.size() < 2) {
    return nums.size();
  }
  vector<ed> els;
  map<int, int> dp;
  dp[0] = 0;
  for (int i : nums) {
    ed e;
    e.p = a[i].first;
    e.t = 0;
    e.i = i;
    els.emplace_back(e);
    e.p = a[i].second;
    e.t = 1;
    els.emplace_back(e);
  }
  sort(els.begin(), els.end());
  int mx = 0, left = 0;
  for (int i = 0; i < els.size(); ++i) {
    if (els[i].t == 1) {
      mx = max(dp[els[i].p], mx);
    } else {
      if (dep[els[i].i] == -1) {
        dep[els[i].i] = dyn(nw[els[i].i], nw, eq, dep, a);
      }
      int val = mx + dep[els[i].i] + eq[els[i].i];
      if (dp.find(a[els[i].i].second) == dp.end()) {
        dp[a[els[i].i].second] = val;
      } else {
        dp[a[els[i].i].second] = max(dp[a[els[i].i].second], val);
      }
    }
  }
  return mx;
}
void solve() {
  int n;
  cin >> n;
  vector<pair<int, int> > a(n);
  vector<int> nums(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i].first >> a[i].second;
    nums[i] = i;
  }
  vector<vector<int> > nw(n);
  vector<int> eq(n), dep(n, -1);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (a[j].first == a[i].first && a[j].second == a[i].second) {
        ++eq[i];
      } else if (a[j].first >= a[i].first && a[j].second <= a[i].second) {
        nw[i].emplace_back(j);
      }
    }
  }
  cout << dyn(nums, nw, eq, dep, a) << endl;
}
int main() {
  int tt = 1;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}
