#include <bits/stdc++.h>
const int MAXN = 1e6 + 5;
std::vector<int> S;
int n;
int l[MAXN], r[MAXN];
int d[MAXN], dd[MAXN];
inline void Solve() {
  scanf("%d", &n);
  S.clear();
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", l + i, r + i);
    S.push_back(l[i]);
    S.push_back(r[i]);
  }
  std::sort(S.begin(), S.end());
  S.erase(std::unique(S.begin(), S.end()), S.end());
  for (int i = 1; i <= n; ++i)
    l[i] = std::lower_bound(S.begin(), S.end(), l[i]) - S.begin() + 1,
    r[i] = std::lower_bound(S.begin(), S.end(), r[i]) - S.begin() + 1;
  int m = 0;
  for (int i = 1; i <= n; ++i) {
    l[i] <<= 1;
    r[i] <<= 1;
    m = std::max(m, r[i]);
  }
  for (int i = 1; i <= n; ++i) d[l[i]]++, d[r[i] + 1]--;
  for (int i = 1; i <= m; ++i) d[i] += d[i - 1];
  d[m + 1] = 0;
  d[m + 2] = -1;
  for (int i = 1; i <= m; ++i)
    dd[i] =
        dd[i - 1] + (d[i] == 1 && d[i - 1] != 1) + (d[i] == 1 && d[i + 1] != 1);
  int ans = -1;
  for (int i = 1; i <= n; ++i) {
    ans = std::max(
        ans, (dd[r[i]] - dd[l[i] - 1]) / 2 - (d[l[i]] == 1) - (d[r[i]] == 1));
  }
  int ori = 1;
  for (int i = 0; i <= m + 1; ++i)
    ori += (d[i] == 0 && d[std::max(0, i - 1)] != 0) +
           (d[i] == 0 && d[i + 1] != 0);
  ori >>= 1;
  --ori;
  printf("%d\n", ans + ori);
  for (int i = 1; i <= m + 2; ++i) d[i] = dd[i] = 0;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) Solve();
  return 0;
}
