#include <bits/stdc++.h>
using namespace std;

#define int long long

struct State {
    int x, y;
    int dx, dy;
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--) {
        int n, m, x, y;
        cin >> n >> m >> x >> y;

        vector<bool> row(n + 1, false);
        vector<bool> col(m + 1, false);

        int cntRow = 0;
        int cntCol = 0;

        State cur = {x, y, 1, 1};

        int time = 0;

        while (true) {

            // lau hàng và cột hiện tại
            if (!row[cur.x]) {
                row[cur.x] = true;
                cntRow++;
            }

            if (!col[cur.y]) {
                col[cur.y] = true;
                cntCol++;
            }

            // đã lau hết
            if (cntRow == n || cntCol == m) {
                cout << time << '\n';
                break;
            }

            // phản xạ theo hàng
            if (cur.x + cur.dx < 1 || cur.x + cur.dx > n)
                cur.dx *= -1;

            // phản xạ theo cột
            if (cur.y + cur.dy < 1 || cur.y + cur.dy > m)
                cur.dy *= -1;

            // di chuyển
            cur.x += cur.dx;
            cur.y += cur.dy;

            time++;
        }
    }

    return 0;
}