#include #include using namespace std; using namespace atcoder; using ll = long long; using db = long double; using ch = char; using bl = bool; using st = string; using pll = pair; using psl = pair; using vst = vector; using vch = vector; using vvch = vector; using vbl = vector; using vvbl = vector; using vdb = vector; using vpll = vector; using vvpll = vector; using vpsl = vector; using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using vll = vector; using vvll = vector; using vvvll = vector; using vvvvll = vector; using vvvvvll = vector; template using pq = priority_queue; template using pqg = priority_queue,greater>; #define all(A) A.begin(),A.end() #define sz(A) (ll)A.size() #define pb(a) push_back(a) #define mp(a,b) make_pair(a,b) #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rrep(i,a,b) for(ll i=(ll)(a);i<=(ll)(b);i++) #define drep(i,n) for(ll i=(ll)n-1; i>=0; i--) #define drrep(i,a,b) for(ll i=(ll)a; i>=(ll)b; i--) // using mint = modint998244353; // using mint = modint1000000007; using mint = modint; using vm = vector; using vvm = vector; using vvvm = vector; // const ll mod = 998244353; // const ll mod = 1000000007; const ll INF = 3e18; template inline bool chmin(T &a, T b) { if (a>b) { a=b; return true; } return false; } template inline bool chmax(T &a, T b) { if (a0) { if(n%2==1)res*=a; a*=a; n/=2; } return res; } ll Pow(ll a, ll n, ll p) { ll res=1; while(n>0) { a%=p; if(n%2==1)res*=a; a*=a; res%=p; n/=2; } return res; } void yn(bl ok) { cout << (ok?"Yes\n":"No\n"); return; } ll N,P,Q; vvm A[100011],A_inv[100011]; vvm S[100011],S_inv[100011]; vvm mtxmul(vvm A, vvm B) { assert(A[0].size()==B.size()); ll l=A.size(),m=B.size(), n=B[0].size(); vvm C(l,vm(n,0)); rep(i,l)rep(k,m)rep(j,n) { C[i][j]+=A[i][k]*B[k][j]; } return C; } vvm mtxpow(vvm A, ll N) { assert(A.size()==A[0].size()); ll n=A.size(); vvm B(n,vm(n,0)); rep(i,n)B[i][i]=1; while(N>0) { if(N&1)B=mtxmul(A,B); A=mtxmul(A,A); N/=2; } return B; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin>>N>>P>>Q; mint::set_mod(P); vvm E={{mint(1),mint(0)},{mint(0),mint(1)}}; rrep(i,1,N) { ll a,b,c,d; cin>>a>>b>>c>>d; A[i]={{a,b},{c,d}}; A_inv[i]={{d,-b},{-c,a}}; } S[0]=S_inv[N+1]=S_inv[0]=E; rrep(i,1,N) { S[i]=mtxmul(A[i],S[i-1]); } drrep(i,N,1) { S_inv[i]=mtxmul(A_inv[i],S_inv[i+1]); } while(Q--) { ll L,R,x,y; cin>>L>>R>>x>>y; vvm B={{x},{y}}; B=mtxmul(mtxmul(S[R],S_inv[L]),B); cout << B[0][0].val() << ' ' << B[1][0].val() << endl; } return 0; }