https://www.acmicpc.net/problem/6064
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
int k=s.nextInt();
for(int i=0;i<k;i++){
int m=s.nextInt();
int n=s.nextInt();
int x=s.nextInt();
int y=s.nextInt();
int num=x;
while(true){
if(num%n==y){
System.out.println(num);
break;
}
if(n==y && num%n==0){
System.out.println(num);
break;
}
if(num>m*n){
System.out.println("-1");
break;
}
num+=m;
}
}
}
}