#include <bits/stdc++.h>
using namespace std;

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

bool dfs(int a, int L, vector<vi>& g, vi& btoa, vi& A, vi& B) {
	if (A[a] != L) return 0;
	A[a] = -1;
	for (int b : g[a]) if (B[b] == L + 1) {
		B[b] = 0;
		if (btoa[b] == -1 || dfs(btoa[b], L + 1, g, btoa, A, B))
			return btoa[b] = a, 1;
	}
	return 0;
}

int hopcroftKarp(vector<vi>& g, vi& btoa) {
	int res = 0;
	vi A(g.size()), B(btoa.size()), cur, next;
	for (;;) {
		fill(all(A), 0);
		fill(all(B), 0);
		/// Find the starting nodes for BFS (i.e. layer 0).
		cur.clear();
		for (int a : btoa) if(a != -1) A[a] = -1;
		rep(a,0,sz(g)) if(A[a] == 0) cur.push_back(a);
		/// Find all layers using bfs.
		for (int lay = 1;; lay++) {
			bool islast = 0;
			next.clear();
			for (int a : cur) for (int b : g[a]) {
				if (btoa[b] == -1) {
					B[b] = lay;
					islast = 1;
				}
				else if (btoa[b] != a && !B[b]) {
					B[b] = lay;
					next.push_back(btoa[b]);
				}
			}
			if (islast) break;
			if (next.empty()) return res;
			for (int a : next) A[a] = lay;
			cur.swap(next);
		}
		/// Use DFS to scan for augmenting paths.
		rep(a,0,sz(g))
			res += dfs(a, 0, g, btoa, A, B);
	}
}

vi val, comp, z, cont;
int Time, ncomps;
template<class G, class F> int dfs(int j, G& g, F& f) {
	int low = val[j] = ++Time, x; z.push_back(j);
	for (auto e : g[j]) if (comp[e] < 0)
		low = min(low, val[e] ?: dfs(e,g,f));

	if (low == val[j]) {
		do {
			x = z.back(); z.pop_back();
			comp[x] = ncomps;
			cont.push_back(x);
		} while (x != j);
		f(cont); cont.clear();
		ncomps++;
	}
	return val[j] = low;
}
template<class G, class F> void scc(G& g, F f) {
	int n = sz(g);
	val.assign(n, 0); comp.assign(n, -1);
	Time = ncomps = 0;
	rep(i,0,n) if (comp[i] < 0) dfs(i, g, f);
}

void solve(){
	int N;
	cin >> N;
	vector<pair<int,int>> locs;
	vector<int> cc;
	for(int i = 0; i < N; i++){
		int x, y;
		cin >> x >> y;
		locs.push_back({x, y});
		cc.push_back(x);
		cc.push_back(y);
	}
	sort(cc.begin(), cc.end());
	set<int> sx, sy;
	for(auto& [x, y] : locs){
		x = lower_bound(cc.begin(), cc.end(), x) - cc.begin();
		y = lower_bound(cc.begin(), cc.end(), y) - cc.begin();
		sx.insert(x);
		sy.insert(y);
	}
	vector<vector<int> > match(cc.size());
	for(auto [x, y] : locs){
		match[x].push_back(y);
	}
	int L = (int)cc.size();
	vector<int> btoa(L, -1);
	int ans = hopcroftKarp(match, btoa);
	int nx = (int)sx.size();
	int ny = (int)sy.size();

	if(ans > 1){
		if(ans == nx){
			vector<vector<int> > graph(2*L);
			for(auto [x, y] : locs){
				graph[x].push_back(y+L);
			}
			for(int i = 0; i < L; i++){
				if(btoa[i] >= 0) graph[i+L].push_back(btoa[i]);
			}
			bool works = false;
			scc(graph, [&](vector<int> scc) -> void {
				set<int> scc_set(scc.begin(), scc.end());
				int cnt = 0;
				bool outgoing = false;
				for(int x : scc){
					if(sx.count(x)) cnt++;
					for(int y : graph[x]) if(!scc_set.count(y)) outgoing = true;
				}
				if(cnt > 0 && scc.size() == cnt * 2 && cnt != nx && outgoing == false){
					works = true;
				}
			});
			if(!works) ans++;
		} else if(ans == ny){
			vector<vector<int> > graph(2*L);
			for(auto [x, y] : locs){
				graph[y].push_back(x+L);
			}
			for(int i = 0; i < L; i++){
				if(btoa[i] >= 0) graph[btoa[i]+L].push_back(i);
			}
			bool works = false;
			scc(graph, [&](vector<int> scc) -> void {
				set<int> scc_set(scc.begin(), scc.end());
				int cnt = 0;
				bool outgoing = false;
				for(int x : scc){
					if(sy.count(x)) cnt++;
					for(int y : graph[x]) if(!scc_set.count(y)) outgoing = true;
				}
				if(cnt > 0 && scc.size() == cnt * 2 && cnt != nx && outgoing == false){
					works = true;
				}
			});
			if(!works) ans++;
		}
	}
	cout << ans << '\n';
}

int main(){
	int T;
	cin >> T;
	while(T--) solve();
}

