#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;
const int mod = 998244353;

#define int __int128
const int inf = (1LL << 60);
const int INF = inf * inf;
template< typename T > inline void get_min(T &x, T y) {if(y < x) x = y;}
template< typename T > inline void get_max(T &x, T y) {if(x < y) x = y;}
inline int read() {
  int x = 0, f = 0; char ch = getchar();
  while (!isdigit(ch)) f |= ch == '-', ch = getchar();
  while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
  return f ? -x : x;
}
template<typename T> inline void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

void extend_gcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return ;
  }
  extend_gcd(b, a % b, y, x);
  y -= a / b * x;
  y %= a;
}

void solve() {
  int x, y, p, q; 
  x = read(), y = read(), p = read(), q = read();
  int ans = INF;
  //print(ans);
  for (int i = x; i < x + y; i++) {
    for (int j = p; j < p + q; j++) {
      int a = 2 * x + 2 * y, b = -(p + q), l, r;
      int c = j - i;
      int d = __gcd(a, b);
      if (c % d) continue; // 无解
      extend_gcd(a, b, l, r);
      int e = a * b / d; //  最小公倍数
      if (e < 0) {
          e = -e;
      }
      l = c / d * l % e;
      r = c / d * r % e;
      int mem = (a * l + i) % e;
      if (mem < 0) mem += e;
      //cout << l << ' ' << r << '\n';
      ans = min(ans, mem);
    }
  }
  if (ans == INF) {
    cout << "infinity\n";
  } else {
    print(ans);
    puts("");
  }
}

signed main() {
  //ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int T = 1; T = read();
  while (T--) {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define debug(a) { cerr << #a << ':' << a << endl; }
#define endl '\n'
#define fi first
#define se second
using lint = long long;
using P = pair<int,int>;
template<class T> using V = vector<T>;
template<class T> using priq     = priority_queue<T>;
template<class T> using priq_inv = priority_queue<T, vector<T>, greater<T>>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
template<class T> T ceil(const T &a, const T &b) { return (a+b-1)/b; }
template<class T> bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; }
template<class T> bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; }
struct INF { template<class T> operator T() { return numeric_limits<T>::max() / 2; } } INF;

template<class T, class U> istream& operator>>(istream &in, pair<T,U> &p) {
    return in >> p.first >> p.second;
}
template<class T, class U> ostream& operator<<(ostream &out, const pair<T,U> &p) {
    return out << p.first << ' ' << p.second;
}
template<class T> istream& operator>>(istream &in, vector<T> &v) {
    for(auto &&e: v) in >> e;
    return in;
}
template<class T> ostream& operator<<(ostream &out, const vector<T> &v) {
    for(const auto &e: v) out << e << ' ';
    return out;
}

/*----------------------------------------------------------------------------------------------------*/

template<class T>                                               
vector<T> Dijkstra(int s, const vector<vector<pair<T,int>>> &g, vector<int> prev = {}) {

    using pi = pair<T,int>;
    const T inf = numeric_limits<T>::max()/2;
    vector<T> dist(g.size(), inf);
    dist[s] = 0;    

    prev.assign(dist.size(), -1);    

    priority_queue<pi, vector<pi>, greater<pi>> que;
    que.emplace(0,s);

    while(!que.empty()) {
        P p = que.top(); que.pop();
        int now = p.second;
        if(dist[now] < p.first) continue;
        for(P np : g[now]) {
            int next = np.second;
            if(dist[next] > dist[now] + np.first) {
                prev[next] = now;
                dist[next] = dist[now] + np.first;
                que.emplace(dist[next], next);
            }
        }
    }
    return dist;
}

int main() {
    int h, w;
    cin >> h >> w;
    V<V<int>> a(h,V<int>(w-1)), b(h-1,V<int>(w));
    cin >> a >> b;

    auto makeId = [&](int y, int x) {
        if(h > w) return x*h+y;
        else return y*w+x;
    };
    auto retPoint = [&](int id) {
        if(h > w) return make_pair(id/h, id%w);
        else return make_pair(id/w, id%h);
    };

    V<V<P>> g(h*w);
    rep(i,h)rep(j,w) {
        int id = makeId(i,j);
        if(j+1 < w) g[id].emplace_back(a[i][j], makeId(i,j+1));
        if(j > 0) g[id].emplace_back(a[i][j-1], makeId(i,j-1));
        if(i+1 < h) g[id].emplace_back(b[i][j], makeId(i+1,j));
        Rep(k,i) g[id].emplace_back(k+1, makeId(i-k,j));
    }

    auto dist = Dijkstra(makeId(0,0), g);
    int ans = dist[makeId(h-1,w-1)];
    cout << ans << endl;
}