MOD = 10**9+7 T = int(input()) for _ in range(T): n,g1,g2,c1,c2,p1,p2 = map(int,input().split()) g = g1*pow(g2,MOD-2,MOD) c = c1*pow(c2,MOD-2,MOD) p = p1*pow(p2,MOD-2,MOD) res = pow(g+c,n,MOD) + pow(c+p,n,MOD) + pow(p+g,n,MOD) res -= 2*(pow(g,n,MOD) + pow(c,n,MOD) + pow(p,n,MOD)) print((1-res)%MOD)