#include <stdio.h>
#define N 10000
typedef struct qu
{
int n;
int a[N];
int b[N];
}Q;
int gcd(int a,int b)
{
return a%b==0?b:gcd(b,a%b);
}
int main ()
{
int t,i,j,p=1,q=1;
scanf("%d",&t);
Q s[t];
for(i=0;i<t;i++)
{
scanf("%d",&s[i].n);
for(j=0;j<=s[i].n;j++)
scanf("%d",&s[i].a[j]);
for(j=0;j<=s[i].n;j++)
scanf("%d",&s[i].b[j]);
}
for(i=0;i<t;i++)
{
while(p<=s[i].n&&s[i].a[s[i].n-p]==0)
p++;
p--;
// printf("p=%d\n",p);
while(q<=s[i].n&&s[i].b[s[i].n-q]==0)
q++;
q--;
// printf("q=%d\n",q);
if(p==q)
printf("%d %d",s[i].a[s[i].n-p]/gcd(s[i].a[s[i].n-p],s[i].b[s[i].n-q]),s[i].b[s[i].n-q]/gcd(s[i].a[s[i].n-p],s[i].b[s[i].n-q]));
else if(p>q)
printf("0 1");
else
printf("infty");
printf("\n");
p=1;q=1;
}
return 0;
}