def mul(A, B, mod): C = [[0,0], [0,0]] for i in [0, 1]: for j in [0, 1]: for k in [0, 1]: C[i][j] += A[i][k] * B[k][j] C[i][j] %= mod return C def inv(A): [[a, b], [c, d]] = A return [[d, -b], [-c, a]] def solve(): N, B, Q = map(int, input().split()) A = [[None, None] for _ in range(N+1)] A[0] = [[1,0], [0,1]] for i in range(1, N+1): A[i][0] = list(map(int, input().split())) A[i][1] = list(map(int ,input().split())) P = [None] * (N+1); P[0] = A[0] for i in range(1, N+1): P[i] = mul(A[i], P[i-1], B) Ans = [None] * Q for q in range(Q): L, R, x, y =map(int, input().split()) [[s, t], [u, v]] = mul(inv(P[L]), P[R], B) z = (s * x + t * y) % B w = (u * x + v * y) % B Ans[q] = (z,w) return Ans #================================================== import sys input=sys.stdin.readline write=sys.stdout.write write("\n".join(map(lambda t: f"{t[0]} {t[1]}", solve())))