#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 200000;
int n;
int l[MAXN], r[MAXN];
int x[2 * MAXN + 1], nx;
int covercnt[2 * MAXN + 1];
int deltacnt[2 * MAXN + 1];
int solve() {
  for (int i = (0); i < (n); ++i) l[i] = 2 * l[i], r[i] = 2 * r[i] + 1;
  nx = 0;
  x[nx++] = INT_MIN;
  for (int i = (0); i < (n); ++i) x[nx++] = l[i], x[nx++] = r[i];
  sort(x, x + nx);
  nx = unique(x, x + nx) - x;
  for (int i = (0); i < (n); ++i)
    l[i] = lower_bound(x, x + nx, l[i]) - x,
    r[i] = lower_bound(x, x + nx, r[i]) - x;
  for (int i = (0); i < (nx); ++i) covercnt[i] = 0;
  for (int i = (0); i < (n); ++i) ++covercnt[l[i]], --covercnt[r[i]];
  for (int i = (1); i < (nx); ++i) covercnt[i] += covercnt[i - 1];
  int have = 0;
  for (int i = (0); i < (nx - 1); ++i)
    if (covercnt[i] == 0 && covercnt[i + 1] > 0) ++have;
  for (int i = (0); i < (nx); ++i)
    deltacnt[i] = i + 1 < nx && covercnt[i] == 1 && covercnt[i + 1] > 1;
  for (int i = (1); i < (nx); ++i) deltacnt[i] += deltacnt[i - 1];
  int extra = INT_MIN;
  for (int i = (0); i < (n); ++i) {
    int cur = deltacnt[r[i] - 1] - deltacnt[l[i] - 1] -
              (covercnt[l[i]] == 1 && covercnt[l[i] - 1] == 0 ? 1 : 0);
    extra = max(extra, cur);
  }
  return have + extra;
}
void run() {
  scanf("%d", &n);
  for (int i = (0); i < (n); ++i) scanf("%d%d", &l[i], &r[i]);
  printf("%d\n", solve());
}
int main() {
  int ncase;
  scanf("%d", &ncase);
  for (int i = (1); i <= (ncase); ++i) run();
  return 0;
}
