#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int t, n;
pair<int, int> S[200010];
pair<int, int> rmax[200010];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> t;
  for (int q = 1; q <= t; ++q) {
    map<int, int> mp;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> S[i].first >> S[i].second;
    }
    sort(S + 1, S + n + 1);
    int cnt = 0;
    rmax[0] = make_pair(-1e9 - 100, -1e9 - 100);
    cnt = 0;
    for (int i = 1; i <= n; ++i) {
      rmax[i] = rmax[i - 1];
      if (S[i].second > rmax[i].first) {
        rmax[i].second = rmax[i].first;
        rmax[i].first = S[i].second;
      } else if (S[i].second > rmax[i].second) {
        rmax[i].second = S[i].second;
      }
      if (rmax[i - 1].first < S[i].first) {
        cnt++;
      }
      if (rmax[i - 1].first >= S[i].first && rmax[i - 1].second < S[i].first) {
        mp[rmax[i - 1].first]++;
      }
    }
    int ans = cnt - 1;
    for (int i = 1; i <= n; ++i) {
      if (S[i].second > rmax[i - 1].first) {
        int v = mp[S[i].second];
        ans = max(ans, (rmax[i - 1].first >= S[i].first) + cnt + v - 1);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
