#include <bits/stdc++.h>
using namespace std;
template <typename A>
using VE = vector<A>;
template <typename A>
using USET = unordered_set<A>;
template <typename A>
using HEAP = priority_queue<A>;
template <typename A, typename B>
using PA = pair<A, B>;
template <typename A, typename B>
using UMAP = unordered_map<A, B>;
template <typename A>
using RHEAP = priority_queue<A, vector<A>, greater<A> >;
template <typename A>
A MAX(const A &a) {
  return a;
}
template <typename A>
A MIN(const A &a) {
  return a;
}
template <typename A>
A MAX(const A *a, const A *b) {
  return *max_element(a, b);
}
template <typename A>
A MIN(const A *a, const A *b) {
  return *min_element(a, b);
}
template <typename A, typename... B>
A MAX(const A &a, const B &...b) {
  return max(a, MAX(b...));
}
template <typename A, typename... B>
A MIN(const A &a, const B &...b) {
  return min(a, MIN(b...));
}
template <typename A, typename B = typename std::iterator_traits<A>::value_type>
B MAX(A a, A b) {
  return *max_element(a, b);
}
template <typename A, typename B = typename std::iterator_traits<A>::value_type>
B MIN(A a, A b) {
  return *min_element(a, b);
}
mt19937 rng(
    (unsigned int)chrono::steady_clock::now().time_since_epoch().count());
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(const char c) { return to_string((string) "" + c); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) res += ", ";
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) res += ", ";
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cout << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}
const int N = 100010, M = 1010;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<pair<int, int> > a(n);
    vector<int> p;
    for (auto &[x, y] : a) {
      cin >> x >> y;
      p.emplace_back(x);
      p.emplace_back(y);
    }
    sort(p.begin(), p.end());
    p.erase(unique(p.begin(), p.end()), p.end());
    int m = ((int)p.size());
    VE<vector<int> > f(m, vector<int>(m));
    VE<vector<int> > L(m), R(m);
    for (auto &[x, y] : a) {
      x = (int)(lower_bound(p.begin(), p.end(), x) - p.begin());
      y = (int)(lower_bound(p.begin(), p.end(), y) - p.begin());
      L[x].emplace_back(y);
      R[y].emplace_back(x);
    }
    for (int len = 1; len <= m; len++)
      for (int l = 0; l + len - 1 < m; l++) {
        int r = l + len - 1;
        if (len > 1) f[l][r] = max(f[l + 1][r], f[l][r - 1]);
        int t = 0;
        for (auto &y : L[l]) {
          t |= (int)(y == r);
          if (y + 1 > r) continue;
          f[l][r] = max(f[l][r], f[l][y] + f[y + 1][r]);
        }
        for (auto &x : R[r]) {
          t |= (int)(x == l);
          if (x - 1 < l) continue;
          f[l][r] = max(f[l][r], f[x][r] + f[l][x - 1]);
        }
        f[l][r] += t;
      }
    cout << f[0][m - 1] << '\n';
  }
  return 0;
}
