#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
typedef long long ll;
template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const vector<T>&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const vector<T>&v){if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.first>>v.second;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.first<<","<<v.second;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>T dup(T x, T y){return (x+y-1)/y;}
template<typename T>ll suma(const vector<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}
int keta(ll n) { int ret = 0; while (n>0) { n/=10; ret++; } return ret; }

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) { cerr << head; dump(); }
template <typename Head, typename... Tail> void dump(Head &&head, Tail &&... tail) { cerr << head << ", "; dump(forward<Tail>(tail)...); }
#define debug(...) do { cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; dump(__VA_ARGS__); } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

const ll LINF = 1LL << 60;
const int INF = 1001001001;

/////////////////////////////////////////////////////////////////////

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

pair<ll, ll> crt(const vector<ll> &b, const vector<ll> &m)
{
  ll r = 0, M = 1;
  for (int i=0; i<int(b.size()); i++) {
    ll p, q;
    ll d = ext_gcd(M, m[i], p, q);
    if ((b[i] - r) % d != 0) return {0, -1};
    ll tmp = (b[i] - r) / d * p % (m[i]/d);
    r += M * tmp;
    M *= m[i]/d;
  }
  return {(r%M + M)%M, M};
}

void solve()
{
  ll x,y,p,q; cin>>x>>y>>p>>q;
  ll a = 2*x + 2*y;
  ll b = p + q;

  ll ans = LINF;
  rep(i, y) rep(j, q) {
    vector<ll> v = {x+i, p+j};
    vector<ll> u = {a, b};
    auto ret = crt(v, u);
    if (ret.second >= 0) mins(ans, ret.first);
  }
  if (ans == LINF) cout << "infinity" << "\n";
  else cout << ans << "\n";

}

int main()
{
  int t;
  // t = 1;
  cin>>t;
  while (t--) solve();

  return 0;
}

#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T> void chmax(T &a, const T b){ a = max(a, b); }
template<class T> void chmin(T &a, const T b){ a = min(a, b); }

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    string s;
    cin >> s;
    if (len(s) < 4) {
        sort(all(s));
        do {
            ll v = atoi(s.c_str());
            if (v % 8 == 0) {
                cout << "Yes" << endl;
                return 0;
            }
        }
        while(next_permutation(all(s)));
        cout << "No" << endl;
        return 0;
    }
    vector<ll> org(10, 0);
    rep(i, len(s)) org[s[i] - '0']++;
    rep(i, 1000) {
        if (i % 8 != 0) continue;
        string t = "";
        if (i < 10) t += "0";
        if (i < 100) t += "0";
        t += to_string(i);
        vector<ll> cnt(10, 0);
        rep(j, 3) cnt[t[j] - '0']++;
        bool f = true;
        rep(j, 10) {
            if (org[j] < cnt[j]) {
                f = false;
            }
        }
        if (f) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
    return 0;
}
