#include <iomanip>
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <set>
#include "queue"
//#include <multiset>
#include <map>
#include <utility>
#include <numeric>
#include "random"
#ifndef __clang__
#include <bits/stdc++.h>
#endif
using namespace std;

#define all(x) (x).begin(), (x).end()

const int mod = 999999001;

int solve(vector<pair<int, int>> beb) {
    int n=beb.size(), A, B;
    vector<int> a(n * 2 + 3, 1e6);
    vector<int> b = a;
    for (int i = 0; i < n+1; ++i) {
        a[i] = 0;
    }
    for (int i = n+1; i < a.size(); ++i) {
        a[i] = i-n-1;
    }
    //cerr << a[n+1] << endl;
    map<int, vector<int>> elba;
    for (auto i : beb) {
        elba[i.first].push_back(i.second);
    }
    for (auto i : elba) {
        vector<int> c = i.second;
        sort(all(c));
        vector<int> stk = {0};
        int ind = 0;
        for (auto j : c) {
            ind++;
            //cerr << j << ' ' << 2 * ind - 1 << endl;
            if (j-(2*ind -1)<=0)
                stk.push_back(max(stk.back(), j-(2*ind -1)));
            else
                stk.push_back(max(stk.back(), (j-(2*ind -1)+1)/2));
        }
        //cout << stk.back() << '\n';
        for (int j = 0; j < b.size(); ++j) {
            b[j] = 1e6;
        }
        for (int j = 0; j < stk.size(); ++j) {
            int bd1 = c.size() - j;
            int bd2 = stk[j];
            int crd = bd2 - bd1;
            //cerr << i.first << ' ' << bd2 << ' ' << bd1 << ' ' << crd << ' ' << min(bd1, bd2) << endl;
            for (int k = 0; k < a.size(); ++k) {
                if (k + crd >= 0 and k + crd < a.size()) {
                    b[k+crd] = min(b[k+crd], a[k]+bd2);
                }
                if (k + crd < 0) {
                    int c1 = -(k+crd);
                    b[0] = min(b[0], a[k] + c1 + bd2);
                }
                if (k + crd >= a.size()) {
                    int t = a.size() - 1;
                    b[t] = min(b[t], a[k] + bd2);
                }
            }
        }
        swap(a, b);
    }
    return a[n+1];
}
int stp(vector<pair<int, int>> beba) {
    vector<vector<int>> dubi;
    map<int, vector<int>> dd;
    for (auto i : beba) {
        dd[i.first].push_back(i.second);
    }
    for (auto i : dd)
        dubi.push_back(i.second);
    vector<int> cnts(dubi.size());
    int res = 1e6;
    while (1) {
        int c1 = 0;
        for (int i = 0; i < cnts.size(); ++i) {
            c1 += cnts[i];
        }
        int c2 = 0;
        for (int i = 0; i < cnts.size(); ++i) {
            int sz = dubi[i].size() - cnts[i];
            int ndad = 0;
            for (int j = 0; j < sz; ++j) {
                ndad = max(ndad, (dubi[i][j]-(j * 2+1)+1)/2);
            }
            c2 += ndad;
        }
        //for (auto i : cnts)
        //    cerr << i << ' ';
        //cerr << c1 << ' ' << c2 << endl;
        res = min(res, max(c1, c2));
        bool ok = 0;
        for (int i = 0; i < cnts.size(); ++i) {
            cnts[i]++;
            if (cnts[i] <= dubi[i].size()) {
                ok = 1;
                break;
            } else {
                cnts[i] = 0;
            }
        }
        if (ok == 0)
            break;
    }
    return res;
}
void gen() {
    int n = rand() % 10 + 2;
    vector<pair<int, int>> a(n);
    for (int i = 0; i < n; ++i) {
        a[i].first = rand() % 4 + 1;
        a[i].second = rand() % 10 + 1;
    }
    if (stp(a) != solve(a)) {
        cout << a.size() << '\n';
        for (auto i : a)
            cout << i.first << ' ' << i.second << '\n';
        cout << "\n\n";
        cout << stp(a) << ' ' << solve(a) << endl;
        exit(1);
    }
}
/*
1
5 1 1
1 1 1 2 1 3 1 4 1 5

 */
signed main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int t = 0;
    /*while (1) {
        gen();
        t++;
        if (t % 1000 == 0)
            cout << t << endl;
    }*/
    cin >> t;
    while (t--) {
        int n, a, b;
        cin >> n >> a >> b;
        vector<pair<int, int>> r(n);
        for (int i = 0; i < n; ++i) {
            cin >> r[i].first >> r[i].second;
        }
        cout << solve(r) << '\n';
    }
}
