#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--) 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; } struct mat{ mint v[2][2]; }; mat e(){ mat ret; rep(i,0,2) rep(j,0,2){ if (i==j) ret.v[i][j] = 1; else ret.v[i][j] = 0; } return ret; } mat te(){ mat ret; rep(i,0,2) rep(j,0,2) ret.v[i][j] = 0; return ret; } mat op(mat a, mat b){ mat ret = te(); rep(i,0,2){ rep(j,0,2){ rep(k,0,2){ ret.v[i][j] += b.v[i][k] * a.v[k][j]; } } } return ret; } int main(){ int n; cin >> n; int b; cin >> b; mint::set_mod(b); int q; cin >> q; vector t(n); rep(i,0,n){ rep(j,0,2){ rep(k,0,2){ int x; cin >> x; t[i].v[j][k] = x; } } } segtree seg(t); rep(num,0,q){ int l, r; cin >> l >> r; int xx, yx; cin >> xx >> yx; vector z = {xx, yx}; mat g = seg.prod(l, r); vector h = { g.v[0][0] * z[0] + g.v[0][1] * z[1], g.v[1][0] * z[0] + g.v[1][1] * z[1] }; cout << h[0].val() << ' ' << h[1].val() << '\n'; } }