#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;
}