#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int t, n, segcnt;
struct segment {
  int start, end;
  int cnt;
  bool candidate;
  bool isocand;
  bool iso;
} segs[200010];
struct point {
  int x;
  int segid;
  bool endpoint;
  point(int x, int id) : x(x), segid(id) {}
};
vector<point> pt;
bool operator<(const point& p, const point& q) {
  if (p.x != q.x) return p.x < q.x;
  if (!p.endpoint && q.endpoint) return true;
  if (p.endpoint && !q.endpoint) return false;
  return p.segid < q.segid;
  ;
}
void solve() {
  cin >> n;
  pt.clear();
  segcnt = 0;
  for (int i = 0; i < n; ++i) {
    cin >> segs[i].start >> segs[i].end;
    segs[i].cnt = 0;
    segs[i].candidate = false, segs[i].isocand = segs[i].iso = false;
    pt.push_back(point(segs[i].start, i));
    pt.back().endpoint = false;
    pt.push_back(point(segs[i].end, i));
    pt.back().endpoint = true;
  }
  sort(pt.begin(), pt.end());
  set<int> open;
  for (int i = 0; i < (int)pt.size();) {
    if (open.size() == 1 && segs[*open.begin()].candidate) {
      if (!pt[i].endpoint) segs[*open.begin()].cnt++;
      segs[*open.begin()].candidate = false;
    }
    if (open.size() == 1 && segs[*open.begin()].isocand) {
      if (pt[i].endpoint) segs[*open.begin()].iso = true;
      segs[*open.begin()].isocand = false;
    }
    int currx = pt[i].x;
    int stc = open.size();
    while (i < (int)pt.size() && pt[i].x == currx && !pt[i].endpoint) {
      open.insert(pt[i].segid);
      ++i;
    }
    int contain = open.size();
    int idd;
    if (contain == 1) idd = *open.begin();
    while (i < (int)pt.size() && pt[i].x == currx && pt[i].endpoint) {
      open.erase(pt[i].segid);
      ++i;
    }
    if (open.size() == 1 && contain > 1) segs[*open.begin()].candidate = true;
    if (open.size() == 1 && contain == 1 && stc == 0)
      segs[*open.begin()].isocand = true;
    if (open.size() == 0 && contain == 1 && stc == 0) segs[idd].iso = true;
    if (open.size() == 0) ++segcnt;
  }
  int maxc = 0;
  bool onlyiso = true;
  for (int i = 0; i < n; ++i) {
    maxc = max(maxc, segs[i].cnt);
    if (!segs[i].iso) onlyiso = false;
  }
  if (onlyiso)
    cout << segcnt - 1 << endl;
  else
    cout << segcnt + maxc << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> t;
  while (t--) solve();
  return 0;
}
