#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  long long T;
  cin >> T;
  while (T--) {
    long long n;
    cin >> n;
    pair<long long, long long> v[n];
    map<long long, long long> m, pf, k;
    for (long long i = 0; i < n; i++) {
      cin >> v[i].first >> v[i].second;
      v[i].first *= 2, v[i].second *= 2;
      m[v[i].first]++, m[v[i].second + 1]--;
      k[v[i].second]++;
    }
    long long p = 0, l = 0, ans = 0;
    for (auto i : m) {
      m[i.first] += p;
      p = m[i.first];
      if (m[i.first] == 0) l++;
    }
    p = 0;
    for (auto i : m) {
      if (i.second == 1) p++;
      pf[i.first] = p;
    }
    for (auto i : v) {
      auto it = --pf.find(i.second + 1);
      long long x = pf[(*it).first] - pf[i.first];
      if (m[(*it).first] == 1 and m[i.second + 1] == 0) x--;
      ans = max(ans, l + x);
    }
    cout << ans << "\n";
  }
}
