def matrix_mul(A, B, MOD): return [ [(A[0][0]*B[0][0] + A[0][1]*B[1][0])%MOD, (A[0][0]*B[0][1] + A[0][1]*B[1][1])%MOD], [(A[1][0]*B[0][0] + A[1][1]*B[1][0])%MOD, (A[1][0]*B[0][1] + A[1][1]*B[1][1])%MOD] ] def matrix_inv(A): return [ [A[1][1], -A[0][1]], [-A[1][0], A[0][0]] ] N, B, Q = map(int, input().split()) A = [[list(map(int, input().split())) for _ in range(2)] for _ in range(N)] LRXY = [list(map(int, input().split())) for _ in range(Q)] S = [[[1, 0], [0, 1]]] for i in range(N): S.append(matrix_mul(A[i], S[-1], B)) for L, R, X, Y in LRXY: tmp = matrix_mul(S[R], matrix_inv(S[L]), B) print( (tmp[0][0]*X+tmp[0][1]*Y)%B, (tmp[1][0]*X+tmp[1][1]*Y)%B )