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

using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvvll = vector<vector<vector<ll>>>;
using vpll = vector<pair<ll, ll>>;
#define rep(i,n) for (ll i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);
const int inf = 1<<30;
const ll INF = 1ll<<62;
//const ll mod = 1000000007;
const ll mod = 998244353;
//const ll dx[4] = {1, 0, -1, 0};
//const ll dy[4] = {0, 1, 0, -1};
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

template<class T> void disp1(vector<T> &array, bool blank=1) {
    rep(i, array.size()) cout << ((i&&blank) ? " " : "") << array[i];
    cout << endl;
    //cout << " ";
}
template<class T> void disp2(vector<vector<T>> &array, bool blank=1) {
    rep(i, array.size()) disp1(array[i], blank);
}


ll rem(ll a, ll b) { return (a % b + b) % b; }      // 余りを0~b-1に丸める
// 小数の入力が文字で与えられたとき、10^n倍したものを返す
ll x10(string str_f, ll n) {
    if (str_f[0] == '-') return -x10(str_f.substr(1), n);
    ll times = 1; rep(i,n) times *= 10;     // times = 10^n
    int id = -1; rep(i,str_f.size()) if (str_f[i] == '.') id = i;
    if (id == -1) { return stoll(str_f)*times; }
    string str_int = str_f.substr(0, id), str_dec = str_f.substr(id+1);
    while (str_dec.size() < n) str_dec += '0';
    return stoll(str_int)*times + stoll(str_dec);
}
// A^N (mod)
ll modpow(ll A, ll N, ll mod) {
    ll RES = 1;
    while (N) {
        if (N & 1) RES = RES * A % mod;
        A = A * A % mod;
        N >>= 1;
    }
    return RES;
}

long long gcd(long long A, long long B) {return B ? gcd(B, A%B) : A;}
long long lcm(long long A, long long B) {return (A / gcd(A, B)) * B;}




//using mint = modint998244353;

ll extgcd(ll a, ll b, ll &x, ll &y) {
    ll d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}


void solve() {
    ll X, Y, P, Q; cin >> X >> Y >> P >> Q;
    ll A = 2*(X+Y), B = P+Q;
    ll ans = INF, g = gcd(A, B), l = lcm(A, B);
    for (ll i = 0; i < Y; i++) {
        for (ll j = 0; j < Q; j++) {
            // Ax+X+i = By+P+j なる(x,y)を求める
            ll x, y;
            ll G = P+j-X-i;
            if (G%g != 0) continue;
            ll a = A/g, b = B/g, c = G/g;
            extgcd(a, b, x, y);
            ll re = c % b * rem(x, b) % b;
            re *= A;
            re += X+i;
            re = rem(re, l);
            chmin(ans, re);
        }
    }
    if (ans != INF) cout << ans << endl;
    else puts("infinity");
    return ;
}


int main() {
    int t; cin >> t;
    while (t--) solve();
    return 0;
}


#include "bits/stdc++.h"
 
using namespace std;
 
#define ll long long
 
#define       forn(i,n)              for(int i=0;i<n;i++)
#define          all(v)              v.begin(), v.end()
#define         rall(v)              v.rbegin(),v.rend()
 
#define            pb                push_back
#define          sz(a)               (int)a.size()
#define         fastio               ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define      GR(a,n,m)               vector<vector<int>> a(n, vector<int>(m, 0));
 
const int mod = 1e9 + 7;
const int N = 3005;
int dp[N][N];
ll a[N];

int cnt[N][N];
void solve()
{
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> a[i];
    }


    dp[0][0] = 1;

    ll sum = 0;

    map<pair<int,int>,int> m;
    for(int i = 1;i <= n;i++){
        sum += a[i];
        for(int j = 1; j <= n;j++){
            if(j == 1)dp[i][j]++;
            dp[i][j]+= cnt[((sum%j)+j)%j][j - 1];
            dp[i][j]%=mod;
        }

        for(int j = 1;j <= n;j++){
            cnt[((sum%(j+1))+(j+1))%(j+1)][j] += dp[i][j];
            cnt[((sum%(j+1))+(j+1))%(j+1)][j] %= mod;
        }
    }

    int ans = 0;
    for(int i = 1;i <= n;i++){
        ans += dp[n][i];
        ans%=mod;
    }
    cout << ans << "\n";
}
 
int32_t main()
{
    fastio;
    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
}