M=10**9+7 T=int(input()) for _ in range(T): n,a1,b1,a2,b2,a3,b3=map(int,input().split()) p1=a1*pow(b1,M-2,M)%M p2=a2*pow(b2,M-2,M)%M p3=a3*pow(b3,M-2,M)%M p=[p1,p2,p3] a=0 for i in range(1<<3): a+=((-1)**(i.bit_count()%2))*pow(1-sum(p[j]*((i>>j)&1) for j in range(3)),n,M) a+=pow(p[0],n,M)+pow(p[1],n,M)+pow(p[2],n,M) a%=M print(a)