#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 7;
const int inf = 1e9 + 1;
int read() {
  int ans = 0, f = 1, c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + (c - '0');
    c = getchar();
  }
  return ans * f;
}
int T, n, ans, base, cnt;
struct node {
  int l, r;
} e[M];
bool cmp(node a, node b) { return a.l == b.l ? a.r < b.r : a.l < b.l; }
int main() {
  T = read();
  while (T--) {
    n = read();
    ans = 0;
    cnt = 0;
    base = 0;
    for (int i = 1; i <= n; i++) e[i].l = read(), e[i].r = read();
    sort(e + 1, e + 1 + n, cmp);
    bool f = 0;
    for (int i = 2; i <= n; i++)
      if (e[i].l <= e[i - 1].r) f = 1;
    if (!f) {
      printf("%d\n", n - 1);
      continue;
    }
    int mxr = -inf, L = inf, R = -inf, Tail = -inf;
    for (int i = 1; i <= n; i++) {
      if (mxr >= L && mxr < e[i].l && e[i].l <= R) cnt++;
      ans = max(ans, cnt);
      if (e[i].r < R)
        mxr = max(mxr, e[i].r);
      else {
        mxr = max(mxr, R);
        cnt = 0;
        L = e[i].l;
        R = e[i].r;
      }
      if (e[i].l > Tail) base++;
      Tail = max(Tail, e[i].r);
    }
    printf("%d\n", base + ans);
  }
  return 0;
}
