#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; using namespace atcoder; typedef long long ll; typedef pair P; int main() { int t; cin>>t; while(t--){ int n, m; ll k; cin>>n>>m>>k; vector c(n), s(n); ll ans=0; for(int i=0; i>x>>y; x--; c[x]++; s[x]+=y; ans+=y*y; } ll l=-2e18, r=1e15; while(r-l>1){ ll m=(l+r)/2; ll cnt=0; for(int i=0; i=0) cnt=1e18; }else{ if(m+2*s[i]+c[i]>0) cnt+=(m+2*s[i]+c[i])/(2*c[i]); } } if(cnt>=k) r=m; else l=m; } ll cnt1=0; for(int i=0; i0){ ll x=(r-1+2*s[i]+c[i])/(2*c[i]); cnt1+=x; ans+=x*x*c[i]-2*s[i]*x; } } ans+=(k-cnt1)*r; cout<