Problem Solving/BOJ

[Silver 1] 6064번 카잉 달력

kmkunk 2022. 4. 1. 14:26
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        
        while(t-- > 0) {
            String[] arr = br.readLine().split(" ");
            int m = Integer.parseInt(arr[0]); int n = Integer.parseInt(arr[1]);
            int x = Integer.parseInt(arr[2]); int y = Integer.parseInt(arr[3]);
            int temp = 0;
            boolean noAnswer = true;
            
            for(int i=x; i<=m*n; i+=m) {
                temp = (i-1)%n+1;
                if(temp==y) {
                    System.out.println(i);
                    noAnswer = false;
                    break;
                }
            }
            
            if(noAnswer) { System.out.println(-1); }
        }
    }
}