#include <bits/stdc++.h>
using namespace std;
struct fast_ios {
  fast_ios() {
    cin.tie(nullptr), ios::sync_with_stdio(false),
        cout << fixed << setprecision(20);
  };
} fast_ios_;
template <class T>
auto add = [](T a, T b) -> T { return a + b; };
template <class T>
auto f_max = [](T a, T b) -> T { return max(a, b); };
template <class T>
auto f_min = [](T a, T b) -> T { return min(a, b); };
template <class T>
using V = vector<T>;
using Vl = V<int>;
using VVl = V<Vl>;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (int i = 0; i < (int)v.size(); i++)
    os << v[i] << (i + 1 != v.size() ? " " : "");
  return os;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& in : v) is >> in;
  return is;
}
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int ceil(int a, int b) { return (a + b - 1) / b; }
int digit(int a) { return (int)log10(a); }
int dist(pair<int, int> a, pair<int, int> b) {
  return abs(a.first - b.first) * abs(a.first - b.first) +
         abs(a.second - b.second) * abs(a.second - b.second);
}
int m_dist(pair<int, int> a, pair<int, int> b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}
void Worshall_Floyd(VVl& g) {
  for (int k = (0), k_end_ = (((int)(g).size())); k < k_end_; k++)
    for (int i = (0), i_end_ = (((int)(g).size())); i < i_end_; i++)
      for (int j = (0), j_end_ = (((int)(g).size())); j < j_end_; j++)
        chmin(g[i][j], g[i][k] + g[k][j]);
}
const int MOD = 1e9 + 7, INF = 5e18;
int dx[8] = {1, 0, -1, 0, 1, -1, 1, -1}, dy[8] = {0, 1, 0, -1, -1, -1, 1, 1};
bool YN(bool flag) {
  cout << (flag ? "YES" : "NO") << '\n';
  return flag;
}
bool yn(bool flag) {
  cout << (flag ? "Yes" : "No") << '\n';
  return flag;
}
struct Edge {
  int from, to;
  int cost;
  Edge(int u, int v, int c) {
    cost = c;
    from = u;
    to = v;
  }
  bool operator<(const Edge& e) const { return cost < e.cost; }
};
struct WeightedEdge {
  int to;
  int cost;
  WeightedEdge(int v, int c = 1) {
    to = v;
    cost = c;
  }
  bool operator<(const WeightedEdge& e) const { return cost < e.cost; }
};
using WeightedGraph = V<V<WeightedEdge>>;
int N, l, r, T;
VVl dp, rg;
int dfs(int l, int r) {
  if (dp[l][r] != -1) return dp[l][r];
  dp[l][r] = 0;
  if (l > r) return dp[l][r];
  bool add = count((rg[l]).begin(), (rg[l]).end(), r);
  chmax(dp[l][r], add + (l + 1 > r ? 0 : dfs(l + 1, r)));
  for (int nr : rg[l]) {
    if (nr >= r) continue;
    chmax(dp[l][r], add + dfs(l, nr) + (nr + 1 > r ? 0 : dfs(nr + 1, r)));
  }
  return dp[l][r];
}
int main() {
  cin >> T;
  while (T--) {
    cin >> N;
    V<pair<int, int>> vec(N);
    set<int> xs;
    for (int i = (0), i_end_ = (N); i < i_end_; i++) {
      cin >> l >> r;
      xs.insert(l);
      xs.insert(r);
      vec[i] = {l, r};
    }
    map<int, int> fx;
    int idx = 0;
    for (int v : xs) {
      fx[v] = idx;
      idx++;
    }
    for (int i = (0), i_end_ = (N); i < i_end_; i++) {
      vec[i] = {fx[vec[i].first], fx[vec[i].second]};
    }
    dp.assign(idx + 1, Vl(idx + 1, -1));
    rg.assign(idx + 1, Vl());
    for (int i = (0), i_end_ = (N); i < i_end_; i++) {
      rg[vec[i].first].push_back(vec[i].second);
    }
    cout << dfs(0, idx - 1) << '\n';
  }
}
