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