MOD=10**9+7 def solve(): N,*arr=map(int,input().split()) Ag,Bg,Ac,Bc,Ap,Bp=arr Cc=pow(Bc-Ac,N,MOD);Cg=pow(Bg-Ag,N,MOD);Cp=pow(Bp-Ap,N,MOD) Ag,Bg,Ac,Bc,Ap,Bp=map(lambda x:pow(x,N,MOD),arr) B=Bc*Bg*Bp%MOD A=0 A=(A+Cc*Bg*Bp%MOD)%MOD A=(A+Cp*Bg*Bc%MOD)%MOD A=(A+Cg*Bc*Bp%MOD)%MOD r=(Ac*Bg*Bp%MOD+Ag*Bc*Bp%MOD+Ap*Bc*Bg%MOD)*2%MOD A=(A-r+MOD)%MOD A=(B-A+MOD)%MOD print(pow(B,MOD-2,MOD)*A%MOD) T=int(input()) for _ in range(T): solve()