int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin>>t;
while(t–){
long long int D,d,p,q;
cin>>D>>d>>p>>q;
long long di = D/d;
long long rem = D%d;
long long sum=0;
sum = (di*(2p+(di-1)q)(d))/2;
sum += rem(p+q*di);
cout<<sum<<endl;
}
return 0;
}

Your algorithm has a time complexity of O(n^2) I think, it’s hard to read the code. And since this problem can be solved with O(1) complexity, the time limit has been set very low.