#include <bits/stdc++.h>
using namespace std;
const long long Max = 6e3 + 5;
const double Pi = acos(-1);
const long long mod = 1e9 + 7;
struct Segments {
  int indx;
  int L, R;
} arr[Max];
int dp[Max][Max];
int main() {
  std::ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> resort;
    for (int i = 0; i < n; i++) {
      cin >> arr[i].L >> arr[i].R;
      arr[i].indx = i;
      resort.push_back(arr[i].L);
      resort.push_back(arr[i].R);
    }
    sort(resort.begin(), resort.end());
    resort.erase(unique(resort.begin(), resort.end()), resort.end());
    for (int i = 0; i < n; i++) {
      arr[i].L =
          lower_bound(resort.begin(), resort.end(), arr[i].L) - resort.begin();
      arr[i].R =
          lower_bound(resort.begin(), resort.end(), arr[i].R) - resort.begin();
    }
    vector<int> Rg[Max];
    int Maxlen = resort.size();
    for (int i = 0; i < n; i++) Rg[arr[i].L].push_back(arr[i].R);
    for (int i = 0; i < Maxlen; i++)
      for (int j = 0; j < Maxlen; j++) dp[i][j] = 0;
    for (int i = 0; i < Maxlen; i++) dp[i][i] = 0;
    for (int len = 1; len <= Maxlen; len++) {
      for (int L = 0; L + len - 1 < Maxlen; L++) {
        int R = L + len - 1;
        bool add = count(Rg[L].begin(), Rg[L].end(), R);
        dp[L][R] = max(dp[L][R], dp[L + 1][R] + add);
        for (auto rg : Rg[L]) {
          if (rg >= R) continue;
          dp[L][R] = max(dp[L][R], add + dp[L][rg] + dp[rg + 1][R]);
        }
      }
    }
    cout << dp[0][Maxlen - 1] << '\n';
  }
}
