#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// BEGIN NO SAD
#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()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(1) cerr

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template<class T>
bool updmin(T& a, T b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}
template<class T>
bool updmax(T& a, T b) {
  if(b > a) {
    a = b;
    return true;
  }
  return false;
}
typedef int64_t ll;

// I forget how this code works... does it even work?
void gen(vector<pair<int, vector<ll>>>& v, const vector<ll>& dp) {
  vector<ll> diffs;
  for(int i = 1; i < dp.size(); i++) diffs.push_back(dp[i] - dp[i-1]);
  ll p = -1e18;
  int lhs = 0;
  vector<ll> curr;
  for(int i = 0; i < diffs.size(); i++) {
    if(diffs[i] < p) {
      v.emplace_back(lhs, curr);
      lhs += curr.size();
      curr.clear();
      curr.push_back(dp[i+1]);
      p = dp[i+1];
    }
    else {
      // normal case
      curr.push_back(diffs[i]);
      p = diffs[i];
    }
  }
  v.emplace_back(lhs, curr);
}

void upd(vector<ll>& dp, int base, const vector<ll>& a, const vector<ll>& b) {
  int ai = 0;
  int bi = 0;
  ll curr = dp[base];
  for(int i = base+1; ai < a.size() || bi < b.size(); i++) {
    if(ai == a.size() || (bi < b.size() && b[bi] < a[ai])) {
      curr += b[bi++];
    }
    else {
      curr += a[ai++];
    }
    dp[i] = min(dp[i], curr);
  }
}

void combine(vector<ll>& dp, const vector<ll>& adp, const vector<ll>& bdp) {
  vector<pair<int, vector<ll>>> a, b;
  gen(a, adp);
  gen(b, bdp);
  /*
  debug(adp);
  for(auto& x: a) {
    debug(x.f, x.s);
  }
  cerr << endl;
  debug(bdp);
  for(auto& x: b) {
    debug(x.f, x.s);
  }
  */
  dp.assign(adp.size() + bdp.size() - 1, 1e18);
  dp[0] = adp[0] + bdp[0];
  for(auto& aa: a) for(auto& bb: b) upd(dp, aa.first + bb.first, aa.second, bb.second);
}

void rsolve() {
  int n, nsuit, nval;
  cin >> n >> nsuit >> nval;
  vector<array<int, 2>> v(n);
  for(auto& x: v) cin >> x[0] >> x[1];
  sort(all(v));
  vector<ll> dp(1, 0); // dp[i] is minimum to add if we remove exactly i
  dp[0] = 0; 
  auto analyze = [&](const vector<int>& v) -> void {
    vector<ll> inc(sz(v)+1);
    for(int rem = 0; rem <= sz(v); rem++) {
      int cand = 0;
      for(int i = 0; i < sz(v) - rem; i++) {
        // want min add such that 2*(i+add)+1 >= v[i]
        // add >= (v[i]-1-2*i)/2
        updmax(cand, (v[i]-2*i)/2);
      }
      inc[rem] = cand;
    }
    //debug(dp, v, inc);
    vector<ll> ndp;
    combine(ndp, dp, inc);
    swap(dp, ndp);
    //debug(dp);
    //cerr << endl;
    for(int i = 1; i < sz(dp); i++) assert(dp[i] <= dp[i-1]);
  };
  ll ret = n;
  for(int i = 0; i < n;) {
    int j = i+1;
    while(j < n && v[i][0] == v[j][0]) j++;      
    vector<int> vals;
    for(int k = i; k < j; k++) vals.pb(v[k][1]);
    i = j;
    analyze(vals);
  }
  assert(sz(dp) == n+1);
  for(int i = 0; i < sz(dp); i++) {
    updmin(ret, max(ll(i), dp[i]));
  }
  cout << ret << "\n";
}
void solve() {
  int t;
  cin >> t;
  while(t--) rsolve();
}

// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should

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