#include <bits/stdc++.h>

using namespace std;

int main() {

    int t;
    cin >> t;

    while(t--) {
        int n;
        cin >> n;
        vector<pair<int,int> > v;
        for (int i = 0; i < n; ++i) {
            int x,y;
            cin >> x >> y;
            v.push_back({x,y});
        }

        sort(v.begin(), v.end());

        priority_queue<int> q;
        int mx = 0;

        for (int i = 0; i < n; ++i) {
            while(!q.empty() and -q.top() < v[i].first) q.pop();
            q.push(-v[i].second);
            int pos = (int)(upper_bound(v.begin(), v.end(), make_pair(v[i].second, 1000000000)) - v.begin() - 1);
            int cur = max(0, pos - i);
            //cout << v[i].first << ": " << pos << endl;
            mx = max(mx, (int)q.size() + cur);
        }

        cout << n - mx << "\n";
    }
    return 0;
}