import sys input = sys.stdin.readline T = int(input()) MOD = 10**9+7 inv = lambda x: pow(x, MOD-2, MOD) for _ in range(T): N, Ag, Bg, Ac, Bc, Ap, Bp = map(int, input().split()) g = Ag*inv(Bg)%MOD c = Ac*inv(Bc)%MOD p = Ap*inv(Bp)%MOD ans = 1 ans -= pow(g+c, N, MOD)-pow(g, N, MOD)-pow(c, N, MOD) ans %= MOD ans -= pow(g+p, N, MOD)-pow(g, N, MOD)-pow(p, N, MOD) ans %= MOD ans -= pow(c+p, N, MOD)-pow(c, N, MOD)-pow(p, N, MOD) ans %= MOD print(ans)