#include using namespace std; using ll = long long; template using matrix = vector>; ll modc; class mint { ll x; public: mint(ll x=0) : x((x%modc+modc)%modc) {} mint operator-() const { return mint(-x);} mint& operator+=(const mint& a) { if ((x += a.x) >= modc) x -= modc; return *this; } mint& operator-=(const mint& a) { if ((x += modc-a.x) >= modc) x -= modc; return *this; } mint& operator*=(const mint& a) { (x *= a.x) %= modc; return *this; } mint operator+(const mint& a) const { mint res(*this); return res+=a; } mint operator-(const mint& a) const { mint res(*this); return res-=a; } mint operator*(const mint& a) const { mint res(*this); return res*=a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } mint inv() const {return pow(modc-2);} mint& operator/=(const mint& a){ return (*this) *= a.inv();} mint operator/(const mint& a) const { mint res(*this); return res/=a; } bool operator == (const mint& a) const{ return x == a.x;} bool operator != (const mint& a) const{ return x != a.x;} friend ostream& operator<<(ostream& os, const mint& m){ os << m.x; return os; } friend istream& operator>>(istream& ip, mint &m) { ll t; ip >> t; m = mint(t); return ip; } ll val(){ return x;} }; template void show(matrix &a){ for (int i=0; i matrix dot(matrix &a, matrix &b){ int N = a.size(); matrix c(N, vector(N)); for (int i=0; i T det(matrix a){ int N = a.size(); T res = 1; for (int i=0; i matrix pow(matrix a, ll N){ int M = a.size(); matrix b(M, vector(M)); for (int i=0; i>= 1; a = dot(a, a); } return b; } template matrix inverse(matrix &a){ T p, q, r, s, t; p = a[0][0]; q = a[0][1]; r = a[1][0]; s = a[1][1]; matrix res = {{s, -q}, {-r, p}}; return res; } int main(){ ll N, B, Q, l, r; mint x, y; cin >> N >> B >> Q; modc = B; vector> s(N+1); s[0] = {{1, 0}, {0, 1}}; for (int i=1; i<=N; i++){ matrix a(2, vector(2)); for (int j=0; j<2; j++) for (int k=0; k<2; k++) cin >> a[j][k]; s[i] = dot(a ,s[i-1]); } while(Q){ Q--; cin >> l >> r >> x >> y; matrix a=s[r], b=inverse(s[l]), c; c = dot(a, b); mint p=x*c[0][0]+y*c[0][1], q=x*c[1][0]+y*c[1][1]; cout << p << " " << q << endl; } return 0; }