//愚直解 #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #define MAX_DATA 100000 #define ll long long int main(void) { int n, b, q; scanf("%d", &n); scanf("%d", &b); scanf("%d", &q); ll A[MAX_DATA][2][2]; for(int i = 0; i < n; ++i){ scanf("%lld", &A[i][0][0]); scanf("%lld", &A[i][0][1]); scanf("%lld", &A[i][1][0]); scanf("%lld", &A[i][1][1]); } int l[MAX_DATA]; int r[MAX_DATA]; ll x[MAX_DATA]; ll y[MAX_DATA]; for(int i = 0; i < q; ++i){ scanf("%d", &l[i]); scanf("%d", &r[i]); scanf("%lld", &x[i]); scanf("%lld", &y[i]); } for(int i = 0; i < q; ++i){ ll P[2][2] = {{1, 0}, {0, 1}}; for(int j = l[i]; j < r[i]; ++j){ ll Q[2][2] = {{P[0][0], P[0][1]}, {P[1][0], P[1][1]}}; P[0][0] = P[0][1] = P[1][0] = P[1][1] = 0; for(int ii = 0; ii < 2; ++ii){ for(int jj = 0; jj < 2; ++jj){ for(int kk = 0; kk < 2; ++kk){ P[ii][jj] += A[j][ii][kk] * Q[kk][jj]; P[ii][jj] %= b; } } } } ll vec[2] = { P[0][0] * x[i] + P[0][1] * y[i], P[1][0] * x[i] + P[1][1] * y[i] }; printf("%lld %lld\n", vec[0] % b, vec[1] % b); } return 0; }