#include <bits/stdc++.h>
using namespace std;
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
}
const int INF = 2e9;
void solve() {
  int n;
  cin >> n;
  vector<pair<int, int> > v1(n);
  vector<int> dif(n);
  map<int, int> ls;
  for (auto& it : v1) cin >> it.first >> it.second;
  sort(v1.begin(), v1.end());
  int l = INF, r = -INF, count = 0;
  for (int i = 0; i < n; i++) {
    if (v1[i].first > r) {
      if (r != -INF) ls[l] = 0;
      count++;
      l = v1[i].first, r = v1[i].second;
    } else
      r = max(r, v1[i].second);
  }
  ls[l] = 0;
  set<pair<int, int> > st;
  for (int i = 0; i < n; i++) {
    while (!st.empty() && st.begin()->first < v1[i].first) st.erase(st.begin());
    int j = i, cnt = 0;
    while (j < n && v1[j].first == v1[i].first) j++, cnt++;
    if (cnt > 1) {
      j = i;
      if ((int)(st.size()) == 1) {
        dif[st.begin()->second]++;
      }
      while (j < n && v1[j].first == v1[i].first) {
        st.insert({v1[j].second, j});
        j++;
      }
      i = j - 1;
      continue;
    }
    if ((int)(st.size()) == 1) {
      dif[st.begin()->second]++;
    }
    st.insert({v1[i].second, i});
  }
  for (int i = 0; i < n; i++)
    if (ls.count(v1[i].first)) ++ls[v1[i].first];
  for (int i = 0; i < n; i++)
    if (ls[v1[i].first] == 1) --dif[i];
  cout << count + *max_element(dif.begin(), dif.end()) << "\n";
}
int main() {
  file();
  fast();
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
