p = 10**9+7 def pow(x,m): if m==0: return 1 if m==1: return x if m%2==0: return (pow(x,m//2)**2)%p else: return (x*(pow(x,(m-1)//2)**2)%p)%p T = int(input()) for _ in range(T): N,AG,BG,AC,BC,AP,BP = map(int,input().split()) S = 1 S = (S+2*pow(AG,N)*pow(pow(BG,N),p-2))%p S = (S+2*pow(AC,N)*pow(pow(BC,N),p-2))%p S = (S+2*pow(AP,N)*pow(pow(BP,N),p-2))%p S = (S-pow(BG-AG,N)*pow(pow(BG,N),p-2))%p S = (S-pow(BC-AC,N)*pow(pow(BC,N),p-2))%p S = (S-pow(BP-AP,N)*pow(pow(BP,N),p-2))%p print(S)