#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<pair<long long, long long>>>;
const long long mod = 1000000007;
class compress {
 public:
  map<long long, long long> label;
  vector<long long> inverse;
  set<long long> num;
  bool ready;
  compress(int a) {
    num.insert(0);
    label[0] = 0;
    inverse.push_back(0);
    ready = false;
  }
  void add(long long x) {
    num.insert(x);
    ready = false;
  }
  void labeling(void) {
    inverse.clear();
    int k = 0;
    for (long long x : num) {
      inverse.push_back(x);
      label[x] = k;
      k++;
    }
  }
  long long lab(long long x) {
    if (!ready) {
      labeling();
      ready = true;
    }
    return label[x];
  }
  long long inv(long long i) {
    if (!ready) {
      labeling();
      ready = true;
    }
    return inverse[i];
  }
  long long size(void) {
    if (!ready) {
      labeling();
      ready = true;
    }
    return inverse.size();
  }
};
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  long long T;
  cin >> T;
  for (int(ii) = 0; (ii) < (T); (ii)++) {
    long long N;
    cin >> N;
    compress comp(1);
    int l[N], r[N];
    for (int(i) = 0; (i) < (N); (i)++) {
      cin >> l[i] >> r[i];
      comp.add(l[i]);
      comp.add(r[i]);
    }
    int lc[N], rc[N];
    int M = comp.size();
    for (int(i) = 0; (i) < (N); (i)++) {
      lc[i] = comp.lab(l[i]);
      rc[i] = comp.lab(r[i]);
    }
    int dp[M][M];
    for (int(i) = 0; (i) < (M); (i)++) {
      for (int(j) = 0; (j) < (M); (j)++) dp[i][j] = 0;
    }
    vector<vector<long long>> st(M);
    for (int(i) = 0; (i) < (N); (i)++) {
      st[rc[i]].push_back(lc[i]);
    }
    for (int d = 0; d < M; d++) {
      for (int(i) = 0; (i) < (M); (i)++) {
        if (i + d >= M) break;
        if (d >= 1) {
          dp[i][i + d] = max(dp[i][i + d], dp[i][i + d - 1] + dp[i + d][i + d]);
          dp[i][i + d] = max(dp[i][i + d], dp[i][i] + dp[i + 1][i + d]);
        }
        int add = 0;
        for (long long x : st[i + d]) {
          if (x > i) {
            dp[i][i + d] = max(dp[i][i + d], dp[i][x - 1] + dp[x][i + d]);
          } else if (x == i) {
            add++;
          }
        }
        dp[i][i + d] += add;
      }
    }
    long long ans = dp[0][M - 1];
    cout << ans << "\n";
  }
}
