#include <bits/stdc++.h>
using namespace std;
const int MX = 3e3 + 5;
int n, l[MX], r[MX], q[2 * MX];
int dp[2 * MX][2 * MX];
vector<int> sg[2 * MX];
void inline upmx(int& x, int y) {
  if (y > x) x = y;
}
void solve() {
  cin >> n;
  for (int i = (0); i < (n); ++i) {
    int li, ri;
    cin >> li >> ri;
    l[i] = li;
    q[2 * i] = li;
    r[i] = ri;
    q[2 * i + 1] = ri;
  }
  sort(q, q + 2 * n);
  int m = unique(q, q + 2 * n) - q;
  for (int i = (0); i < (n); ++i) {
    l[i] = lower_bound(q, q + m, l[i]) - q;
    r[i] = lower_bound(q, q + m, r[i]) - q;
  }
  for (int j = (0); j < (m); ++j) {
    sg[j].clear();
    memset((dp[j]), 0, (m) * sizeof((dp[j])[0]));
  }
  for (int i = (0); i < (n); ++i) sg[l[i]].push_back(r[i]);
  for (int j = (0); j < (m); ++j) sort((sg[j]).begin(), (sg[j]).end());
  for (int i = (m)-1; i >= (0); --i) {
    for (int j = (i + 1); j < (m); ++j) dp[i][j] = dp[i + 1][j];
    for (auto& j : (sg[i])) {
      dp[i][j] += 1;
      for (int k = (j + 1); k < (m); ++k) {
        upmx(dp[i][k], dp[i][j] + dp[j + 1][k]);
      }
    }
  }
  cout << dp[0][m - 1] << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
