#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 2 * 1e5 + 15;
int farR[MAXN], lcnt[MAXN], s[MAXN];
struct SEG {
  int l, r;
  friend bool operator<(SEG A, SEG B) { return A.l < B.l; }
} seg[MAXN];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d%d", &seg[i].l, &seg[i].r);
    sort(seg + 1, seg + 1 + n);
    farR[0] = -2e9, lcnt[0] = 0;
    for (int i = 1; i <= n; i++) {
      if (seg[i].l > farR[i - 1])
        lcnt[i] = lcnt[i - 1] + 1;
      else
        lcnt[i] = lcnt[i - 1];
      farR[i] = max(farR[i - 1], seg[i].r);
    }
    int res = 0, t = 0;
    for (int i = n; i >= 1; i--) {
      if (i == 1) {
        res = max(res, t);
        break;
      }
      int k =
          lower_bound(s + 1, s + 1 + t, farR[i - 1], greater<int>()) - s - 1;
      res = max(res, lcnt[i - 1] + k);
      while (t != 0 && seg[i].r >= s[t]) t--;
      s[++t] = seg[i].l;
    }
    printf("%d\n", res);
  }
  return 0;
}
