#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (a % b)
    return gcd(b, a % b);
  else
    return b;
}
long long int solve(vector<long long int> a, int b) {
  if (!(b + 1)) return -1;
  vector<long long int> nse;
  vector<long long int> se;
  long long int cur = (1LL << b);
  if (!a.size()) return -cur * 2;
  for (int i = 0; i < a.size(); i++)
    if (a[i] & cur)
      se.push_back(a[i]);
    else
      nse.push_back(a[i]);
  long long int ans1 = solve(nse, b - 1);
  long long int ans2 = solve(se, b - 1);
  return min(max(ans1, ans2 + cur), max(ans2, ans1 + cur));
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<tuple<long long int, long long int, int>> seg;
    for (int i = 0; i < n; i++) {
      long long int a, b;
      cin >> a >> b;
      seg.push_back({a, b, i});
    }
    sort(seg.begin(), seg.end());
    set<int> s;
    priority_queue<pair<long long int, int>> pq;
    int rem[n], pre[n];
    memset(rem, 0, sizeof(rem));
    memset(pre, 0, sizeof(pre));
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      while (!pq.empty() && -pq.top().first < get<0>(seg[i]))
        s.erase(pq.top().second), pq.pop();
      if (s.size() == 1) {
        rem[*(s.begin())]++;
      }
      if (!s.size()) cnt++, pre[get<2>(seg[i])] = 1;
      s.insert(get<2>(seg[i]));
      pq.push({-get<1>(seg[i]), get<2>(seg[i])});
    }
    if (cnt == n) {
      cout << n - 1 << "\n";
    } else {
      int ans = cnt;
      for (int i = 0; i < n; i++) ans = max(ans, cnt + rem[i] - pre[i]);
      cout << ans << "\n";
    }
  }
  return 0;
}
