#include <bits/stdc++.h>
using namespace std;
struct data {
  pair<long long, long long> d;
  bool operator<(data a) const { return d.first > a.d.first; }
};
int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    priority_queue<pair<long long, long long> > start, end;
    for (int i = 0; i < n; i++) {
      pair<long long, long long> a;
      cin >> a.first >> a.second;
      start.push({-a.first, -a.second});
    }
    start.push({-1e17, -1e17});
    int darab = 0;
    long long sol = 0;
    int plus = 0;
    while (start.size() > 0) {
      long long pre;
      bool del = false;
      while (end.size() > 0 && end.top().first > start.top().first) {
        del = true;
        sol = max(sol, darab - end.top().second);
        end.pop();
      }
      if (end.size() == 0 && start.size() > 1) plus++;
      if (end.size() == 1 && start.size() > 1 && del) darab++;
      pre = start.top().first;
      while (start.size() > 0 && start.top().first == pre) {
        end.push({start.top().second, darab});
        start.pop();
      }
    }
    while (end.size() > 0) end.pop();
    if (plus == n) plus--;
    cout << sol + plus << "\n";
  }
}
