https://www.acmicpc.net/problem/6064
#include<stdio.h>
#include<string.h>
int main() {
int k;
scanf("%d",&k);
for(int i=0;i<k;i++){
int m,n,x,y;
scanf("%d %d %d %d",&m,&n,&x,&y);
int num=x;
while(1){
if(num%n==y){
printf("%d\n",num);
break;
}
if(n==y && num%n==0){
printf("%d\n",num);
break;
}
if(num>m*n){
printf("-1\n");
break;
}
num+=m;
}
}
}