#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
string num2bit(long long num, long long len) {
  string bit = "";
  for (int i = 0; i < (int)len; ++i) {
    bit += char('0' + (num >> i & 1));
  }
  return bit;
}
void solve() {
  long long n;
  cin >> n;
  vector<long long> l(n), r(n), cl(n), cr(n);
  vector<long long> cand;
  for (int i = 0; i < (int)n; ++i) {
    cin >> l[i] >> r[i];
    r[i]++;
    cand.push_back(l[i]);
    cand.push_back(r[i]);
  }
  sort(cand.begin(), cand.end());
  cand.erase(unique(cand.begin(), cand.end()), cand.end());
  for (int i = 0; i < (int)n; ++i) {
    long long lpos = lower_bound(cand.begin(), cand.end(), l[i]) - cand.begin();
    cl[i] = 2 * lpos;
    long long rpos = lower_bound(cand.begin(), cand.end(), r[i]) - cand.begin();
    cr[i] = 2 * rpos - 1;
  }
  vector<long long> imos(5 * n + 1, 0), ip(5 * n + 1, 0), sip(5 * n + 2, 0);
  for (int i = 0; i < (int)n; ++i) {
    imos[cl[i]]++;
    imos[cr[i]]--;
  }
  for (int i = 0; i < (int)5 * n; ++i) imos[i + 1] += imos[i];
  long long somo = 0;
  for (int i = 0; i < (int)5 * n; ++i)
    if (imos[i] > 0 && imos[i + 1] == 0) somo++;
  for (int i = 0; i < (int)5 * n; ++i) {
    if (imos[i + 1] == 1 && imos[i] > 1) ip[i + 1] = 1;
  }
  for (int i = 0; i < (int)5 * n + 1; ++i) sip[i + 1] = sip[i] + ip[i];
  long long res = -1;
  for (int i = 0; i < (int)n; ++i) {
    long long left = cl[i];
    long long right = cr[i];
    long long val = sip[right] - sip[left];
    if (left > 0 && imos[left] == 1 && imos[left - 1] == 1) val++;
    if (imos[right] == 0 && imos[right - 1] == 1) val--;
    chmax(res, somo + val);
  }
  cout << res << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
