import numpy as np def main(): n,b,q=map(int,input().split()) arr = [] for _ in range(n): a11,a12=map(int,input().split()) a21,a22=map(int,input().split()) a = np.matrix([[a11,a12],[a21,a22]]) arr.append(a) mod_b = lambda x: x % b acc = [np.eye(2, dtype=int)] for i in range(n): a = arr[i] * acc[-1] a = mod_b(a) acc.append(a) for _ in range(q): l,r,x,y=map(int,input().split()) a1 = acc[r] a2 = acc[l] inv_a2 = np.matrix([[a2[1,1],-a2[0,1]],[-a2[1,0],a2[0,0]]]) a = a1 * inv_a2 a = mod_b(a) zw = a * np.matrix([[x],[y]]) z = zw[0,0] w = zw[1,0] z = z%b w = w%b print(z,w) if __name__ == "__main__": main()