#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<; using vi = vector; using vll = vector; void solve() { int T; cin >> T; while (T--) { int N, M, K; cin >> N >> M >> K; vll S(N), A(N), B(N); rep(i, M) { int x, y; cin >> x >> y; --x; S[x]++; A[x] += y; B[x] += (ll)y * y; } ll LIM = (ll)1e18; ll ng = LIM + 10, ok = -(ll)1e15; bool zero = find(all(S), 0ll) != S.end() ? true : false; if (zero)ng = 1; auto f = [&](ll z) { ll sm = 0, k = 0, just = 0; rep(i, N) { if (!S[i])continue; ll x = max((z + S[i] + 2 * A[i]) / (2 * S[i]), 0ll); k += x; sm += S[i] * x * x - 2 * A[i] * x + B[i]; ll w = 2 * S[i] * x - S[i] - 2 * A[i]; if (w == z) { just++; k--; sm -= w; } if (sm > LIM) { k = K + 1; break; } if (k > K)break; } if (k < K && just) { ll a = min(K - k, just); k += a; sm += a * z; } if (z == 0 && zero && k < K)k = K; return pair(sm, k); }; while (ng - ok > 1) { ll z = (ok + ng) / 2; ll sm, k; tie(sm, k) = f(z); (k <= K ? ok : ng) = z; } cout << f(ok).first << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }