#include <bits/stdc++.h>
using namespace std;
vector<int> coordinate_compression(vector<long long> &a, int cur = 0) {
  int n = a.size();
  vector<long long> b(n);
  copy(a.begin(), a.end(), b.begin());
  sort(b.begin(), b.end());
  unordered_map<long long, int> mp;
  for (int i = 0; i < n; i++) {
    if ((i > 0) && b[i] == b[i - 1]) continue;
    mp[b[i]] = cur++;
  }
  vector<int> res(n);
  for (int i = 0; i < n; i++) {
    res[i] = mp[a[i]];
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<pair<long long, long long>> a(n);
    for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
    vector<long long> b(2 * n);
    for (int i = 0; i < n; i++) {
      b[2 * i] = a[i].first;
      b[2 * i + 1] = a[i].second;
    }
    vector<int> bb = coordinate_compression(b);
    int m = 0;
    for (int i = 0; i < n; i++) {
      a[i].first = bb[2 * i] * 2;
      a[i].second = bb[2 * i + 1] * 2;
      m = max(m, (int)a[i].first);
      m = max(m, (int)a[i].second);
    }
    m += 5;
    vector<int> rui(m);
    for (int i = 0; i < n; i++) {
      rui[a[i].first]++;
      rui[a[i].second + 1]--;
    }
    for (int i = 1; i < m; i++) {
      rui[i] += rui[i - 1];
    }
    sort(a.begin(), a.end(),
         [&](pair<long long, long long> &p0, pair<long long, long long> &p1) {
           return p0.second < p1.second;
         });
    stack<long long> st;
    for (int i = 0; i < n; i++) {
      while (st.size() && st.top() >= a[i].first) st.pop();
      st.push(a[i].second);
    }
    int res = st.size();
    if (res == n) {
      cout << res - 1 << "\n";
      continue;
    }
    vector<int> co(m);
    int sum = 0;
    for (int i = 0; i < m; i++) {
      if (rui[i] == 1) sum++;
      co[i] = sum;
    }
    int mx = 0;
    for (int i = 0; i < n; i++) {
      int left = a[i].first + 1;
      int right = a[i].second - 1;
      if (rui[a[i].first] == 1) left++;
      if (rui[a[i].second] == 1) right--;
      if (left > right) continue;
      int te = co[right];
      if (left > 0) te -= co[left - 1];
      mx = max(mx, te);
    }
    res += mx;
    cout << res << "\n";
  }
  return 0;
}
