import itertools as iter import collections as coll import heapq as hq import bisect as bis from decimal import Decimal as dec from functools import cmp_to_key import math import sys #import pypyjit #pypyjit.set_param('max_unroll_recursion=-1') sys.setrecursionlimit(10 ** 6) inp = sys.stdin.readline input = lambda : inp()[:-1] getN = lambda : int(inp()) getNs = lambda : map(int, inp().split()) getList = lambda :list(map(int, inp().split())) getStrs = lambda n : [input() for _ in [0] * n] def yexit(): print("Yes"); exit(0) def nexit(): print("No"); exit(0) pi = 3.141592653589793 mod = 1000000007 MOD = 998244353 INF = 4611686018427387903 dx = [1, 0, -1, 0]; dy = [0, 1, 0, -1] #di = coll.defaultdict(int) """ SegmentTree from : https://github.com/customaddone/beginPython/blob/master/cgi-bin/library/segment_tree/segment_tree.py ref : https://algo-logic.info/segment-tree/ """ """ (単位元一覧) min:INF max:-INF XOR:0 区間和:0 区間積:1 最大公約数:0 """ ide_ele = [[1, 0], [0, 1]] #初期値(単位元) """ update(k,x) :k番目の要素をxに更新する query(l,r) : [l, r)(l <= k < r の区間)から値を取得する """ def segfunc(B, A): #求めたい処理(maxやmin、XORなど) 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 class SegmentTree: """ init(init_val, ide_ele): 配列init_valで初期化 O(N) update(k, x): k番目の値をxに更新 O(logN) query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN) """ def __init__(self, init_val, segfunc, ide_ele): """ init_val: 配列の初期値 segfunc: 区間にしたい操作 ide_ele: 単位元 n: 要素数 num: n以上の最小の2のべき乗 tree: セグメント木(1-index) """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res """ Main Code """ n, b, q = getNs() As = [[getList() for _ in [0] * 2] for _ in [0] * n] query = [getList() for _ in [0] * q] seg = SegmentTree(As, segfunc, ide_ele) def prod_vector(A, vec): ret = [0, 0] for i in range(2): for j in range(2): ret[i] += A[i][j] * vec[j] ret[i] %= b return ret for l, r, x, y in query: vec = [x, y] ans = prod_vector(seg.query(max(l - 1, 0), r), vec) print(*ans)