#include using namespace std; #include using namespace atcoder; #define rep(i, n) for(int i=0;i<(n);++i) #define rep1(i, n) for(int i=1;i<=(n);i++) #define ll long long using mint = modint; using P = pair; using lb = long double; using T = tuple; #ifdef LOCAL # include # define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define dbg(...) (static_cast(0)) #endif int main() { int n, b, q; cin >> n >> b >> q; mint::set_mod(b); vector>> a(n, vector>(2, vector(2))); rep(i,n){ rep(j,2)rep(k,2){ cin >> a[i][j][k]; } } vector>> sum(n+1, vector>(2, vector(2))); vector>> inv(n+1, vector>(2, vector(2))); sum[0] = {{1,0},{0,1}}; // dbg(sum[0]); inv[0] = {{1,0},{0,1}}; rep(i,n){ rep(k,2)rep(j,2)rep(l,2){ sum[i+1][k][j] += a[i][k][l] * sum[i][l][j]; } vector> inva = a[i]; swap(inva[0][0], inva[1][1]); inva[0][1] *= -1; inva[1][0] *= -1; rep(k,2)rep(j,2)rep(l,2){ inv[i+1][k][j] += inv[i][k][l] * inva[l][j]; } } rep(qi,q){ int l, r, x, y; cin >> l >> r >> x >> y; // --l; vector> tmp = sum[r]; vector> tinv = inv[l]; vector> now(2, vector(2)); rep(k,2)rep(j,2)rep(l,2){ now[k][j] += tmp[k][l] * tinv[l][j]; } mint ax, ay = 0; ax = now[0][0]*x + now[0][1]*y; ay = now[1][0]*x + now[1][1]*y; cout << ax.val() << " " << ay.val() << endl; } return 0; }