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

using bs = bitset<5010>;

void solve(){
	int N, A, B;
	cin >> N >> A >> B;
	map<int, vector<int> > suits;
	for(int i = 0; i < N; i++){
		int x, y;
		cin >> x >> y;
		x--; y--;
		suits[x].push_back(y);
	}
	vector<bs> dp(N+1);
	dp[0][0] = 1;
	for(auto [_, v] : suits){
		vector<bs> ndp(N+1);
		sort(v.begin(), v.end());
		int add_cost = 0;
		for(int i = 0; i <= v.size(); i++){
			if(i){
				add_cost = max(add_cost, (v[i-1] - 2 * (i-1) + 1) / 2);
			}
			int remove = (int)v.size() - i;
			if(add_cost > N || remove > N) continue;
			for(int a = 0; a + add_cost <= N; a++){
				ndp[a + add_cost] |= dp[a] << remove;
			}
		}
		dp = ndp;
	}
	int ans = N;
	for(int a = 0; a <= N; a++){
		for(int b = 0; b <= N; b++){
			if(dp[a][b]) ans = min(ans, max(a, b));
		}
	}
	cout << ans << '\n';
}

int main(){
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	int T;
	cin >> T;
	while(T--) solve();
}
