#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ferrario ios_base::sync_with_stdio(0), cin.tie(0)
struct matrix
{
int a, b, c, d;
};
const int N = 2e5 + 5;
int r, n, m, n_;
matrix S[4 * N], I;
void f1(int sti, int ll, int rr, int ix, const matrix &x)
{
if (ll == rr)
{
S[sti] = x;
}
else
{
int m = (ll + rr) >> 1;
if (ix <= m)
f1(2 * sti + 1, ll, m, ix, x);
else
f1(2 * sti + 2, m + 1, rr, ix, x);
S[sti].a = (S[2 * sti + 1].a * S[2 * sti + 2].a + S[2 * sti + 1].b * S[2 * sti + 2].c) % r;
S[sti].b = (S[2 * sti + 1].a * S[2 * sti + 2].b + S[2 * sti + 1].b * S[2 * sti + 2].d) % r;
S[sti].c = (S[2 * sti + 1].c * S[2 * sti + 2].a + S[2 * sti + 1].d * S[2 * sti + 2].c) % r;
S[sti].d = (S[2 * sti + 1].c * S[2 * sti + 2].b + S[2 * sti + 1].d * S[2 * sti + 2].d) % r;
}
}
matrix f2(int sti, int ll, int rr, int lq, int rq)
{
if (rq < ll || rr < lq)
return I;
else if (lq <= ll && rr <= rq)
return S[sti];
else
{
int m = (ll + rr) >> 1;
matrix AM = f2(2 * sti + 1, ll, m, lq, rq), BM = f2(2 * sti + 2, m + 1, rr, lq, rq);
matrix CM;
CM.a = (AM.a * BM.a + AM.b * BM.c) % r;
CM.b = (AM.a * BM.b + AM.b * BM.d) % r;
CM.c = (AM.c * BM.a + AM.d * BM.c) % r;
CM.d = (AM.c * BM.b + AM.d * BM.d) % r;
return CM;
}
}
signed main()
{
ferrario;
cin >> r >> n >> m;
I.a = I.d = 1, I.b = I.c = 0;
for (int i = 0; i < 4 * N; i++)
S[i] = I;
n_ = 1 << (__lg(n) + 1);
if (((n) & (n - 1)) == 0)
n_ >>= 1;
for (int i = 0; i < n; i++)
{
matrix x;
cin >> x.a >> x.b >> x.c >> x.d;
f1(0, 0, n_ - 1, i, x);
}
for (int i = 0; i < m; i++)
{
int lx, rx;
cin >> lx >> rx;
lx--, rx--;
matrix rs = f2(0, 0, n_ - 1, lx, rx);
cout << '\n';
cout << rs.a << ' ' << rs.b << '\n';
cout << rs.c << ' ' << rs.d << '\n';
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGludCBsb25nIGxvbmcKI2RlZmluZSBmZXJyYXJpbyBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApLCBjaW4udGllKDApCgpzdHJ1Y3QgbWF0cml4CnsKICAgIGludCBhLCBiLCBjLCBkOwp9OwoKY29uc3QgaW50IE4gPSAyZTUgKyA1OwppbnQgciwgbiwgbSwgbl87Cm1hdHJpeCBTWzQgKiBOXSwgSTsKCnZvaWQgZjEoaW50IHN0aSwgaW50IGxsLCBpbnQgcnIsIGludCBpeCwgY29uc3QgbWF0cml4ICZ4KQp7CiAgICBpZiAobGwgPT0gcnIpCiAgICB7CiAgICAgICAgU1tzdGldID0geDsKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBpbnQgbSA9IChsbCArIHJyKSA+PiAxOwogICAgICAgIGlmIChpeCA8PSBtKQogICAgICAgICAgICBmMSgyICogc3RpICsgMSwgbGwsIG0sIGl4LCB4KTsKICAgICAgICBlbHNlCiAgICAgICAgICAgIGYxKDIgKiBzdGkgKyAyLCBtICsgMSwgcnIsIGl4LCB4KTsKCiAgICAgICAgU1tzdGldLmEgPSAoU1syICogc3RpICsgMV0uYSAqIFNbMiAqIHN0aSArIDJdLmEgKyBTWzIgKiBzdGkgKyAxXS5iICogU1syICogc3RpICsgMl0uYykgJSByOwogICAgICAgIFNbc3RpXS5iID0gKFNbMiAqIHN0aSArIDFdLmEgKiBTWzIgKiBzdGkgKyAyXS5iICsgU1syICogc3RpICsgMV0uYiAqIFNbMiAqIHN0aSArIDJdLmQpICUgcjsKICAgICAgICBTW3N0aV0uYyA9IChTWzIgKiBzdGkgKyAxXS5jICogU1syICogc3RpICsgMl0uYSArIFNbMiAqIHN0aSArIDFdLmQgKiBTWzIgKiBzdGkgKyAyXS5jKSAlIHI7CiAgICAgICAgU1tzdGldLmQgPSAoU1syICogc3RpICsgMV0uYyAqIFNbMiAqIHN0aSArIDJdLmIgKyBTWzIgKiBzdGkgKyAxXS5kICogU1syICogc3RpICsgMl0uZCkgJSByOwogICAgfQp9CgptYXRyaXggZjIoaW50IHN0aSwgaW50IGxsLCBpbnQgcnIsIGludCBscSwgaW50IHJxKQp7CiAgICBpZiAocnEgPCBsbCB8fCByciA8IGxxKQogICAgICAgIHJldHVybiBJOwogICAgZWxzZSBpZiAobHEgPD0gbGwgJiYgcnIgPD0gcnEpCiAgICAgICAgcmV0dXJuIFNbc3RpXTsKICAgIGVsc2UKICAgIHsKICAgICAgICBpbnQgbSA9IChsbCArIHJyKSA+PiAxOwogICAgICAgIG1hdHJpeCBBTSA9IGYyKDIgKiBzdGkgKyAxLCBsbCwgbSwgbHEsIHJxKSwgQk0gPSBmMigyICogc3RpICsgMiwgbSArIDEsIHJyLCBscSwgcnEpOwogICAgICAgIG1hdHJpeCBDTTsKCiAgICAgICAgQ00uYSA9IChBTS5hICogQk0uYSArIEFNLmIgKiBCTS5jKSAlIHI7CiAgICAgICAgQ00uYiA9IChBTS5hICogQk0uYiArIEFNLmIgKiBCTS5kKSAlIHI7CiAgICAgICAgQ00uYyA9IChBTS5jICogQk0uYSArIEFNLmQgKiBCTS5jKSAlIHI7CiAgICAgICAgQ00uZCA9IChBTS5jICogQk0uYiArIEFNLmQgKiBCTS5kKSAlIHI7CgogICAgICAgIHJldHVybiBDTTsKICAgIH0KfQoKc2lnbmVkIG1haW4oKQp7CiAgICBmZXJyYXJpbzsKICAgIGNpbiA+PiByID4+IG4gPj4gbTsKICAgIEkuYSA9IEkuZCA9IDEsIEkuYiA9IEkuYyA9IDA7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IDQgKiBOOyBpKyspCiAgICAgICAgU1tpXSA9IEk7CgogICAgbl8gPSAxIDw8IChfX2xnKG4pICsgMSk7CiAgICBpZiAoKChuKSAmIChuIC0gMSkpID09IDApCiAgICAgICAgbl8gPj49IDE7CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICB7CiAgICAgICAgbWF0cml4IHg7CiAgICAgICAgY2luID4+IHguYSA+PiB4LmIgPj4geC5jID4+IHguZDsKICAgICAgICBmMSgwLCAwLCBuXyAtIDEsIGksIHgpOwogICAgfQoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgaSsrKQogICAgewogICAgICAgIGludCBseCwgcng7CiAgICAgICAgY2luID4+IGx4ID4+IHJ4OwogICAgICAgIGx4LS0sIHJ4LS07CiAgICAgICAgbWF0cml4IHJzID0gZjIoMCwgMCwgbl8gLSAxLCBseCwgcngpOwogICAgICAgIGNvdXQgPDwgJ1xuJzsKICAgICAgICBjb3V0IDw8IHJzLmEgPDwgJyAnIDw8IHJzLmIgPDwgJ1xuJzsKICAgICAgICBjb3V0IDw8IHJzLmMgPDwgJyAnIDw8IHJzLmQgPDwgJ1xuJzsKICAgIH0KCiAgICByZXR1cm4gMDsKfQ==