#include #include #include #include using namespace std;using i128=__int128_t; int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin>>T;while(T--){long long n,k,x,y;cin>>n>>k>>x>>y;vectorpx(n+1),py(n+1);for(int i=0;i>a>>b;px[i+1]=px[i]+a;py[i+1]=py[i]+b;}const i128 INF=(i128(1)<<120);i128 best=INF;for(int r=0;r0){if(R<0)low=1,high=0;else high=min(high,R/C);}else if(R<0){i128 need=(-R+(-C)-1)/(-C);low=max(low,need);}}if(low<=high)best=min(best,low*n+r);}if(best==INF)cout<<-1<<'\n';else cout<<(long long)best<<'\n';}}