import sys def input(): return sys.stdin.buffer.readline()[:-1] MOD = 10**9+7 def f(n, a1, b1, a2, b2, a3, b3): g = (a1 * pow(b1, MOD-2, MOD)) % MOD c = (a2 * pow(b2, MOD-2, MOD)) % MOD p = (a3 * pow(b3, MOD-2, MOD)) % MOD res = 1 res += 2 * (pow(g, n, MOD) + pow(c, n, MOD) + pow(p, n, MOD)) res -= pow(g+c, n, MOD) + pow(c+p, n, MOD) + pow(p+g, n, MOD) return res % MOD for _ in range(int(input())): n, p, q, r, s, t, u = map(int, input().split()) print(f(n, p, q, r, s, t, u))