#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define endl '\n'
#define pb push_back
#define conts continue
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define ff first
#define ss second
#define ceil2(x,y) ((x+y-1) / y)
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#ifndef ONLINE_JUDGE
#define debug(x) cout << #x <<" = "; print(x); cout << endl;
#else
#define debug(x)
#endif
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
bool iseven(ll n) {if ((n & 1) == 0) return true; return false;}
void print(ll t) {cout << t;}
void print(int t) {cout << t;}
void print(string t) {cout << t;}
void print(char t) {cout << t;}
void print(double t) {cout << t;}
void print(ld t) {cout << t;}
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(pair <T, V> p) {cout << "{"; print(p.ff); cout << ","; print(p.ss); cout << "}";}
template <class T> void print(vector <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T> void print(set <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T> void print(multiset <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T, class V> void print(map <T, V> v) {cout << "[ "; for (auto i : v) {print(i); cout << " ";} cout << "]";}
void usaco(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
int MOD = 1e9 + 7;
const int maxn = 1e5 + 5;
const int inf1 = 1e9 + 5;
const ll inf2 = 1e18 + 5;
template<typename T, int sp>
struct segtree {
/*=======================================================*/
struct data {
int a, b, c, d;
};
data neutral = {1, 0, 0, 1};
void merge(data &curr, data &left, data &right) {
/*
[a1,b1] * [a2,b2] = [a1a2 + b1c2, a1b2 + b1d2]
[c1,d1] [c2,d2] [c1a2 + d1c2, c1b2 + d1d2]
*/
auto [a1, b1, c1, d1] = left;
auto [a2, b2, c2, d2] = right;
int a3, b3, c3, d3;
a3 = a1 * a2 + b1 * c2;
b3 = a1 * b2 + b1 * d2;
c3 = c1 * a2 + d1 * c2;
d3 = c1 * b2 + d1 * d2;
a3 %= MOD, b3 %= MOD, c3 %= MOD, d3 %= MOD;
curr = {a3, b3, c3, d3};
}
void create(int x, int lx, int rx, T v) {
auto [a, b, c, d] = v;
tr[x] = {a, b, c, d};
}
void modify(int x, int lx, int rx, T v) {
}
/*=======================================================*/
int siz = 1;
vector<data> tr;
segtree(int n) {
init(n);
}
segtree() {
};
void init(int n) {
while (siz < n) siz *= 2;
tr.assign(2 * siz, neutral);
}
void build(vector<T> &a, int n, int x, int lx, int rx) {
if (rx - lx == 1) {
if (lx >= sp and lx < n) {
create(x, lx, rx, a[lx]);
}
return;
}
int mid = (lx + rx) / 2;
build(a, n, 2 * x + 1, lx, mid);
build(a, n, 2 * x + 2, mid, rx);
merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
}
void build(vector<T> &a, int n) {
build(a, n, 0, 0, siz);
}
void pupd(int i, T v, int x, int lx, int rx) {
if (rx - lx == 1) {
modify(x, lx, rx, v);
return;
}
int mid = (lx + rx) / 2;
if (i < mid) {
pupd(i, v, 2 * x + 1, lx, mid);
}
else {
pupd(i, v, 2 * x + 2, mid, rx);
}
merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
}
void pupd(int i, T v) {
pupd(i, v, 0, 0, siz);
}
data query(int l, int r, int x, int lx, int rx) {
if (lx >= r or rx <= l) return neutral;
if (lx >= l and rx <= r) return tr[x];
int mid = (lx + rx) / 2;
data curr;
data left = query(l, r, 2 * x + 1, lx, mid);
data right = query(l, r, 2 * x + 2, mid, rx);
merge(curr, left, right);
return curr;
}
data query(int l, int r) {
return query(l, r + 1, 0, 0, siz);
}
};
void solve(int test_case)
{
cin >> MOD;
int n, m; cin >> n >> m;
vector<array<int, 4>> a(n + 5);
rep1(i, n) {
rep(j, 4) cin >> a[i][j], a[i][j] %= MOD;
}
segtree<array<int, 4>, 1> st(n + 5);
st.build(a, n + 1);
while (m--) {
int l, r; cin >> l >> r;
auto ans = st.query(l, r);
cout << ans.a << " " << ans.b << endl;
cout << ans.c << " " << ans.d << endl;
cout << endl;
}
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIG5hbWVzcGFjZSBfX2dudV9wYmRzOwoKdGVtcGxhdGUgPGNsYXNzIFQ+IHVzaW5nIFRyZWUgPSB0cmVlPFQsIG51bGxfdHlwZSwgbGVzczxUPiwgcmJfdHJlZV90YWcsIHRyZWVfb3JkZXJfc3RhdGlzdGljc19ub2RlX3VwZGF0ZT47Cgp0eXBlZGVmIGxvbmcgbG9uZyBpbnQgbGw7CnR5cGVkZWYgbG9uZyBkb3VibGUgbGQ7CgojZGVmaW5lIGZhc3RpbyBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsgY2luLnRpZShOVUxMKQojZGVmaW5lIGVuZGwgJ1xuJwojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIGNvbnRzIGNvbnRpbnVlCiNkZWZpbmUgYWxsKGEpIGEuYmVnaW4oKSxhLmVuZCgpCiNkZWZpbmUgcmFsbChh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mlsZW5hbWUgKyAiLm91dCIpLmNfc3RyKCksICJ3Iiwgc3Rkb3V0KTsKfQoKaW50IE1PRCA9IDFlOSArIDc7CmNvbnN0IGludCBtYXhuID0gMWU1ICsgNTsKY29uc3QgaW50IGluZjEgPSAxZTkgKyA1Owpjb25zdCBsbCBpbmYyID0gMWUxOCArIDU7Cgp0ZW1wbGF0ZTx0eXBlbmFtZSBULCBpbnQgc3A+CnN0cnVjdCBzZWd0cmVlIHsKICAgIC8qPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PSovCgogICAgc3RydWN0IGRhdGEgewogICAgICAgIGludCBhLCBiLCBjLCBkOwogICAgfTsKCiAgICBkYXRhIG5ldXRyYWwgPSB7MSwgMCwgMCwgMX07CgogICAgdm9pZCBtZXJnZShkYXRhICZjdXJyLCBkYXRhICZsZWZ0LCBkYXRhICZyaWdodCkgewogICAgICAgIC8qCiAgICAgICAgW2ExLGIxXSAqIFthMixiMl0gPSBbYTFhMiArIGIxYzIsIGExYjIgKyBiMWQyXQogICAgICAgIFtjMSxkMV0gICBbYzIsZDJdICAgW2MxYTIgKyBkMWMyLCBjMWIyICsgZDFkMl0KICAgICAgICAqLwoKICAgICAgICBhdXRvIFthMSwgYjEsIGMxLCBkMV0gPSBsZWZ0OwogICAgICAgIGF1dG8gW2EyLCBiMiwgYzIsIGQyXSA9IHJpZ2h0OwoKICAgICAgICBpbnQgYTMsIGIzLCBjMywgZDM7CgogICAgICAgIGEzID0gYTEgKiBhMiArIGIxICogYzI7CiAgICAgICAgYjMgPSBhMSAqIGIyICsgYjEgKiBkMjsKICAgICAgICBjMyA9IGMxICogYTIgKyBkMSAqIGMyOwogICAgICAgIGQzID0gYzEgKiBiMiArIGQxICogZDI7CgogICAgICAgIGEzICU9IE1PRCwgYjMgJT0gTU9ELCBjMyAlPSBNT0QsIGQzICU9IE1PRDsKICAgICAgICBjdXJyID0ge2EzLCBiMywgYzMsIGQzfTsKICAgIH0KCiAgICB2b2lkIGNyZWF0ZShpbnQgeCwgaW50IGx4LCBpbnQgcngsIFQgdikgewogICAgICAgIGF1dG8gW2EsIGIsIGMsIGRdID0gdjsKICAgICAgICB0clt4XSA9IHthLCBiLCBjLCBkfTsKICAgIH0KCiAgICB2b2lkIG1vZGlmeShpbnQgeCwgaW50IGx4LCBpbnQgcngsIFQgdikgewoKICAgIH0KCiAgICAvKj09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0qLwoKICAgIGludCBzaXogPSAxOwogICAgdmVjdG9yPGRhdGE+IHRyOwoKICAgIHNlZ3RyZWUoaW50IG4pIHsKICAgICAgICBpbml0KG4pOwogICAgfQoKICAgIHNlZ3RyZWUoKSB7CgogICAgfTsKCiAgICB2b2lkIGluaXQoaW50IG4pIHsKICAgICAgICB3aGlsZSAoc2l6IDwgbikgc2l6ICo9IDI7CiAgICAgICAgdHIuYXNzaWduKDIgKiBzaXosIG5ldXRyYWwpOwogICAgfQoKICAgIHZvaWQgYnVpbGQodmVjdG9yPFQ+ICZhLCBpbnQgbiwgaW50IHgsIGludCBseCwgaW50IHJ4KSB7CiAgICAgICAgaWYgKHJ4IC0gbHggPT0gMSkgewogICAgICAgICAgICBpZiAobHggPj0gc3AgYW5kIGx4IDwgbikgewogICAgICAgICAgICAgICAgY3JlYXRlKHgsIGx4LCByeCwgYVtseF0pOwogICAgICAgICAgICB9CgogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQoKICAgICAgICBpbnQgbWlkID0gKGx4ICsgcngpIC8gMjsKCiAgICAgICAgYnVpbGQoYSwgbiwgMiAqIHggKyAxLCBseCwgbWlkKTsKICAgICAgICBidWlsZChhLCBuLCAyICogeCArIDIsIG1pZCwgcngpOwoKICAgICAgICBtZXJnZSh0clt4XSwgdHJbMiAqIHggKyAxXSwgdHJbMiAqIHggKyAyXSk7CiAgICB9CgogICAgdm9pZCBidWlsZCh2ZWN0b3I8VD4gJmEsIGludCBuKSB7CiAgICAgICAgYnVpbGQoYSwgbiwgMCwgMCwgc2l6KTsKICAgIH0KCiAgICB2b2lkIHB1cGQoaW50IGksIFQgdiwgaW50IHgsIGludCBseCwgaW50IHJ4KSB7CiAgICAgICAgaWYgKHJ4IC0gbHggPT0gMSkgewogICAgICAgICAgICBtb2RpZnkoeCwgbHgsIHJ4LCB2KTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KCiAgICAgICAgaW50IG1pZCA9IChseCArIHJ4KSAvIDI7CgogICAgICAgIGlmIChpIDwgbWlkKSB7CiAgICAgICAgICAgIHB1cGQoaSwgdiwgMiAqIHggKyAxLCBseCwgbWlkKTsKICAgICAgICB9CiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIHB1cGQoaSwgdiwgMiAqIHggKyAyLCBtaWQsIHJ4KTsKICAgICAgICB9CgogICAgICAgIG1lcmdlKHRyW3hdLCB0clsyICogeCArIDFdLCB0clsyICogeCArIDJdKTsKICAgIH0KCiAgICB2b2lkIHB1cGQoaW50IGksIFQgdikgewogICAgICAgIHB1cGQoaSwgdiwgMCwgMCwgc2l6KTsKICAgIH0KCiAgICBkYXRhIHF1ZXJ5KGludCBsLCBpbnQgciwgaW50IHgsIGludCBseCwgaW50IHJ4KSB7CiAgICAgICAgaWYgKGx4ID49IHIgb3IgcnggPD0gbCkgcmV0dXJuIG5ldXRyYWw7CiAgICAgICAgaWYgKGx4ID49IGwgYW5kIHJ4IDw9IHIpIHJldHVybiB0clt4XTsKCiAgICAgICAgaW50IG1pZCA9IChseCArIHJ4KSAvIDI7CgogICAgICAgIGRhdGEgY3VycjsKICAgICAgICBkYXRhIGxlZnQgPSBxdWVyeShsLCByLCAyICogeCArIDEsIGx4LCBtaWQpOwogICAgICAgIGRhdGEgcmlnaHQgPSBxdWVyeShsLCByLCAyICogeCArIDIsIG1pZCwgcngpOwoKICAgICAgICBtZXJnZShjdXJyLCBsZWZ0LCByaWdodCk7CiAgICAgICAgcmV0dXJuIGN1c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