#include <bits/stdc++.h>
using namespace std;
inline void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
struct seg {
  int l, r;
  seg(int _l, int _r) : l(_l), r(_r) {}
  seg() {}
};
seg readseg() {
  int a, b;
  cin >> a >> b;
  return seg(a, b);
}
int compress(vector<seg>& v) {
  int n = (int)v.size();
  vector<int> coordinates;
  for (int i = 0; i < n; ++i) {
    coordinates.push_back(v[i].l);
    coordinates.push_back(v[i].r);
  }
  sort(coordinates.begin(), coordinates.end());
  coordinates.erase(unique(coordinates.begin(), coordinates.end()),
                    coordinates.end());
  for (int i = 0; i < n; ++i) {
    v[i].l = lower_bound(coordinates.begin(), coordinates.end(), v[i].l) -
             coordinates.begin();
    v[i].r = lower_bound(coordinates.begin(), coordinates.end(), v[i].r) -
             coordinates.begin();
  }
  return coordinates.size();
}
void solve() {
  int n;
  cin >> n;
  vector<seg> v(n);
  for (int i = 0; i < n; ++i) {
    v[i] = readseg();
  }
  int x = compress(v);
  vector<vector<int> > dp(x, vector<int>(x, 0));
  vector<vector<int> > nums(x);
  for (int i = 0; i < n; ++i) {
    nums[v[i].l].push_back(i);
  }
  for (int len = 1; len <= x; ++len) {
    for (int l = 0; l + len - 1 < x; ++l) {
      int r = l + len - 1;
      if (l + 1 < x) dp[l][r] = max(dp[l + 1][r], dp[l][r]);
      if (r > 0) dp[l][r] = max(dp[l][r - 1], dp[l][r]);
      int add = 0;
      for (int i = 0; i < nums[l].size(); ++i) {
        if (r == v[nums[l][i]].r) add++;
      }
      for (int i = 0; i < nums[l].size(); ++i) {
        if (r <= v[nums[l][i]].r) continue;
        dp[l][r] =
            max(dp[l][r], dp[l][v[nums[l][i]].r] + dp[v[nums[l][i]].r + 1][r]);
      }
      dp[l][r] += add;
    }
  }
  cout << dp[0][x - 1] << '\n';
}
signed main() {
  fastio();
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
