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

int main() {
    int n, t;
    cin >> n;
    vector<pair<double, double>> v;
    double l, r;
    for (int i = 0; i < n; ++i) {
        cin >> t >> l >> r;
        if (t == 2) {
            r -= 0.1;
        } else if (t == 3) {
            l += 0.1;
        } else if (t == 4) {
            l += 0.1;
            r -= 0.1;
        }
        v.push_back(make_pair(l, r));
    }

    int ans = 0;
    double l1, r1, l2, r2;
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            tie(l1, r1) = v.at(i);
            tie(l2, r2) = v.at(j);
            if (r1 < l2 || r2 < l1) continue;
            ans++;
        }
    }

    cout << ans << endl;
}
#include <bits/stdc++.h>

long long extGCD(long long a, long long b, long long c, long long &x, long long &y){
    long long a_gcd = a;
    long long b_gcd = b;
    std::vector< long long > A, B;
    A.push_back(a_gcd);
    B.push_back(b_gcd);
    
    while(b_gcd != 0){
        long long tmp = a_gcd % b_gcd;
        a_gcd = b_gcd;
        b_gcd = tmp;
        A.push_back(a_gcd);
        B.push_back(b_gcd);
    }
    
    if(c % a_gcd != 0){
        x = -1e18;
        y = -1e18;
        return -1;
    }
    
    std::vector< long long > X(A.size()), Y(A.size());
    X[A.size() - 1] = c / a_gcd;
    Y[A.size() - 1] = 0;
    for(int i=A.size()-2; i>=0; i--){
        X[i] = Y[i+1];
        Y[i] = X[i+1] - (A[i] / B[i]) * X[i];
    }
    
    x = X[0];
    y = Y[0];
    
    b /= a_gcd;
    
    if(x >= 0){
        return x % b;
    }else{
        long long ret;
        ret = b - std::abs(x) % b;
        if(ret == b){
            ret = 0;
        }
        return ret;
    }
    
}

int main(){
    int T;
    std::cin >> T;
    std::vector< long long > ans(T);
    
    for(int t=0; t<T; t++){
        long long X, Y, P, Q;
        std::cin >> X >> Y >> P >> Q;
        
        long long min = 9223372036854775807;
        for(int p=P; p<P+Q; p++){
            for(int x=X; x<X+Y; x++){
                long long ret1, ret2;
                long long tmp = extGCD(P+Q, 2*(X+Y), x-p, ret1, ret2);
                if(tmp != -1){
                    min = std::min(min, p + (P+Q) * tmp);
                }
            }
        }
        
        ans[t] = min;
        
    }
    
    for(int i=0; i<T; i++){
        if(ans[i] == 9223372036854775807){
            std::cout << "infinity" << std::endl;
        }else{
            std::cout << ans[i] << std::endl;
        }
    }
    
    return 0;
    
}
