import sys input = sys.stdin.readline n, b, q = map(int, input().split()) a = [[list(map(int, input().split())) for _ in [0] * 2] for _ in [0] * n] query = [list(map(int, input().split())) for _ in [0] * q] def matrix_prod(A, B): ret = [[0, 0], [0, 0]] for i in range(2): for j in range(2): for k in range(2): ret[i][j] += A[i][k] * B[k][j] ret[i][j] %= b return ret acc = [[[1, 0], [0, 1]]] + a[:] for i in range(1, n + 1): acc[i] = matrix_prod(a[i - 1], acc[i - 1]) # print(acc) def matrix_inv(A): return [[A[1][1], -A[0][1]], [-A[1][0], A[0][0]]] for l, r, x, y in query: P = matrix_prod(acc[r], matrix_inv(acc[l])) v = [x, y] ans = [0, 0] for i in range(2): for j in range(2): ans[i] += P[i][j] * v[j] ans[i] %= b print(*ans)