#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int T, x, y, a, b;
    cin >> T;
    while (T--)
    {
        cin >> x >> y >> a >> b;
        if ((x == 1 && a >= 1) || (y == 1 && b >= 1) || ((x == 2 && y == 2 && a + b >= 3)))
            puts("INF");
        else
        {
            int cnt = 0;
            for (int c1 = a, c2 = b, tmp; c1 >= x || c2 >= y; c1 = c1 % x + tmp, c2 = c2 % y + tmp)
                cnt += (tmp = c1 / x + c2 / y);
            printf("%d\n", cnt);
        }
    }
    return 0;
}
