import sys
sys.stdin=open("00.txt","r")


n = int(input())

for _ in range(n):
    M,N,x,y= map(int,input().split())
    year = 1

    while year<N*M:
        if year % M == x and year % N == y:
            print(year)
            break
        year+=1
    else:
        print(-1)
        break


b1f44d9705bd9e4fa90522f85f4879693e8edd5a
<mask token>

<mask token>
for _ in range(n):
    M, N, x, y = map(int, input().split())
    year = 1
    while year < N * M:
        if year % M == x and year % N == y:
            print(year)
            break
        year += 1
    else:
        print(-1)
        break

<mask token>
sys.stdin = open('00.txt', 'r')
n = int(input())
for _ in range(n):
    M, N, x, y = map(int, input().split())
    year = 1
    while year < N * M:
        if year % M == x and year % N == y:
            print(year)
            break
        year += 1
    else:
        print(-1)
        break

import sys
sys.stdin = open('00.txt', 'r')
n = int(input())
for _ in range(n):
    M, N, x, y = map(int, input().split())
    year = 1
    while year < N * M:
        if year % M == x and year % N == y:
            print(year)
            break
        year += 1
    else:
        print(-1)
        break

import sys
sys.stdin=open("00.txt","r")


n = int(input())

for _ in range(n):
    M,N,x,y= map(int,input().split())
    year = 1

    while year<N*M:
        if year % M == x and year % N == y:
            print(year)
            break
        year+=1
    else:
        print(-1)
        break

