import sys input = sys.stdin.buffer.readline MOD = 10 ** 9 + 7 modinv = lambda a, mod=MOD: pow(a, mod - 2, mod) def solve(N, AG, BG, AC, BC, AP, BP): G = AG * modinv(BG) % MOD C = AC * modinv(BC) % MOD P = AP * modinv(BP) % MOD ans = (1 - pow(1 - G, N, MOD) - pow(1 - C, N, MOD) - pow(1 - P, N, MOD) + 2 * pow(G, N, MOD) + 2 * pow(C, N, MOD) + 2 * pow(P, N, MOD) ) % MOD return ans T = int(input()) for _ in range(T): print(solve(*map(int, input().split())))