#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    map<int, vector<int> > mp;
    set<int> unvisited_intervals;
    vector<pair<int, int> > all_intervals;
    for (int i = 1; i <= n; ++i) {
      int l, r;
      cin >> l >> r;
      all_intervals.push_back({l, r});
      mp[l].push_back(i);
      mp[r].push_back(-i);
      unvisited_intervals.insert(i);
    }
    sort(all_intervals.begin(), all_intervals.end());
    bool intersection = false;
    for (int i = 0; i < n - 1; ++i) {
      if (all_intervals[i].second >= all_intervals[i + 1].first) {
        intersection = true;
        break;
      }
    }
    if (!intersection) {
      cout << n - 1 << endl;
      continue;
    }
    vector<int> cnt(n + 1, 0), status(n + 1, 0);
    set<int> interval_idx;
    int num_components = 0;
    for (auto &item : mp) {
      int sz = item.second.size(), interval_OI = -1;
      bool some_intervals_erased = false;
      bool some_intervals_added = false;
      if (interval_idx.size() == 1) {
        interval_OI = *interval_idx.begin();
      }
      for (int idx : item.second) {
        if (idx > 0) {
          interval_idx.insert(idx);
          some_intervals_added = true;
        } else {
          interval_idx.erase(-idx);
          some_intervals_erased = true;
          if (unvisited_intervals.find(-idx) != unvisited_intervals.end()) {
            unvisited_intervals.erase(-idx);
          }
        }
      }
      if (some_intervals_added && interval_OI != -1 && status[interval_OI]) {
        ++cnt[interval_OI];
        status[interval_OI] = 0;
      }
      int depth = interval_idx.size();
      num_components += (depth == 0);
      if (depth == 1 && some_intervals_erased) {
        status[*interval_idx.begin()] = 1;
      }
    }
    int max_cnt = -1;
    for (int i = 1; i <= n; ++i) {
      max_cnt = max(max_cnt, cnt[i]);
    }
    cout << num_components + max_cnt << endl;
  }
  return 0;
}
