using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static long[] NList => ReadLine().Split().Select(long.Parse).ToArray(); static long[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); // Test(); } static void Test() { var r = new Random(); var count = 0; while (true) { var n = 100; var b = r.Next(1, 1000000000); var q = 1; var mxlist = new long[n][][]; for (var i = 0; i < n; ++i) mxlist[i] = Make(r); var query = new long[q][]; for (var i = 0; i < q; ++i) { var s = r.Next(1, n + 1); var t = r.Next(1, n + 1); query[i] = new long[] { Math.Min(s, t), Math.Max(s, t), r.Next(1000000000), r.Next(1000000000) }; } // 愚直解 var s1 = All(n, b, q, mxlist, query); // 作成解 var s2 = Linear(n, b, q, mxlist, query); if (s1 != s2) { WriteLine($"{n} {b} {q}"); for (var i = 0; i < n; ++i) WriteLine(string.Join("\n", mxlist[i].Select(m => string.Join(" ", m)))); WriteLine(string.Join("\n", query.Select(qi => string.Join(" ", qi)))); WriteLine(s1); WriteLine(s2); return; } ++count; if (count % 1000 == 0) WriteLine(count); } } static long[][] Make(Random r) { while (true) { var ans = new long[][] { new long[] { r.Next(-10, 10), r.Next(-10, 10) }, new long[] { r.Next(-10, 10), r.Next(-10, 10) } }; if (ans[0][0] * ans[1][1] - ans[0][1] * ans[1][0] == 1) return ans; } } static string All(long n, long b, long q, long[][][] mxlist, long[][] query) { Matrix.mod = b; var ans = new long[q][]; for (var i = 0; i < q; ++i) { var x = new long[][] { new long[] { query[i][2] }, new long[] { query[i][3] } }; for (var j = query[i][0] + 1; j <= query[i][1]; ++j) { x = Matrix.Mul(mxlist[j - 1], x); } ans[i] = new long[] { (x[0][0] % b + b) % b, (x[1][0] % b + b) % b }; } return string.Join("\n", ans.Select(ai => string.Join(" ", ai))); } static void Solve() { var c = NList; var (n, b, q) = (c[0], c[1], c[2]); var mxlist = new long[n][][]; for (var i = 0; i < n; ++i) { mxlist[i] = NArr(2); } var query = NArr(q); } static string Linear(long n, long b, long q, long[][][] _mxlist, long[][] query) { var mxlist = new long[n][][]; for (var i = 0; i < n; ++i) { mxlist[i] = new long[2][]; mxlist[i][0] = new long[] { _mxlist[i][0][0], _mxlist[i][0][1] }; mxlist[i][1] = new long[] { _mxlist[i][1][0], _mxlist[i][1][1] }; for (var j = 0; j < 2; ++j) for (var k = 0; k < 2; ++k) { mxlist[i][j][k] = (mxlist[i][j][k] % b + b) % b; } } Matrix.mod = b; var mullist = new long[n + 1][][]; mullist[0] = new long[][] { new long[] { 1, 0 }, new long[] { 0, 1 } }; for (var i = 0; i < n; ++i) { mullist[i + 1] = Matrix.Mul(mxlist[i], mullist[i]); } var revlist = new long[n + 1][][]; revlist[0] = new long[][] { new long[] { 1, 0 }, new long[] { 0, 1 } }; for (var i = 0; i < n; ++i) { revlist[i + 1] = Matrix.Mul(revlist[i], Rev1(mxlist[i])); } var ans = new long[q][]; for (var i = 0; i < q; ++i) { var l = query[i][0]; var r = query[i][1]; var x = new long[][] { new long[] { query[i][2] }, new long[] { query[i][3] } }; var z = Matrix.Mul(Matrix.Mul(mullist[r], revlist[l]), x); ans[i] = new long[] { z[0][0], z[1][0] }; } return string.Join("\n", ans.Select(ai => string.Join(" ", ai))); } static long[][] Rev1(long[][] m) { return new long[][] { new long[] { m[1][1], (Matrix.mod - m[0][1]) % Matrix.mod }, new long[] { (Matrix.mod - m[1][0]) % Matrix.mod, m[0][0] }}; } class Matrix { public static long mod = 1_000_000_007; public static long[][] Mul(long[][] x, long[][] y) { var r = new long[x.Length][]; checked { for (var i = 0; i < x.Length; ++i) r[i] = new long[y[0].Length]; for (var i = 0; i < x.Length; ++i) for (var k = 0; k < x[0].Length; ++k) { for (var j = 0; j < y[0].Length; ++j) r[i][j] = (r[i][j] + x[i][k] * y[k][j]) % mod; } } return r; } } }