#include <bits/stdc++.h>
using namespace std;
const int NMAX = 3005;
const int MAX = 200005;
int N;
pair<int, int> seg[NMAX];
int dp[NMAX];
int aint[4 * MAX], best;
vector<int> graph[NMAX];
inline int LeftSon(const int& x) { return 2 * x; }
inline int RightSon(const int& x) { return 2 * x + 1; }
void Update(int node, int left, int right, const int& pos, const int& val) {
  if (left == right) {
    aint[node] = val;
    return;
  }
  int mid = (left + right) / 2;
  if (pos <= mid)
    Update(LeftSon(node), left, mid, pos, val);
  else
    Update(RightSon(node), mid + 1, right, pos, val);
  aint[node] = max(aint[LeftSon(node)], aint[RightSon(node)]);
}
void Query(int node, int left, int right, const int& LeftQuery,
           const int& RightQuery) {
  if (LeftQuery <= left && right <= RightQuery) {
    best = max(best, aint[node]);
    return;
  }
  int mid = (left + right) / 2;
  if (LeftQuery <= mid) Query(LeftSon(node), left, mid, LeftQuery, RightQuery);
  if (RightQuery >= mid + 1)
    Query(RightSon(node), mid + 1, right, LeftQuery, RightQuery);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests;
  cin >> tests;
  while (tests-- > 0) {
    cin >> N;
    for (int i = 1; i <= N; ++i) cin >> seg[i].first >> seg[i].second;
    ++N;
    seg[N] = make_pair(0, MAX - 1);
    for (int i = 1; i <= N; ++i) {
      for (int j = 1; j <= N; ++j)
        if (i != j && seg[i].first <= seg[j].first &&
            seg[j].second <= seg[i].second)
          graph[i].push_back(j);
    }
    vector<pair<int, int> > diff;
    for (int i = 1; i <= N; ++i)
      diff.push_back(make_pair(seg[i].second - seg[i].first, i));
    sort((diff).begin(), (diff).end());
    int mndiff = diff[0].first;
    int answer = 0;
    for (auto& i : diff) {
      dp[i.second] = 1;
      if (i.first == mndiff) continue;
      sort(graph[i.second].begin(), graph[i.second].end(), [&](int p, int q) {
        pair<int, int> x = seg[p], y = seg[q];
        if (x.second == y.second) return x.first < y.first;
        return x.second < y.second;
      });
      for (auto& j : graph[i.second]) {
        best = 0;
        Query(1, 0, MAX - 1, seg[i.second].first, seg[j].first - 1);
        dp[i.second] = max(dp[i.second], best + dp[j] + 1);
        answer = max(answer, dp[i.second]);
        Update(1, 0, MAX - 1, seg[j].second, dp[i.second] - 1);
      }
      for (auto& j : graph[i.second]) Update(1, 0, MAX - 1, seg[j].second, 0);
    }
    for (int i = 1; i <= N; ++i) {
      graph[i].clear();
      dp[i] = 0;
    }
    cout << max(1, answer - 1) << "\n";
  }
  return 0;
}
