def mat_mul(a, b, MOD): """ a: 行列(2次元配列)I*J b: 行列(2次元配列)J*K """ I, J, K = len(a), len(b[0]), len(b) c = [[0] * J for _ in range(I)] for i in range(I): for j in range(J): for k in range(K): c[i][j] += a[i][k] * b[k][j] c[i][j] %= MOD return c N, B, Q = map(int, input().split()) A = [] cumA = [[[1, 0], [0, 1]]] cumA_inv = [[[1, 0], [0, 1]]] for _ in range(N): An = [list(map(int, input().split())) for _ in range(2)] A_inv = [[An[1][1], -An[0][1]], [-An[1][0], An[0][0]]] cumA.append(mat_mul(An, cumA[-1], B)) cumA_inv.append(mat_mul(cumA_inv[-1], A_inv, B)) ans = [] for _ in range(Q): L, R, x, y = map(int, input().split()) C = mat_mul(cumA[R], cumA_inv[L], B) z = (C[0][0]*x+C[0][1]*y) % B w = (C[1][0]*x+C[1][1]*y) % B ans.append((z, w)) for c in ans: print(*c)