T = int(input()) P = 10 ** 9 + 7 def calc(a,b,n): inv = pow(b,P-2,P) return pow(a * inv % P,n,P) for _ in range(T): n,a,b,c,d,e,f = map(int,input().split()) ans = calc(a,b,c) + calc(c,d,n) + calc(e,f,n) print(ans % P)