//
// Created by watemus on 15.12.2020.
//

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define FF first
#define SS second

using ll = long long;
using ld = long double;

//#define int ll

template<typename T>
using vec = std::vector<T>;

template<typename T>
using uset = std::unordered_set<T>;

template<typename T1, typename T2>
using umap = std::unordered_map<T1, T2>;

constexpr ll INFL = 1000000000000000069;
constexpr int INFI = 1e9 + 10;
const ld PI = acos(-1);

#ifdef LOCAL
std::mt19937 rnd(69);
#else
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
#endif

vec<pair<int, int>> DD = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

#ifdef LOCAL
#else
#endif

const int T = 1e7;

int t[T], lt[T], rt[T], agn[T];
int cnt_nodes = 0;

int new_node() {
  int u = cnt_nodes++;
  t[u] = 0;
  lt[u] = rt[u] = -1;
  agn[u] = 0;
  return u;
}

inline void kk(int u) {
  if (lt[u] == -1) lt[u] = new_node();
  if (rt[u] == -1) rt[u] = new_node();
}

void modify(int v, int l, int r, int at, int x) {
  if (r - l == 1) {
    t[v] += x;
  } else {
    int mid = (l + r) / 2;
    if (at < mid) {
      if (lt[v] == -1) lt[v] = new_node();
      modify(lt[v], l, mid, at, x);
    } else {
      if (rt[v] == -1) rt[v] = new_node();
      modify(rt[v], mid, r, at, x);
    }
    t[v] = 0;
    if (lt[v] != -1) t[v] += t[lt[v]];
    if (rt[v] != -1) t[v] += t[rt[v]];
  }
}

int get(int v, int l, int r, int lq, int rq) {
  if (lq <= l && r <= rq) {
    return t[v];
  } else if (rq <= l || r <= lq || v == -1) {
    return 0;
  } else {
    int mid = (l + r) / 2;
    return get(lt[v], l, mid, lq, rq) + get(rt[v], mid, r, lq, rq);
  }
}

void print(int v, int l, int r) {
//  push(v, l, r);
  if (r - l == 1) {
    cerr << t[v] << ' ';
  } else {
    kk(v);
    print(lt[v], l, (l + r) / 2);
    print(rt[v], (l + r) / 2, r);
  }
}

void run() {
  int n;
  cin >> n;
  vec<pair<int, int>> segs(n);
  cnt_nodes = 0;
  int begs = new_node();
  int ends = new_node();
  for (auto &[l, r] : segs) {
    cin >> l >> r;
    modify(begs, 0, INFI, l, 1);
    modify(ends, 0, INFI, r, 1);
  }
//  print(begs, 0, INFI);
//  cerr << endl;
//  print(ends, 0, INFI);
//  cerr << endl;
  int ans = 1;
  for (auto &[l, r] : segs) {
//    cerr << get(begs, 0, INFI, 0, r + 1) << ' ' << get(ends, 0, INFI, 0, l) << endl;
    ans = max(ans, get(begs, 0, INFI, 0, r + 1) - get(ends, 0, INFI, 0, l));
  }
//  cerr << endl;
  cout << n - ans << '\n';
}

signed main() {
#ifdef LOCAL
  std::freopen("input.txt", "r", stdin);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif
  int t = 1;
   cin >> t;
  while (t--) {
    run();
  }
  return 0;
}


