#include <bits/stdc++.h>
using namespace std;
void _init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(10);
  cout << fixed;
}
int main() {
  _init();
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> d;
    vector<pair<int, int>> seg(n);
    for (auto &i : seg) {
      cin >> i.first >> i.second;
      d.push_back(i.first);
      d.push_back(i.second);
    }
    sort((d).begin(), (d).end());
    d.erase(unique((d).begin(), (d).end()), d.end());
    int mx = 0;
    for (auto &i : seg) {
      i.first = lower_bound((d).begin(), (d).end(), i.first) - d.begin() + 1;
      i.second = lower_bound((d).begin(), (d).end(), i.second) - d.begin() + 1;
      i.first *= 2;
      i.second *= 2;
      mx = max(mx, i.second);
    }
    mx += 2;
    vector<vector<int>> inc(mx), dec(mx);
    for (int i = 0; i < n; ++i) {
      inc[seg[i].first].push_back(i);
      dec[seg[i].second].push_back(i);
    }
    vector<pair<int, int>> cnt(mx);
    set<int> s;
    for (int i = 0; i < mx; ++i) {
      for (auto &j : inc[i]) {
        s.insert(j);
      }
      if ((int)(s).size() == 0)
        ;
      else if ((int)(s).size() == 1)
        cnt[i] = {1, *s.begin()};
      else
        cnt[i] = {2, -1};
      for (auto &j : dec[i]) {
        s.erase(j);
      }
    }
    int ori = 0;
    for (int i = 1; i < mx; ++i) {
      if (cnt[i - 1].first == 0 && cnt[i].first) ++ori;
    }
    if (ori == n) {
      cout << n - 1 << endl;
      continue;
    }
    vector<int> ans(n);
    for (int i = 0; i < mx;) {
      if (cnt[i].first == 1) {
        int j = i;
        while (cnt[j + 1].first == 1) ++j;
        if (cnt[i - 1].first == 2 && cnt[j + 1].first == 2)
          ans[cnt[i].second]++;
        i = j + 1;
      } else {
        ++i;
      }
    }
    cout << ori + *max_element((ans).begin(), (ans).end()) << endl;
  }
  return 0;
}
