#pragma GCC optimize ("Ofast") #include using namespace std; 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 void rd(long long &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(int x){ int s=0; int m=0; char f[10]; 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'); } } 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'); } } inline void wt_L(const char c[]){ int i=0; for(i=0;c[i]!='\0';i++){ my_putchar_unlocked(c[i]); } } template inline void arrInsert(const int k, int &sz, S a[], const S aval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } a[k] = aval; } template inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } a[k] = aval; b[k] = bval; } template inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; } template inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } for(i=sz-1;i>k;i--){ d[i] = d[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; d[k] = dval; } template inline S chmin(S &a, T b){ if(a>b){ a=b; } return a; } template inline S chmax(S &a, T b){ if(a 1){ sa = s[sz-2] - s[sz-1]; nd = sa * n[sz-1] + (n[sz-1] + n[sz-2] - 1); if(r < nd){ break; } r -= sa * n[sz-1]; n[sz-2] += n[sz-1]; sz--; } r -= n[sz-1] - 1; if(r < 0){ return res; } sa = r / n[sz-1]; if(sz >= 2){ chmin(sa, s[sz-2] - s[sz-1] - 1); } res = s[sz-1] + sa; return res; } int main(){ int cTE1_r3A; int RZTsC2BF = rd_int(); for(cTE1_r3A=(0);cTE1_r3A<(RZTsC2BF);cTE1_r3A++){ long long res = -4611686016279904256LL; long long n[3]; long long s[3]; int sz; rd(N); rd(A); rd(W); rd(K); if(K < N){ if(A >= K-1){ res = (A - (K-1)) / K; wt_L(res); wt_L('\n'); continue; } if(W >= N-K+1){ wt_L(-1); wt_L('\n'); continue; } wt_L(":("); wt_L('\n'); continue; } sz = 0; if(N - W % N){ arrInsert(sz, sz, n, N - W % N, s, W / N); } if(W % N){ arrInsert(sz, sz, n, W % N, s, W / N + 1); } chmax(res, solve(sz, n, s)); if(W){ sz = 0; W--; if(N - W % N){ arrInsert(sz, sz, n, N - W % N, s, W / N); } if(W % N){ arrInsert(sz, sz, n, W % N, s, W / N + 1); } W++; n[sz-1]--; arrInsert(sz, sz, n, 1LL, s, s[sz-1]+1); chmax(res, solve(sz, n, s)); } if(res==-4611686016279904256LL){ wt_L(":("); wt_L('\n'); } else{ wt_L(res); wt_L('\n'); } } return 0; } // cLay version 20210321-1 [beta] // --- original code --- // ll N, A, W, K; // // ll solve(int sz, ll n[], ll s[]){ // ll res = -ll_inf, r = A, sa, nd; // // rep(i,sz) s[i] = -(s[i]*(s[i]+1)/2); // while(sz > 1){ // sa = s[sz-2] - s[sz-1]; // nd = sa * n[sz-1] + (n[sz-1] + n[sz-2] - 1); // if(r < nd) break; // r -= sa * n[sz-1]; // n[sz-2] += n[sz-1]; // sz--; // } // // r -= n[sz-1] - 1; // if(r < 0) return res; // sa = r / n[sz-1]; // if(sz >= 2) sa = K-1){ // res = (A - (K-1)) / K; // wt(res); // continue; // } // if(W >= N-K+1) wt(-1), continue; // wt(":("), continue; // } // // sz = 0; // if(N - W % N) arrInsert(sz, sz, n, N - W % N, s, W / N); // if(W % N) arrInsert(sz, sz, n, W % N, s, W / N + 1); // res >?= solve(sz, n, s); // // if(W){ // sz = 0; // W--; // if(N - W % N) arrInsert(sz, sz, n, N - W % N, s, W / N); // if(W % N) arrInsert(sz, sz, n, W % N, s, W / N + 1); // W++; // n[sz-1]--; // arrInsert(sz, sz, n, 1LL, s, s[sz-1]+1); // res >?= solve(sz, n, s); // } // // wt(if[res==-ll_inf, ":(", res]); // } // }