#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 p = 0, ans = 0, f = 1;
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 k = 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] == '^')
k += read(fac);
else if (!strcmp(fac, "x") || !strcmp(fac, "sinx") || !strcmp(fac, "arcsinx") || !strcmp(fac, "tanx") || !strcmp(fac, "arctanx") || !strcmp(fac, "e^x-1") || !strcmp(fac, "ln(x+1)"))++k;
else if (!strcmp(fac, "1-cosx"))k += 2, b *= 2;
else a *= read(fac);
}
for (int i = 0; i < m; ++i)
{
scanf("%s", fac);
if (fac[0] == 'x' && fac[1] == '^')
k -= read(fac);
else if (!strcmp(fac, "x") || !strcmp(fac, "sinx") || !strcmp(fac, "arcsinx") || !strcmp(fac, "tanx") || !strcmp(fac, "arctanx") || !strcmp(fac, "e^x-1") || !strcmp(fac, "ln(x+1)"))--k;
else if (!strcmp(fac, "1-cosx"))k -= 2, a *= 2;
else b *= read(fac);
}
if (k > 0)
puts("0");
else if (k < 0)
puts("INF");
else
{
bool flag = (a > 0 && b > 0) || (a < 0 && b < 0);
a = abs(a);
b = abs(b);
long long c = gcd(a, b);
if (!flag)
putchar('-');
if (c == b)
printf("%lld\n", a / b);
else
printf("%lld/%lld\n", a / c, b / c);
}
}
// system("pause");
}