#pragma GCC optimize ("Ofast") #include using namespace std; template struct cLtraits_identity{ using type = T; } ; template using cLtraits_try_make_signed = typename conditional< is_integral::value, make_signed, cLtraits_identity >::type; template struct cLtraits_common_type{ using tS = typename cLtraits_try_make_signed::type; using tT = typename cLtraits_try_make_signed::type; using type = typename common_type::type; } ; template inline auto max_L(S a, T b) -> typename cLtraits_common_type::type{ return (typename cLtraits_common_type::type) a >= (typename cLtraits_common_type::type) b ? a : b; } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline int rd_int(void){ int x; rd(x); return x; } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } template inline T pow2_L(T a){ return a*a; } template inline S chmax(S &a, T b){ if(a m){ if(c1 > c2){ mn[i] = mx[i] =bes1; } else{ mn[i] = mx[i] =bes2; } } else{ long long Q5VJL1cS; long long e98WHCEY; long long cTE1_r3A; Q5VJL1cS = 0; e98WHCEY = 100000000000LL; while(Q5VJL1cS < e98WHCEY){ if((Q5VJL1cS + e98WHCEY)%2==0){ cTE1_r3A = (Q5VJL1cS + e98WHCEY) / 2; } else{ cTE1_r3A = (Q5VJL1cS + e98WHCEY + 1) / 2; } if(c1 * (2*cTE1_r3A-1) + c2 * (2*cTE1_r3A-3) <= m){ Q5VJL1cS = cTE1_r3A; } else{ e98WHCEY = cTE1_r3A - 1; } } x =e98WHCEY; mx[i] = bes1 + x; long long xr20shxY; long long WYIGIcGE; long long t_ynMSdg; xr20shxY = 0; WYIGIcGE = 100000000000LL; while(xr20shxY < WYIGIcGE){ if((xr20shxY + WYIGIcGE)%2==0){ t_ynMSdg = (xr20shxY + WYIGIcGE) / 2; } else{ t_ynMSdg = (xr20shxY + WYIGIcGE + 1) / 2; } if(c1 * (2*t_ynMSdg-1) + c2 * (2*t_ynMSdg+1) <= m){ xr20shxY = t_ynMSdg; } else{ WYIGIcGE = t_ynMSdg - 1; } } x =WYIGIcGE; mn[i] = bes1 - x; } } for(i=(0);i<(N);i++){ chmax(mn[i], 0); } } int main(){ int hCmBdyQB; long long res; long long m; long long ok; int V9aVTaxx = rd_int(); for(hCmBdyQB=(0);hCmBdyQB<(V9aVTaxx);hCmBdyQB++){ int i; rd(N); rd(M); rd(K); { int jZyWAPpY; for(jZyWAPpY=(0);jZyWAPpY<(M);jZyWAPpY++){ rd(X[jZyWAPpY]);X[jZyWAPpY] += (-1); rd(Y[jZyWAPpY]); } } for(i=(0);i<(N);i++){ cnt[i] = sm[i] = 0; } for(i=(0);i<(M);i++){ cnt[X[i]]++; sm[X[i]] += Y[i]; } long long BUotOFBp; long long Q5rsz4fz; long long GgkpftXM; BUotOFBp = 0; Q5rsz4fz = 10000000000LL; while(BUotOFBp < Q5rsz4fz){ if((BUotOFBp + Q5rsz4fz)%2==0){ GgkpftXM = (BUotOFBp + Q5rsz4fz) / 2; } else{ GgkpftXM = (BUotOFBp + Q5rsz4fz - 1) / 2; } ok = 1; chk(GgkpftXM); int Hjfu7Vx7; long long zT28qSmp; if(N==0){ zT28qSmp = 0; } else{ zT28qSmp = mn[0]; for(Hjfu7Vx7=(1);Hjfu7Vx7<(N);Hjfu7Vx7++){ zT28qSmp += mn[Hjfu7Vx7]; } } if(K ytthggxT){ ok = 0; } if(ok){ Q5rsz4fz = GgkpftXM; } else{ BUotOFBp = GgkpftXM + 1; } } m =Q5rsz4fz; res = 0; chk(max_L(0, m-1)); int OC5CYxKD; long long o3WxPXbE; if(N==0){ o3WxPXbE = 0; } else{ o3WxPXbE = mn[0]; for(OC5CYxKD=(1);OC5CYxKD<(N);OC5CYxKD++){ o3WxPXbE += mn[OC5CYxKD]; } } if(K ::type>::type mlGkBPoR; if(N==0){ mlGkBPoR = 0; } else{ mlGkBPoR = mn[0]; for(XNa8avth=(1);XNa8avth<(N);XNa8avth++){ mlGkBPoR += mn[XNa8avth]; } } res += m *max_L(0,mlGkBPoR- K); } else{ for(i=(0);i<(M);i++){ res +=(pow2_L((mx[X[i]] - Y[i]))); } int AAsEZMFe; cLtraits_try_make_signed::type>::type xtzQOlbs; if(N==0){ xtzQOlbs = 0; } else{ xtzQOlbs = mx[0]; for(AAsEZMFe=(1);AAsEZMFe<(N);AAsEZMFe++){ xtzQOlbs += mx[AAsEZMFe]; } } res += m *max_L(0, K -xtzQOlbs); } wt_L(res); wt_L('\n'); } return 0; } // cLay version 20210405-1 // --- original code --- // int N, M, K, X[2d5], Y[]; // ll cnt[], sm[], mn[], mx[]; // // void chk(ll m){ // ll bes1, bes2, c1, c2; // ll x; // rep(i,N){ // if(cnt[i] == 0) mn[i] = 0, mx[i] = K, continue; // bes1 = sm[i] / cnt[i]; // bes2 = bes1 + 1; // c2 = sm[i] % cnt[i]; // c1 = cnt[i] - c2; // if(abs(c1-c2) > m){ // mn[i] = mx[i] = if[c1 > c2, bes1, bes2]; // } else { // x = bsearch_max[ll,x,0,1d11](c1 * (2*x-1) + c2 * (2*x-3) <= m); // mx[i] = bes1 + x; // x = bsearch_max[ll,x,0,1d11](c1 * (2*x-1) + c2 * (2*x+1) <= m); // mn[i] = bes1 - x; // } // } // rep(i,N) mn[i] >?= 0; // } // // { // ll res, m, ok; // REP(rd_int()){ // rd(N,M,K,(X--,Y)(M)); // rep(i,N) cnt[i] = sm[i] = 0; // rep(i,M) cnt[X[i]]++, sm[X[i]] += Y[i]; // // m = bsearch_min[ll,m,0,1d10][ // ok = 1; // chk(m); // if(K < sum(mn(N))) ok = 0; // if(K > sum(mx(N))) ok = 0; // ](ok); // // res = 0; // chk(max(0,m-1)); // if(K < sum(mn(N))){ // rep(i,M) res += (mn[X[i]] - Y[i]) ** 2; // res += m * max(0, sum(mn(N))- K); // } else { // rep(i,M) res += (mx[X[i]] - Y[i]) ** 2; // res += m * max(0, K - sum(mx(N))); // } // // wt(res); // } // }