#include <bits/stdc++.h>
using namespace std;
const int maxn = 200050;
int l[maxn], r[maxn], b[maxn * 2];
int d[maxn * 4], sum[maxn * 4];
int main() {
  int n, t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &l[i], &r[i]);
      b[++cnt] = l[i];
      b[++cnt] = r[i];
    }
    sort(b + 1, b + 1 + cnt);
    cnt = unique(b + 1, b + 1 + cnt) - b - 1;
    for (int i = 1; i <= cnt * 2 + 1; i++) {
      d[i] = sum[i] = 0;
    }
    int old = 0;
    for (int i = 1; i <= n; i++) {
      l[i] = lower_bound(b + 1, b + 1 + cnt, l[i]) - b;
      r[i] = lower_bound(b + 1, b + 1 + cnt, r[i]) - b;
      l[i] *= 2;
      r[i] *= 2;
      d[l[i]]++;
      d[r[i] + 1]--;
    }
    int flag = 0;
    for (int i = 1; i <= cnt * 2 + 1; i++) {
      d[i] = d[i - 1] + d[i];
      if (flag) {
        if (d[i] != 1) {
          flag = 0;
          sum[i - 1]++;
        }
      } else {
        if (d[i] == 1) {
          flag = 1;
          sum[i]++;
        }
      }
    }
    for (int i = 1; i <= cnt * 2; i++) {
      old += (d[i] && !d[i + 1]);
    }
    int ans = -2;
    for (int i = 1; i <= cnt * 2; i++) sum[i] = sum[i] + sum[i - 1];
    for (int i = 1; i <= n; i++) {
      int tmp = sum[r[i]] - sum[l[i] - 1];
      tmp /= 2;
      if (d[l[i]] == 1) tmp--;
      if (d[r[i]] == 1) tmp--;
      ans = max(ans, tmp);
    }
    printf("%d\n", ans + old);
  }
}
