#include <bits/stdc++.h>
template <class t>
inline void read(t &s) {
  s = 0;
  register int f = 1;
  register char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
  s *= f;
  return;
}
template <class t, class... A>
inline void read(t &x, A &...a) {
  read(x);
  read(a...);
}
template <class t>
inline void write(t x) {
  if (x < 0) putchar('-'), x = -x;
  int buf[21], top = 0;
  while (x) buf[++top] = x % 10, x /= 10;
  if (!top) buf[++top] = 0;
  while (top) putchar(buf[top--] ^ '0');
  return;
}
inline void setIn(std::string s) {
  freopen(s.c_str(), "r", stdin);
  return;
}
inline void setOut(std::string s) {
  freopen(s.c_str(), "w", stdout);
  return;
}
inline void setIO(std::string s = "") {
  setIn(s + ".in");
  setOut(s + ".out");
  return;
}
template <class t>
inline bool ckmin(t &x, t y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
template <class t>
inline bool ckmax(t &x, t y) {
  if (x < y) {
    x = y;
    return 1;
  }
  return 0;
}
inline int lowbit(int x) { return x & (-x); }
const int MaxN = 6050;
struct Node {
  int l, r;
} a[MaxN];
int f[MaxN], g[MaxN][MaxN];
std::bitset<MaxN> is[MaxN];
std::vector<int> L[MaxN];
inline int get(std::vector<int> &A, int x) {
  return std::lower_bound((A).begin(), (A).end(), x) - A.begin() + 1;
}
inline int dfs(int l, int r) {
  if (l > r) return 0;
  if (~g[l][r]) return g[l][r];
  g[l][r] = dfs(l + 1, r);
  for (auto k : L[l])
    if (k < r) ckmax(g[l][r], dfs(l, k) + dfs(k + 1, r));
  return g[l][r] += is[l][r];
}
inline void work() {
  int n, m;
  read(n);
  std::vector<int> Dx;
  for (int i = 1; i <= n; ++i)
    read(a[i].l, a[i].r), Dx.push_back(a[i].l), Dx.push_back(a[i].r);
  std::sort((Dx).begin(), (Dx).end()),
      Dx.erase(std::unique((Dx).begin(), (Dx).end()), Dx.end());
  for (int i = 1; i <= n; ++i)
    a[i].l = get(Dx, a[i].l), a[i].r = get(Dx, a[i].r);
  m = (int)Dx.size();
  for (int i = 1; i <= m; ++i)
    for (int j = 1; j <= m; ++j) is[i][j] = 0, g[i][j] = -1;
  for (int i = 1; i <= n; ++i) is[a[i].l][a[i].r] = 1;
  for (int i = 1; i <= m; ++i) L[i].clear();
  for (int i = 1; i <= n; ++i) L[a[i].l].push_back(a[i].r);
  std::memset(f, 0, sizeof f);
  for (int i = 1; i <= m; ++i)
    for (int j = 0; j < i; ++j) ckmax(f[i], f[j] + dfs(j + 1, i));
  write(f[m]), std::puts("");
}
signed main(void) {
  int t;
  read(t);
  while (t--) work();
  return 0;
}
