#include #include using namespace std; using lint = long long; constexpr lint INF = 1LL << 60; void solve() { int n, m; lint k; cin >> n >> m >> k; vector ds(n, 0), first(n, 0); lint ans = 0; while (m--) { int x; lint y; cin >> x >> y; --x; ds[x] += 2; first[x] += y * 2 - 1; ans += y * y; } lint ok = -1, ng = INF; while (ng - ok > 1) { lint mid = (ok + ng) / 2; lint cnt = 0; for (int i = 0; i < n && cnt <= k; ++i) { if (first[i] < mid) continue; if (ds[i] == 0) { cnt = k + 1; } else { cnt += (first[i] - mid) / ds[i] + 1; } } if (cnt >= k) { ok = mid; } else { ng = mid; } } for (int i = 0; i < n; ++i) { if (first[i] < ng) continue; lint c = (first[i] - ng) / ds[i] + 1; k -= c; ans -= first[i] * c - c * (c - 1) / 2 * ds[i]; } ans -= k * ok; cout << ans << "\n"; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int q; cin >> q; while (q--) solve(); return 0; }