#include <bits/stdc++.h>
using namespace std;
const int dx[8] = {0, -1, 0, 1, 1, -1, -1, 1},
          dy[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
const long long mod = 1000000007;
const int base = 311;
const int N = 2e5 + 5;
vector<int> s;
int l[N], r[N];
int n;
int f[4 * N], sum[4 * N];
void gogo() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> l[i] >> r[i];
    s.push_back(l[i]);
    s.push_back(r[i]);
  }
  sort((s).begin(), (s).end());
  s.resize(unique((s).begin(), (s).end()) - s.begin());
  int mx = 0;
  for (int i = 1; i <= n; ++i) {
    l[i] = lower_bound((s).begin(), (s).end(), l[i]) - s.begin() + 1;
    r[i] = lower_bound((s).begin(), (s).end(), r[i]) - s.begin() + 1;
    l[i] *= 2;
    r[i] *= 2;
    mx = max(mx, r[i]);
    f[l[i]]++;
    f[r[i] + 1]--;
  }
  for (int i = 1; i <= mx; ++i) f[i] += f[i - 1];
  for (int i = 1; i <= mx; ++i) {
    sum[i] = sum[i - 1];
    if (f[i] == 1 && f[i - 1] >= 2) sum[i]++;
  }
  f[mx + 1] = 0;
  int cur = 0, ans = 0;
  for (int i = 1; i <= mx; ++i) cur += (f[i] >= 1 && f[i - 1] == 0);
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, cur + sum[r[i]] - sum[l[i] - 1] -
                       (f[r[i]] == 1 && f[r[i] + 1] == 0));
  }
  cout << ans << '\n';
  for (int i = 1; i <= mx + 1; ++i) f[i] = sum[i] = 0;
  s.clear();
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen("sol"
            ".inp",
            "r")) {
    freopen(
        "sol"
        ".inp",
        "r", stdin);
    freopen(
        "sol"
        ".out",
        "w", stdout);
  }
  int t;
  cin >> t;
  while (t--) gogo();
  return 0;
}
