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()) n = N%(p-1) S = 1 a = pow(BG,n) b = pow(BC,n) c = pow(BP,n) S = (S+2*pow(AG,n)*pow(a,p-2))%p S = (S+2*pow(AC,n)*pow(b,p-2))%p S = (S+2*pow(AP,n)*pow(c,p-2))%p S = (S-pow(BG-AG,n)*pow(a,p-2))%p S = (S-pow(BC-AC,n)*pow(b,p-2))%p S = (S-pow(BP-AP,n)*pow(c,p-2))%p print(S)