#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int INF = 1e9 + 1;
int n;
pair<int, int> segment[N];
int cal(int idx, int max_right) {
  int return_value = 0;
  if (max_right < segment[idx].first) return_value--;
  if (segment[idx].second <= max_right) return return_value;
  for (int i = idx + 1; i <= n; i++) {
    if (segment[idx].second < segment[i].first) break;
    if (max_right < segment[i].first) return_value++;
    if (segment[idx].second <= segment[i].second) break;
    max_right = max(max_right, segment[i].second);
  }
  return return_value;
}
int main() {
  int test_case;
  scanf("%d", &test_case);
  for (int test = 1; test <= test_case; test++) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &segment[i].first, &segment[i].second);
    }
    if (n == 1) {
      printf("0\n");
      continue;
    }
    sort(segment + 1, segment + 1 + n);
    int union_size = 0, max_right = segment[1].second;
    for (int i = 2; i <= n; i++) {
      if (max_right < segment[i].first) {
        union_size += 1;
      }
      max_right = max(max_right, segment[i].second);
      if (i == n) {
        union_size += 1;
      }
    }
    int ans = 0;
    max_right = -INF;
    for (int i = 1; i <= n; i++) {
      ans = max(ans, union_size + cal(i, max_right));
      max_right = max(max_right, segment[i].second);
    }
    printf("%d\n", ans);
  }
  return 0;
}
