#include using namespace std; #define ll long long #define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++) #define rrep(i, n, a) for (int i = (int)(n); i >= (int)(a); i--) ll b; void mult(ll m1[2][2], ll m2[2][2], ll result[2][2]){ result[0][0] = m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]; result[0][1] = m1[0][0]*m2[0][1]+m1[0][1]*m2[1][1]; result[1][0] = m1[1][0]*m2[0][0]+m1[1][1]*m2[1][0]; result[1][1] = m1[1][0]*m2[0][1]+m1[1][1]*m2[1][1]; } ll positive_modulo(ll i, ll n) { return (i % n + n) % n; } void solve(){ int n,q; cin >> n >> b >> q; vector A(n+1); vector Ap(n+1); vector Ainv(n+1); Ainv[0][0][0] = 1; Ainv[0][0][1] = 0; Ainv[0][1][0] = 0; Ainv[0][1][1] = 1; Ap[0][0][0] = 1; Ap[0][0][1] = 0; Ap[0][1][0] = 0; Ap[0][1][1] = 1; rep(i,1,n+1){ cin >> A[i][0][0] >> A[i][0][1]; cin >> A[i][1][0] >> A[i][1][1]; mult(A[i], Ap[i-1], Ap[i]); Ainv[i][0][0] = Ap[i][1][1]; Ainv[i][0][1] = -Ap[i][0][1]; Ainv[i][1][0] = -Ap[i][1][0]; Ainv[i][1][1] = Ap[i][0][0]; } for(int i = 0; i < q; i++){ ll L,R,x,y; cin >> L >> R >> x >> y; ll mat[2][2]; mult(Ap[R], Ainv[L], mat); ll z = positive_modulo(mat[0][0]*x+mat[0][1]*y,b); ll w = positive_modulo(mat[1][0]*x+mat[1][1]*y,b); cout << z << " " << w << endl; //cout << " no "<> t; rep(i,0,t) solve(); return 0; }