#include "bits/stdc++.h"

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

struct Dinic {
	struct Edge {
		int to, rev;
		ll c, oc;
		ll flow() { return max(oc - c, 0LL); } // if you need flows
	};
	vi lvl, ptr, q;
	vector<vector<Edge>> adj;
	Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {}
	void addEdge(int a, int b, ll c, ll rcap = 0) {
		adj[a].push_back({b, sz(adj[b]), c, c});
		adj[b].push_back({a, sz(adj[a]) - 1, rcap, rcap});
	}
	ll dfs(int v, int t, ll f) {
		if (v == t || !f) return f;
		for (int& i = ptr[v]; i < sz(adj[v]); i++) {
			Edge& e = adj[v][i];
			if (lvl[e.to] == lvl[v] + 1)
				if (ll p = dfs(e.to, t, min(f, e.c))) {
					e.c -= p, adj[e.to][e.rev].c += p;
					return p;
				}
		}
		return 0;
	}
	ll calc(int s, int t) {
		ll flow = 0; q[0] = s;
		rep(L,0,31) do { // 'int L=30' maybe faster for random data
			lvl = ptr = vi(sz(q));
			int qi = 0, qe = lvl[s] = 1;
			while (qi < qe && !lvl[t]) {
				int v = q[qi++];
				for (Edge e : adj[v])
					if (!lvl[e.to] && e.c >> (30 - L))
						q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
			}
			while (ll p = dfs(s, t, LLONG_MAX)) flow += p;
		} while (lvl[t]);
		return flow;
	}
	bool leftOfMinCut(int a) { return lvl[a] != 0; }
};

int reachable(vector<pair<int,int>> e, vector<int> st) {

	int n = e.size();
	vector<vector<int>> g(n);
	for (auto [x, y] : e) {
		g[x].push_back(y);
	}

	vector<int> vis(n);
	int seen = 0;

	queue<int> cur;
	for (int x : st) {
		cur.push(x);
		seen += (!vis[x]);
		vis[x] = 1;
	}

	while (cur.size()) {
		int c = cur.front();
		cur.pop();
		for (int i : g[c]) {
			if (vis[i]) continue;
			seen++;
			vis[i] = 1;
			cur.push(i);
		}
	}

	return seen;

}

int solve(int n, vector<pair<int,int>> a) {
	vector<int> vx, vy;
	for (auto [x, y] : a) {
		vx.push_back(x);
		vy.push_back(y);
	}

	int xok = 1, yok = 1;
	for (int i = 1; i < n; i++) {
		xok &= (a[i].first == a[i - 1].first);
		yok &= (a[i].second == a[i - 1].second);
	}

	if (xok || yok) {
		return 1;
	}

	sort(all(vx));
	sort(all(vy));
	vx.resize(unique(all(vx)) - vx.begin());
	vy.resize(unique(all(vy)) - vy.begin());

	if (vx.size() > vy.size()) {
		swap(vx, vy);
		for (auto &[x, y] : a) swap(x, y);
	}

	for (auto &[x, y] : a) {
		x = lower_bound(all(vx), x) - vx.begin();
		y = lower_bound(all(vy), y) - vy.begin();
	}

	Dinic helper(2 + vx.size() + vy.size());
	int src = 0, snk = 1 + vx.size() + vy.size();

	for (int i = 0; i < vx.size(); i++)
		helper.addEdge(src, 1 + i, 1);
	for (int i = 0; i < vy.size(); i++)
		helper.addEdge(1 + vx.size() + i, snk, 1);
	for (auto [x, y] : a) 
		helper.addEdge(1 + x, 1 + vx.size() + y, 1);

	int flow = helper.calc(src, snk);

	if (flow == vx.size()) {

		vector<int> toy(vx.size()), tox(vy.size(), -1);
		for (int i = 0; i < vx.size(); i++) {

			for (auto e : helper.adj[1 + i]) {
				if (e.to >= 1 + vx.size() && e.flow()) {
					toy[i] = e.to - 1 - vx.size();
					tox[e.to - 1 - vx.size()] = i;
				}
			}

		}

		vector<pair<int,int>> fw, bw;
		for (auto [x, y] : a) {
			if (tox[y] != -1) {
				fw.push_back({x, tox[y]});
				bw.push_back({tox[y], x});
			}
		}

		if (vy.size() > vx.size()) {

			vector<int> bad;
			for (auto [x, y] : a) {
				if (tox[y] == -1) {
					bad.push_back(x);
				}
			}

			flow += (reachable(bw, bad) == vx.size());

		}
		else {
			vector<int> st = {0};

			flow += (reachable(fw, st) == vx.size() &&
							 reachable(bw, st) == vx.size());
		}

	}

	return flow;

}

/*
int brute(int n, vector<pair<int,int>> a) {

	vector<int> xs, ys;
	for (auto [x, y] : a) {
		xs.push_back(x);
		ys.push_back(y);
	}

	sort(all(xs));
	sort(all(ys));
	xs.resize(unique(all(xs)) - xs.begin());
	ys.resize(unique(all(ys)) - ys.begin());

	if (xs.size() == 1 || ys.size() == 1) return 1;

	for (auto &[x, y] : a) {
		x = lower_bound(all(xs), x) - xs.begin();
		y = lower_bound(all(ys), y) - ys.begin();
	}

	int ans = 2 * n;
	for (int i = 1; i < (1 << xs.size()); i++) {

		int j = 0;
		for (auto [x, y] : a) {
			if (i & (1 << x)) continue;
			j = j | (1 << y);
		}
		if (!j) j = 1;

		ans = min(ans, __builtin_popcount(i) + __builtin_popcount(j));

	}

	return ans;

}
*/

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

	int t; cin >> t;

	while (t--) {

		int n; cin >> n;

		vector<pair<int,int>> a(n);
		for (auto &[x, y] : a) {
			cin >> x >> y;
		}

		cout << solve(n, a) << "\n";

	}
  
}

