n, md, q = map(int, input().split()) a = [[list(map(int, input().split())), list(map(int, input().split()))] for _ in range(n)] # 後ろから累積積を取る # 0>E # 1>N-1 # 2>(N-1)*(N-2) b = [[[1, 0], [0, 1]]] for i in a[::-1]: # b[-1]*iを末尾に追加 b.append([[b[-1][0][0] * i[0][0] + b[-1][0][1] * i[1][0], b[-1][0][0] * i[0][1] + b[-1][0][1] * i[1][1]], [b[-1][1][0] * i[0][0] + b[-1][1][1] * i[1][0], b[-1][1][0] * i[0][1] + b[-1][1][1] * i[1][1]]]) b.append(b[-1]) for _ in range(q): l, r, x, y = map(int, input().split()) p = b[n - r] q = b[n - l + 1] # p^-1*qを求める # pの行列式は1 c = p[1][1] d = -p[0][1] e = -p[1][0] f = p[0][0] g = q[0][0] h = q[0][1] i = q[1][0] j = q[1][1] s = c * g + d * i t = c * h + d * j u = e * g + f * i v = e * h + f * j print((s * x + t * y) % md, (u * x + v * y) % md)