#include <bits/stdc++.h>
using namespace std;
const int LINF = 1e17;
const double pi = 3.1415926535897932;
const string endstr = "\n";
template <typename T>
T gcd(T a, T b) {
  return (a == 0) ? b : gcd(b % a, a);
}
template <typename T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
bool p_comp_fs(const pair<int, int> p1, const pair<int, int> p2) {
  return p1.first < p2.first;
};
bool p_comp_fg(const pair<int, int> p1, const pair<int, int> p2) {
  return p1.first > p2.first;
};
bool p_comp_ss(const pair<int, int> p1, const pair<int, int> p2) {
  return p1.second < p2.second;
};
bool p_comp_sg(const pair<int, int> p1, const pair<int, int> p2) {
  return p1.second > p2.second;
};
template <typename T>
vector<T> uniquen(vector<T> vec) {
  sort((vec).begin(), (vec).end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  return vec;
}
inline int popcnt(int x) {
  return __builtin_popcountll((unsigned long long)x);
};
template <class T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
bool bit(int st, int b) { return ((st >> b) & 1) == 1; }
vector<vector<int>> memo, lseg;
vector<vector<bool>> cseg;
int dfs(int l, int r) {
  if (l > r) return 0;
  if (memo[l][r] >= 0) return memo[l][r];
  int res = 0, add = 0;
  if (cseg[l][r]) add++;
  chmax(res, dfs(l + 1, r) + add);
  for (int tr : lseg[l]) {
    if (tr >= r) continue;
    chmax(res, dfs(l, tr) + dfs(tr + 1, r) + add);
  }
  return memo[l][r] = res;
}
void solve() {
  cseg.clear();
  memo.clear();
  lseg.clear();
  int N;
  cin >> N;
  vector<pair<int, int>> segs(N);
  vector<int> all;
  for (int i = 0; i < N; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    segs[i] = {l, r};
    all.push_back(l);
    all.push_back(r);
  }
  all = uniquen(all);
  lseg = vector<vector<int>>(all.size() + 1);
  memo = vector<vector<int>>(all.size() + 1, vector<int>(all.size() + 1, -1));
  cseg =
      vector<vector<bool>>(all.size() + 1, vector<bool>(all.size() + 1, false));
  for (int i = 0; i < N; i++) {
    int cl =
        lower_bound((all).begin(), (all).end(), segs[i].first) - all.begin();
    int cr =
        lower_bound((all).begin(), (all).end(), segs[i].second) - all.begin();
    cseg[cl][cr] = true;
    lseg[cl].push_back(cr);
  }
  cout << dfs(0, all.size() - 1) << endstr;
}
int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  for (int _ = 0; _ < T; _++) {
    solve();
  }
  return 0;
}
