#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
using namespace std;
char fac[10];
int read(char s[])
{
int f = 1;
int ans = 0;
int p = 0;
for (; s[p] && !isdigit(s[p]) && s[p] != '-'; ++p);
if (s[p] == '-')
{
f = -1;
++p;
}
for (; s[p]; ++p)
ans = ans * 10 + s[p] - '0';
return ans * f;
}
long long gcd(long long a, long long b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n, m;
long long x = 0, a = 1, b = 1;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i)
{
scanf("%s", fac);
if (fac[0] == 'x' && fac[1] == '^')
x += read(fac);
else if (strcmp(fac, "x") == 0 || strcmp(fac, "sinx") == 0 || strcmp(fac, "arcsinx") == 0 || strcmp(fac, "arctanx") == 0 || strcmp(fac, "tanx") == 0 || strcmp(fac, "e^x-1") == 0 || strcmp(fac, "ln(x+1)") == 0)++x;
else if (strcmp(fac, "1-cosx") == 0)x += 2, b *= 2;
else a *= read(fac);
}
for (int i = 0; i < m; ++i)
{
scanf("%s", fac);
if (fac[0] == 'x' && fac[1] == '^')
x -= read(fac);
else if (strcmp(fac, "x") == 0 || strcmp(fac, "sinx") == 0 || strcmp(fac, "arcsinx") == 0 || strcmp(fac, "arctanx") == 0 || strcmp(fac, "tanx") == 0 || strcmp(fac, "e^x-1") == 0 || strcmp(fac, "ln(x+1)") == 0)--x;
else if (strcmp(fac, "1-cosx") == 0)x -= 2, a *= 2;
else b *= read(fac);
}
if (x > 0)
puts("0");
else if (x < 0)
puts("INF");
else
{
int flag = (a > 0 && b > 0) || (a < 0 && b < 0);
a = abs(a);
b = abs(b);
if (flag == 0)
putchar('-');
long long k = gcd(a, b);
if (k == b)
{
printf("%lld\n", a / b);
}
else
{
printf("%lld/%lld\n", a / k, b / k);
}
}
}
//system("pause");
}