mod = int(1e9) + 7 def doubling(n, m): y = 1 bas = n while m: if m % 2: y *= bas y %= mod bas *= bas bas %= mod m >>= 1 return y def inved(a): x, y, u, v, k, l = 1, 0, 0, 1, a, mod while l: x, y, u, v, k, l = u, v, x - u * (k // l), y - v * (k // l), l, k % l return x % mod T = int(input()) for _ in range(T): N, AG, BG, AC, BC, AP, BP = map(int, input().split()) P = 1 GP, CP, PP = AG * inved(BG) % mod, AC * inved(BC) % mod, AP * inved(BP) % mod P -= doubling(GP+CP, N) % mod P %= mod P -= doubling(CP+PP, N) % mod P %= mod P -= doubling(PP+GP, N) % mod P %= mod P += 2 * doubling(GP, N) % mod P %= mod P += 2 * doubling(CP, N) % mod P %= mod P += 2 * doubling(PP, N) % mod P %= mod print(P)