#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> a[200005];
map<int, int> how;
int score[200005];
struct Event {
  int val, idx, type;
  Event(int val = 0, int idx = 0, int type = 0)
      : val(val), idx(idx), type(type) {}
  bool operator<(const Event& x) const {
    if (val != x.val) return val < x.val;
    if (type != x.type) return type < x.type;
    return a[idx] > a[x.idx];
  }
};
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    vector<Event> events;
    how.clear();
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &a[i].first, &a[i].second);
      events.push_back(Event(a[i].first, i, 1));
      events.push_back(Event(a[i].second + 1, i, 0));
      how[a[i].second + 1]++;
      score[i] = 0;
    }
    sort(events.begin(), events.end());
    multiset<long long> ends;
    multiset<long long> active;
    int ans = 0;
    for (int i = 0; i < events.size(); i++) {
      Event cur = events[i];
      if (cur.type == 1) {
        active.insert(cur.idx);
      } else {
        active.erase(cur.idx);
        if (active.size() && score[cur.idx]) {
          score[cur.idx]++;
        }
        if (active.size() == 1) {
          score[(*active.rbegin())]++;
        }
        if (active.size() == 0) ans++;
      }
    }
    int mx = -1;
    for (int i = 0; i < n; i++) {
      mx = max(mx, score[i]);
    }
    mx--;
    mx = max(mx, 0);
    if (ans == n) {
      printf("%d\n", n - 1);
      continue;
    }
    printf("%d\n", ans + mx);
  }
  return 0;
}
