#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(a) (a).begin(),(a).end()

template<typename T1, typename T2> inline void chkmin(T1 & x, const T2 & y) {if (y < x) x = y;}
template<typename T1, typename T2> inline void chkmax(T1 & x, const T2 & y) {if (x < y) x = y;}

void no() {
    cout << "No\n";
    exit(0);
}

const int N = 210;
int n;
int a[N], b[N];

void read() {
    cin >> n;
    set<int> setik;
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
        if (a[i] != -1 && setik.count(a[i])) no();
        if (b[i] != -1 && setik.count(b[i])) no();
        setik.insert(a[i]);
        setik.insert(b[i]);
    }
}

bool used[N];

bool check(int l, int r) {
    //l = 2 * l + 1;
    //r = 2 * r;
    //cout << "l = " << l << " r = " << r << endl;
    if (r <= l) return false;
    if ((r - l + 1) % 2) return false;
    fill(used, used + N, false);
    int c = (r - l + 1) / 2;
    //cout << "c = " << c << endl;
    for (int i = 0; i < n; ++i) {
        int x = a[i], y = b[i];
        //cout << "x = " << x << " y = " << y << endl;
        if (x == -1 && y == -1) continue;
        if (x != -1 && y != -1 && x >= y) return false;
        bool must = (x >= l && x <= r) || (y >= l && y <= r);
        if (x == -1) {
            x = y - c;
        } else if (y == -1) {
            y = x + c;
        }
        if (x > r || y < l) continue;
        if ((x < l || y > r) && must) return false;
        if ((x < l || y > r)) continue;
        if (y - x != c) return false;
        if (used[x] || used[y]) return false;
        //cout << "x = " << x << "y = " << y << endl;
        used[x] = true;
        used[y] = true;
    }
    return true;
}

bool dp[N];

void calcDp() {
    
    //cout << check(1, 4) << endl;
    //exit(0); 
    /*cout << check(1, 3) << endl;
    exit(0);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j <= n; ++j) {
            cout << check(i, j) << " ";
        }
        cout << endl;
    }*/
    //exit(0);
    dp[0] = 1;
    for (int i = 1; i <= 2 * n; ++i) {
        for (int j = 0; j < i; ++j) {
            dp[i] |= dp[j] && check(j + 1, i);
        }
    }
}

void run() {
    calcDp();
}

void write() {
    if (!dp[2 * n]) {
        no(); 
    }
    cout << "Yes\n";
}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(20), cout.setf(ios::fixed);
    read();
    run();
    write();
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
 
#define GO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define endl "\n"
const int maxr = 1e6 + 5;

int gcd(int a, int b, int& x, int& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g) {
    g = gcd(abs(a), abs(b), x0, y0);
    if (c % g) {
        return false;
    }

    x0 *= c / g;
    y0 *= c / g;
    if (a < 0) x0 = -x0;
    if (b < 0) y0 = -y0;
    return true;
}

int better_less(int n, int v, bool t) {
    int val = 0;
    if(t)
        val = ceil(abs(n) / (v*1.0));
    else
        val = -(n / v);

    n = n + val*v;
    return n;
}


int32_t main()
{
    GO;
    int t;
    cin >> t;
    while(t--) {
        int x, y, p, q;
        cin >> x >> y >> p >> q;
        int INF = 2e18;
        int ans = INF;
        for(int v = 0; v < y; v++) {
            for(int e = 0; e < q; e++) {

                int c = (p + e - x - v);
                int a = (2*x + 2*y);
                int b = -(p + q);
                int n, h, g;
                if(find_any_solution(a, b, c, n, h, g)) {
                    
                    /* some n and h such that we have time minimium*/ 
                    // cout << n << " " << h << endl;
                    // cout << "^^^^^^^^" << endl;
                    // cout << abs(b / g) << " " << abs(a / g) << endl;
                    // cout << "vvvvvvvv" << endl;
                    if(n < 0)
                        n = better_less(n, abs(b / g), 1);
                    else 
                        n = better_less(n, abs(b / g), 0);
                    if(h < 0)
                        h = better_less(h, abs(a / g), 1);
                    else
                        h = better_less(h, abs(a / g), 0);

                    // cout << n << " " << h << endl;

                    int time1 = (2*x + 2*y)*n + x + v;
                    int time2 = (p + q)*h + p + e;
                    ans = min(ans, min(time1, time2));
                }
            }
        }
        if(ans >= INF)
            cout << "infinity" << endl;
        else
            cout << ans << endl;
    }
    return 0;
}