#include using namespace std; using ll = long long; int mod_pow(int a, int b, int m) { int ret = 1; while (b) { if (b & 1) ret = (ll)ret * a % m; a = (ll)a * a % m; b >>= 1; } return ret; } bool is_square(int a, int p) { return mod_pow(a, (p - 1) >> 1, p) == 1; } int mod_sqrt(int a, int p) { if (a == 0) return 0; if (p == 2) return a; if (!is_square(a, p)) return -1; int b = 2; while (is_square(((ll)b * b - a + p) % p, p)) b++; int w = ((ll)b * b - a + p) % p; auto mul = [&](pair u, pair v) { int a = ((ll)u.first * v.first + (ll)u.second * v.second % p * w) % p; int b = ((ll)u.first * v.second + (ll)u.second * v.first) % p; return make_pair(a, b); }; // (b + sqrt(b^2-a))^(p+1)/2 int e = (p + 1) / 2; auto ret = make_pair(1, 0); auto v = make_pair(b, 1); while (e) { if (e & 1) ret = mul(ret, v); v = mul(v, v); e >>= 1; } return ret.first; } ll mod_inv(ll a, ll md = 1e9 + 7) { ll b = md, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= md; if (u < 0) u += md; return u; } int P; pair solve(ll a, ll b, ll c) { a = a * 2 % P; c = c * 2 % P; int rt = mod_sqrt((b * b % P + P - a * c % P) % P, P); if (rt == -1) return make_pair(-1, -1); pair res(rt, (P - rt) % P); res.first = (res.first + P - b) % P * mod_inv(a, P) % P; res.second = (res.second + P - b) % P * mod_inv(a, P) % P; if (res.first > res.second) swap(res.first, res.second); return res; } int main() { ios::sync_with_stdio(false), cin.tie(0); int R, Q; cin >> P >> R >> Q; while (Q--) { int A, B, C; cin >> A >> B >> C; auto res = solve(A, B, C); if (res.first == -1) { printf("%d\n", -1); } else if (res.first == res.second) { printf("%d\n", res.first); } else { printf("%d %d\n", res.first, res.second); } } return 0; }