#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma warning(disable : 6031)
using namespace std;
const int N = 3010;
struct node {
  int l, r, id, len;
} a[N];
int ox[2 * N], dp[2 * N], A[N];
bool cmp(node a, node b) { return a.len < b.len; }
bool cmp2(node a, node b) { return a.r < b.r; }
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i].l >> a[i].r;
      a[i].id = i;
      ox[i * 2 - 1] = a[i].l, ox[i * 2] = a[i].r;
    }
    sort(ox + 1, ox + 2 * n + 1);
    int xlen = unique(ox + 1, ox + 2 * n + 1) - ox - 1;
    for (int i = 1; i <= n; i++) {
      a[i].l = lower_bound(ox + 1, ox + 1 + xlen, a[i].l) - ox;
      a[i].r = lower_bound(ox + 1, ox + 1 + xlen, a[i].r) - ox;
      a[i].len = a[i].r - a[i].l + 1;
    }
    a[n + 1].l = 1;
    a[n + 1].r = xlen;
    a[n + 1].id = n + 1;
    a[n + 1].len = xlen;
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n + 1; i++) {
      fill(dp + a[i].l - 1, dp + a[i].r + 1, 0);
      vector<node> temp;
      for (int j = 1; j < i; j++) {
        if (a[j].l >= a[i].l && a[j].r <= a[i].r) temp.push_back(a[j]);
      }
      sort(temp.begin(), temp.end(), cmp2);
      int cou = 0;
      bool play = !temp.empty();
      for (int j = a[i].l; j <= a[i].r; j++) {
        dp[j] = dp[j - 1];
        while (play && temp[cou].r == j) {
          dp[j] = max(dp[j], A[temp[cou].id] + dp[temp[cou].l - 1]);
          cou++;
          if (cou == temp.size()) play = false;
        }
      }
      A[a[i].id] = dp[a[i].r] + 1;
    }
    cout << A[n + 1] - 1 << endl;
  }
  return 0;
}
