t = int(input()) mod = 10**9+7 for _ in range(t): n, ag, bg, ac, bc, ap, bp = map(int, input().split()) p1 = (bp-ap)*pow(bp, mod-2, mod) p1 = pow(p1, n, mod) p2 = ap*pow(bp, mod-2, mod) p2 = pow(p2, n, mod) c1 = (bc-ac)*pow(bc, mod-2, mod) c1 = pow(c1, n, mod) c2 = ac*pow(bc, mod-2, mod) c2 = pow(c2, n, mod) g1 = (bg-ag)*pow(bg, mod-2, mod) g1 = pow(g1, n, mod) g2 = ag*pow(bg, mod-2, mod) g2 = pow(g2, n, mod) ans = 1-p1-c1-g1+2*p2+2*c2+2*g2 ans %= mod print(ans)