#include using namespace std; using ll = long long; #include using namespace atcoder; using mint = modint1000000007; using vm = vector; using vvm = vector; using vvvm = vector; vector fact, factinv, inv; const ll mod = 1e9+7; void prenCkModp(ll n) { fact.resize(n + 5); factinv.resize(n + 5); inv.resize(n + 5); fact[0] = fact[1] = 1; factinv[0] = factinv[1] = 1; inv[1] = 1; for (ll i = 2; i < n + 5; i++) { fact[i] = (fact[i - 1] * i); inv[i] = (mod - ((inv[mod % i] * (mod / i)))); factinv[i] = (factinv[i - 1] * inv[i]); } } mint nCk(ll n, ll k) { if (n < k || k < 0) return 0; return (fact[n] * ((factinv[k] * factinv[n - k]))); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int T; cin>>T; while(T--){ ll N; cin>>N; vector A(6); for(int i=0;i<6;i++){ ll a; cin>>a; A[i]=a; if(i%2==1)A[i-1]/=A[i]; } mint an=(A[0]+A[2]).pow(N)+(A[4]+A[2]).pow(N)+(A[0]+A[4]).pow(N); an-=(A[0].pow(N)+A[2].pow(N)+A[4].pow(N))*2; cout<<(1-an).val()<