/** author: shobonvip created: 2025.05.30 22:38:12 **/ #include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) #define all(v) v.begin(), v.end() template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } ll ceil_sqrt(ll n) { ll ub = 2e9; ll lb = -1; while (ub - lb > 1) { ll t = (ub + lb) / 2; if (t * t >= n) { ub = t; }else{ lb = t; } } return ub; } ll modpow(ll n, ll m, ll mod) { ll v = 1; ll w = n; while (m > 0) { if (m & 1) { v = v * w % mod; } w = w * w % mod; m >>= 1; } return v; } vector> prod( const vector> &a, const vector> &b ) { vector> ret(2, vector(2)); rep(i,0,2)rep(j,0,2)rep(k,0,2) ret[i][j] += a[i][k] * b[k][j]; return ret; } vector> matpow( vector> &n, ll m ) { vector> v(2, vector(2)); v[0][0] = 1; v[1][1] = 1; vector> w = n; while (m > 0) { if (m & 1) { v = prod(v, w); } w = prod(w, w); m >>= 1; } return v; } ll discrete_logarithm(ll x, ll y, ll m) { if (m == 1) return 0; if (y == 1) return 0; if (x == 0 && y == 0) return 1; ll r = ceil_sqrt(m); map d; ll a = 1; for (ll i=0; i> &x) { if (x[0][0].val() != 1) return false; if (x[1][1].val() != 1) return false; if (x[1][0].val() != 0) return false; if (x[0][1].val() != 0) return false; return true; } typedef array ar; ll discrete_logarithm_matrix(vector> x, vector> y) { ll r = (ll)mint::mod(); map d; vector> a(2,vector(2)); a[0][0]=1; a[1][1]=1; for (ll i=0; i> pr = prod(a,y); ar prl; rep(s,0,2)rep(t,0,2)prl[s*2+t]=pr[s][t].val(); if (d.find(prl) == d.end()) { d[prl] = i; } a = prod(a, x); } vector> xi = matpow(x, r); vector> b = xi; for (ll i=1; i> &a) { return a[0][0] * a[1][1] - a[1][0] * a[0][1]; } void solve() { int p; cin >> p; mint::set_mod(p); vector> a(2, vector(2)); vector> b(2, vector(2)); rep(i,0,2) rep(j,0,2) { int x; cin >> x; a[i][j] = x; } rep(i,0,2) rep(j,0,2) { int x; cin >> x; b[i][j] = x; } if (is_identity(b)) { cout << 0 << '\n'; return; } if (a == b) { cout << 1 << '\n'; return; } // https://math.stackexchange.com/questions/3278216/the-order-of-a-2-times-2-matrix-mod-p // the period devides p^2-1 or p^2-p // thus period <= p^2, so just bsgs and done in O(p sqrt(p)). ll ans = discrete_logarithm_matrix(a, b); cout << ans << '\n'; return; /* if (det(a) == 0) { if (det(b) != 0) { cout << -1 << '\n'; return; } bool mode = 0; vector> c(2, vector(2)); mint tr = a[0][0] + a[1][1]; if (tr.val() != 0) { bool mode = 0; mint captain = 0; rep(i,0,2) { rep(j,0,2) { if (a[i][j].val() == 0 && b[i][j].val() != 0) { cout << -1 << '\n'; return; } if (a[i][j].val() != 0 && b[i][j].val() == 0) { cout << -1 << '\n'; return; } if (a[i][j].val() != 0 && b[i][j].val() != 0) { if (!mode) { captain = b[i][j] / a[i][j]; mode = 1; } else { if (captain.val() != (b[i][j] / a[i][j]).val()) { cout << -1 << '\n'; return; } } } } } ll v = discrete_logarithm(tr.val(), captain.val(), p); cout << v+1 << '\n'; return; }else{ rep(i,0,2) { rep(j,0,2) { if (b[i][j].val() != 0) { cout << -1 << '\n'; return; } } } cout << 1 << '\n'; return; } }else{ if (det(b) == 0) { cout << -1 << '\n'; return; } } */ } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while(t--) solve(); }