#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &val) {
  val = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    val = (val << 3) + (val << 1) + (c ^ 48);
    c = getchar();
  }
  val *= f;
}
template <typename T>
void write_(T val) {
  if (!val) return;
  write_(val / 10);
  putchar(val % 10 + 48);
}
template <typename T>
inline void write(T val) {
  if (val == 0)
    putchar(48);
  else if (val < 0) {
    putchar('-');
    write_(-val);
  } else
    write_(val);
}
const int N = 4e5 + 5;
struct segment {
  long long l, r;
} s[N];
int n;
long long ans0, ans1, tmp[N << 1], d_[N << 2], d[N << 2];
int main() {
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) {
    ans0 = 0;
    ans1 = INT_MIN;
    cin >> n;
    for (register int i = 1; i <= n; i++) {
      cin >> s[i].l >> s[i].r;
      tmp[(i << 1) - 1] = s[i].l;
      tmp[i << 1] = s[i].r;
    }
    sort(tmp + 1, tmp + (n << 1) + 1);
    int tot = unique(tmp + 1, tmp + (n << 1) + 1) - tmp - 1;
    for (register int i = 1; i <= n; i++) {
      s[i].l = lower_bound(tmp + 1, tmp + tot + 1, s[i].l) - tmp;
      s[i].r = lower_bound(tmp + 1, tmp + tot + 1, s[i].r) - tmp;
      s[i].l <<= 1LL;
      s[i].r <<= 1LL;
      d[s[i].l]++;
      d[s[i].r + 1]--;
    }
    tot <<= 1LL;
    for (register long long i = 1; i <= tot + 5; i++) d[i] += d[i - 1];
    for (register long long i = 0; i <= tot + 5; i++) ans0 += d[i] && !d[i + 1];
    for (register long long i = 0; i <= tot + 4; i++)
      if (d[i] == 1 && d[i + 1] != 1) d_[i]++;
    for (register long long i = 1; i <= tot + 5; i++)
      if (d[i] == 1 && d[i - 1] != 1) d_[i]++;
    for (register long long i = 1; i <= tot + 5; i++) d_[i] += d_[i - 1];
    for (register int i = 1; i <= n; i++) {
      long long t = d_[s[i].r] - d_[s[i].l - 1];
      t /= 2;
      t -= (d[s[i].r] == 1) + (d[s[i].l] == 1);
      ans1 = max(ans1, t);
    }
    cout << ans0 + ans1 << endl;
    for (register long long i = 0; i <= tot + 10; i++) d[i] = d_[i] = 0;
  }
  return 0;
}
