#include<bits/stdc++.h>
#define int long long 
#define ri register signed
#define rd(x) x=read()
using namespace std;
const int N=3e5+5;
const int maxn=5e6+5;
const int mod=998244353;
const int inf=0x3f3f3f3f3f3f3f3f;
inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
inline int ksm(int x,int y=mod-2,int z=mod){int ret=1;while (y){if (y&1) ret=(ret*x)%z;x=(x*x)%z;y>>=1;}return ret;}
int inv[N],fac[N],ifc[N];
void Init(int n)
{
    inv[1]=1;for (ri i=2;i<=n;i++)inv[i]=inv[mod%i]*(mod-mod/i)%mod;
    fac[0]=1;for (ri i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod;
    ifc[0]=1;for (ri i=1;i<=n;i++) ifc[i]=ifc[i-1]*inv[i]%mod;
}
int C(int n,int m){if (m>n || m<0) return 0;return fac[n]*ifc[m]%mod*ifc[n-m]%mod;}
int C(int n,int a,int b){if (a<0 || b<0 || a+b>n) return 0;return fac[n]*ifc[a]%mod*ifc[b]%mod*ifc[n-a-b]%mod;}
int T,n;
int lcm(int a,int b){return a/__gcd(a,b)*b;}
int exgcd(int a,int b,int &x,int &y) 
{
    if (!b) {x=1,y=0;return a;}
    int ret=exgcd(b,a%b,y,x);y-=a/b*x;
	return ret;
}
int a[3],r[3];
int excrt()
{
    int M=a[1],R=r[1],x,y,d;
    d=exgcd(M,a[2],x,y);
    if ((R-r[2])%d) return -1;
    x=(R-r[2])/d*x%a[2];
    R-=M*x;
    M=M/d*a[2];
    R%=M;
    return (R%M+M)%M;
}
int x,y,p,q,ans;

signed main()
{
	rd(T);
	while (T--)
	{
		rd(x);rd(y);rd(p);rd(q);
		ans=-1;
		n=(p+q)*(2*x+2*y); 
		for (int i=x;i<x+y;i++) for (int j=p;j<p+q;j++)
		{
			r[1]=i;r[2]=j;
			a[1]=2*x+2*y;a[2]=p+q;
			int C=excrt();
			if (C==-1) continue;
			if (ans==-1) ans=C;
			else ans=min(ans,C);
		}
		if (ans==-1) puts("infinity");
		else cout<<ans<<endl;
	}
}
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef long long ll;

template <typename _T>
inline void read(_T &f) {
    f = 0; _T fu = 1; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); }
    while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
    f *= fu;
}

template <typename T>
void print(T x) {
    if (x < 0) putchar('-'), x = -x;
    if (x < 10) putchar(x + 48);
    else print(x / 10), putchar(x % 10 + 48);
}

template <typename T>
void print(T x, char t) {
    print(x); putchar(t);
}

const int N = 1005;
const char str[] = " atcoder";

char c[N];
int T, n;

int main() {
    read(T);
    while (T--) {
        scanf("%s", c + 1); n = strlen(c + 1);
        int cando = -1;
        for (int i = 1; i <= min(n, 7); i++) {
            if (c[i] != str[i]) {
                if (c[i] > str[i]) cando = 1;
                else cando = 0;
                break;
            }
        }
        if (cando == 1 || (cando == -1 && n > 7)) {
            printf("0\n");
            continue;
        }
        if (n == 1) {
            printf("-1\n");
            continue;
        }
        if (c[2] != 'a') {
            printf("1\n");
            continue;
        }
        int ans = 100000;
        for (int i = 3; i <= n; i++) {
            if (c[i] > 'a') ans = min(ans, i - 1);
            if (c[i] > 't') ans = min(ans, i - 2);
        }
        if (ans == 100000) ans = -1;
        print(ans, '\n');
    }
    return 0;
}