#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const long long N = 1e6 + 10;
const long long inf = (long long)1e18 + 10ll;
const long long mod = 998244353;
const long long eps = 1e-9;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
using namespace std;
long long q;
long long n;
pair<long long, long long> a[N];
long long pref[N];
long long suf[N];
long long rs[N];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  while (q--) {
    cin >> n;
    for (long long i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
    sort(a, a + n);
    long long mxr = -inf;
    rs[0] = -inf;
    for (long long i = 0; i < n; ++i) {
      pref[i + 1] = pref[i];
      if (a[i].first > mxr) ++pref[i + 1];
      mxr = max(mxr, a[i].second);
      rs[i + 1] = mxr;
    }
    vector<pair<long long, long long> > st;
    long long ans = 0;
    for (long long i = n - 1; i > 0; --i) {
      auto it = lower_bound(st.begin(), st.end(), make_pair(rs[i], inf),
                            greater<pair<long long, long long> >());
      long long add = -((long long)st.size() - (it - st.begin()));
      if (it == st.end()) add = 0;
      ans = max(ans, pref[i] + (long long)st.size() + add);
      long long nr = a[i].second;
      while (!st.empty() && nr >= st.back().first) {
        nr = max(nr, st.back().second);
        st.pop_back();
      }
      st.emplace_back(a[i].first, nr);
    }
    ans = max(ans, (long long)st.size());
    cout << ans << "\n";
  }
}
