/*** author: yuji9511 ***/ #include // #include // using namespace atcoder; using namespace std; using ll = long long; using lpair = pair; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) #define printa(x,n) for(ll i=0;i void print(H&& h, T&&... t){cout<(t)...);} #define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << "\n" ll power(ll x, ll n, ll m = MOD){ if(n == 0) return 1LL; ll res = power(x * x % m, n/2, m); if(n % 2 == 1) (res *= x) %= m; return res; } void solve(){ ll N, Ag, Bg, Ac, Bc, Ap, Bp; cin >> N >> Ag >> Bg >> Ac >> Bc >> Ap >> Bp; ll ans = 1; ll g = Ag * power(Bg, MOD-2) % MOD; ll c = Ac * power(Bc, MOD-2) % MOD; ll p = Ap * power(Bp, MOD-2) % MOD; ans = (ans - power(g+c, N) + MOD) % MOD; ans = (ans - power(p+c, N) + MOD) % MOD; ans = (ans - power(g+p, N) + MOD) % MOD; ans = (ans + power(p, N) + MOD) % MOD; ans = (ans + power(g, N) + MOD) % MOD; ans = (ans + power(c, N) + MOD) % MOD; ans = (ans + power(p, N) + MOD) % MOD; ans = (ans + power(g, N) + MOD) % MOD; ans = (ans + power(c, N) + MOD) % MOD; print(ans); } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll T; cin >> T; while(T--) solve(); }