#include using namespace std; #define fst(t) std::get<0>(t) #define snd(t) std::get<1>(t) #define thd(t) std::get<2>(t) using ll = long long; const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; ll babyStepGiantStep(ll a, ll b, ll p, ll l, ll r); template T expt(T a, T n, T mod = std::numeric_limits::max()); template T inverse(T n, T mod); std::tuple extgcd(ll a, ll b); ll P, R; int main(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cin >> P >> R; int Q; std::cin >> Q; for(int i=0;i> a >> b >> c; ll D = (b * b % P - 4ll * a % P * c % P) % P; D = D >= 0 ? D : D + P; ll sq; if(D == 0){ sq = 0; }else{ ll m = babyStepGiantStep(R, D, P, 0, P - 1); ll g, s; tie(g, s, ignore) = extgcd(2ll, P-1); if(m % g != 0){ std::cout << -1ll << std::endl; continue; } s = s * (m / g) % (P - 1); s = s >= 0 ? s : s + (P - 1); sq = expt(R, s, P); } ll den = inverse(2ll * a % P, P), x0 = (-b - sq) * den % P, x1 = (-b + sq) * den % P; x0 = x0 >= 0 ? x0 : x0 + P; x1 = x1 >= 0 ? x1 : x1 + P; if(x0 > x1){swap(x0, x1);} if(x0 == x1){ std::cout << x0 << std::endl; }else{ std::cout << x0 << " " << x1 << std::endl; } } } // solve a^n = b, 0 <= l <= n <= r < p (in F_p) ll babyStepGiantStep(ll a, ll b, ll p, ll l, ll r){ ll n = std::floor(std::sqrt(p)), alpha = expt(a, n, p); std::vector> expos(n); for(int i=0;i(*it) == v){ ll c = n * i + std::get<1>(*it); if(c <= r){ return c; } } } return -1ll; } std::tuple extgcd(ll a, ll b){ if(b == 0){ return std::make_tuple(a, 1ll, 0ll); } auto s = extgcd(b, a % b); return std::make_tuple(fst(s), thd(s), snd(s)-(a/b)*thd(s)); } template T expt(T a, T n, T mod){ T res = 1; while(n){ if(n & 1){res = res * a % mod;} a = a * a % mod; n >>= 1; } return res; } template T inverse(T n, T mod){ return expt(n, mod-2, mod); }