// ### test.cpp ### #include #ifdef __DEBUG_VECTOR namespace for_debugging{ struct subscript_and_location{ int sub; std::source_location loc; subscript_and_location(int sub_,std::source_location loc_=std::source_location::current()){ sub=sub_; loc=loc_; } void check_out_of_range(size_t sz){ if(sub<0||(int)sz<=sub){ std::clog << loc.file_name() << ":(" << loc.line() << ":" << loc.column() << "):" << loc.function_name() << std::endl; std::clog << "out of range: subscript = " << sub << ", vector_size = " << sz << std::endl; exit(EXIT_FAILURE); } } }; } namespace std{ template> class vector_for_debugging:public std::vector{ using std::vector::vector; public: [[nodiscard]] constexpr std::vector::reference operator[](for_debugging::subscript_and_location n) noexcept(!std::is_same::value){ n.check_out_of_range(this->size()); return std::vector::operator[](n.sub); } [[nodiscard]] constexpr std::vector::const_reference operator[](for_debugging::subscript_and_location n) const noexcept(!std::is_same::value){ n.check_out_of_range(this->size()); return std::vector::operator[](n.sub); } }; namespace pmr{ template using vector_for_debugging=std::vector_for_debugging>; } } #define vector vector_for_debugging #endif using namespace std; using ll = long long; using ull = unsigned long long; using sll = __int128_t; using db = double; using Pr = pair; using Pd = pair; using vi = vector; using vs = vector; using vc = vector; using vl = vector; using vb = vector; using vd = vector; using vp = vector; using vpd = vector; using vvi = vector>; using vvc = vector>; using vvl = vector>; using vvp = vector>; using vvb = vector>; using vvd = vector>; using vvs = vector>; using vvvi = vector>>; using vvvl = vector>>; using vvvb = vector>>; using vvvd = vector>>; using t3 = tuple; using t3d = tuple; using t4 = tuple; using vt3 = vector; using vt3d = vector; using vt4 = vector; using vvt3 = vector>; using vvt3d = vector>; using vvt4 = vector>; using pq = priority_queue,greater>; using cl = complex; using cd = complex; #define rep(i, N) for (ll i=0; i<(ll)(N); ++i) #define repr(i, N) for (ll i = (ll)(N) - 1; i >= 0; --i) #define repk(i, k, N) for (ll i = k; i < (ll)(N); ++i) #define rep1(i, N) for (ll i=1; i<(ll)(N+1); ++i) #define rep1r(i, N) for (ll i=(ll)(N); i>0; i--) #define all(v) (v).begin(), (v).end() #define allr(v) (v).rbegin(), (v).rend() #define SIZE(v) (ll)((v).size()) #define PYes {puts("Yes"); exit(0);} #define PNo {puts("No"); exit(0);} #define Pm0 {puts("0"); exit(0);} #define Pm1 {puts("-1"); exit(0);} #define INT(...) int __VA_ARGS__; in(__VA_ARGS__) #define INTM(...) int __VA_ARGS__; inm(__VA_ARGS__) #define LONG(...) ll __VA_ARGS__; in(__VA_ARGS__) #define LONGM(...) ll __VA_ARGS__; inm(__VA_ARGS__) #define DOUBLE(...) double __VA_ARGS__; in(__VA_ARGS__) #define CHAR(...) char __VA_ARGS__; in(__VA_ARGS__) #define STRING(...) string __VA_ARGS__; in(__VA_ARGS__) #define VI(ivec, n) vi ivec(n); input_ivec(ivec, n) #define VIM(ivec, n) vi ivec(n); input_ivecm(ivec, n) #define VL(lvec, n) vl lvec(n); input_lvec(lvec, n) #define VLM(lvec, n) vl lvec(n); input_lvecm(lvec, n) #define VL2(lvec1, lvec2, n) vl lvec1(n), lvec2(n); input_lvec12(lvec1, lvec2, n) #define VL2M(lvec1, lvec2, n) vl lvec1(n), lvec2(n); input_lvec12m(lvec1, lvec2, n) #define VC(cvec, n) vc cvec(n); input_cvec(cvec, n) #define VS(svec, n) vs svec(n); input_svec(svec, n) #define VD(dvec, n) vd dvec(n); input_dvec(dvec, n) #define VP(pvec, n) vp pvec(n); input_pvec(pvec, n) #define VPD(pvec, n) vpd pvec(n); input_pvecd(pvec, n) #define VPM(pvec, n) vp pvec(n); input_pvecm(pvec, n) #define VVI(ivec2, h, w) vvi ivec2(h, vi(w)); input_ivec2(ivec2, h, w) #define VVL(lvec2, h, w) vvl lvec2(h, vl(w)); input_lvec2(lvec2, h, w) #define VVLM(lvec2, h, w) vvl lvec2(h, vl(w)); input_lvec2m(lvec2, h, w) #define VVC(cvec2, h, w) vvc cvec2(h, vc(w)); input_cvec2(cvec2, h, w) #define pcnt(x) (ll)__builtin_popcountll(x) #define parity(x) (ll)__builtin_parityll(x) #define uset unordered_set #define umap unordered_map inline void Out(double x) {printf("%.15f",x);cout<<'\n';} template inline void Out(pair x) {cout< inline void Out(T x) {cout< v) {rep(i,SIZE(v)) cout< inline void Out(queue q){while(!q.empty()) {cout< inline void Out(deque q){while(!q.empty()) {cout< inline void Out(vector v) {rep(i,SIZE(v)) cout< inline void Out(vector> &vv){for(auto &v: vv) Out(v);} template inline void Out(vector> v) {for(auto p:v) Out(p);} template inline void Outend(T x) {Out(x); exit(0);} template inline void chmin(T &a, T b) { a = min(a, b); } template inline void chmax(T &a, T b) { a = max(a, b); } inline void mi(void) {return;} template void mi(T1& f, T2&... r) {--f; mi(r...);} template void in(T&... x) {(cin >> ... >> x);} template void inm(T&... x) {(cin >> ... >> x); mi(x...);} inline void input_ivec(vi &ivec, int n) {rep(i, n) {cin>>ivec[i];}} inline void input_ivecm(vi &ivec, int n) {rep(i, n) {cin>>ivec[i];--ivec[i];}} inline void input_lvec(vl &lvec, ll n) {rep(i, n) {cin>>lvec[i];}} inline void input_lvecm(vl &lvec, ll n) {rep(i, n) {cin>>lvec[i];--lvec[i];}} inline void input_lvec12(vl &lvec1, vl &lvec2, ll n) {rep(i, n) {cin>>lvec1[i]>>lvec2[i];}} inline void input_lvec12m(vl &lvec1, vl &lvec2, ll n) {rep(i, n) {cin>>lvec1[i]>>lvec2[i];--lvec1[i];--lvec2[i];}} inline void input_cvec(vc &cvec, ll n) {rep (i, n) {cin>>cvec[i];}} inline void input_svec(vs &svec, ll n) {rep (i, n) {cin>>svec[i];}} inline void input_dvec(vd &dvec, ll n) {rep (i, n) {cin>>dvec[i];}} inline void input_pvec(vp &pvec, ll n) {rep (i, n) {cin>>pvec[i].first>>pvec[i].second;}} inline void input_pvecm(vp &pvec, ll n) {rep (i, n) {cin>>pvec[i].first>>pvec[i].second;pvec[i].first--,pvec[i].second--;}} inline void input_pvecd(vpd &pvec, ll n) {rep (i, n) {cin>>pvec[i].first>>pvec[i].second;}} inline void input_ivec2(vvi &ivec2, int h, int w) {rep(i, h) rep(j, w) {cin>>ivec2[i][j];}} inline void input_lvec2(vvl &lvec2, ll h, ll w) {rep(i, h) rep(j, w) {cin>>lvec2[i][j];}} inline void input_lvec2m(vvl &lvec2, ll h, ll w) {rep(i, h) rep(j, w) {cin>>lvec2[i][j];--lvec2[i][j];}} inline void input_cvec2(vvc &cvec2, ll h, ll w) {rep(i, h) rep(j, w) {cin>>cvec2[i][j];}} inline bool isin(ll i, ll j, ll h, ll w) {if(i<0||i>=h||j<0||j>=w) return false; else return true;} inline sll TmpPercent(sll a, sll b) {if(b<0){a=-a,b=-b;} return (a%b+b)%b;} inline sll Percent(sll a, sll b) {if(b<0) return -TmpPercent(a,b); return TmpPercent(a,b);} inline sll Div(sll a, sll b) {if(b<0){a=-a,b=-b;} return (a-TmpPercent(a,b))/b; } inline sll Divceil(sll a, sll b) {if(TmpPercent(a,b)==0) return Div(a,b); return Div(a,b)+1;} template void erase(multiset &st, T x) {if(st.contains(x)) st.erase(st.find(x));} template T pop(vector &x) {T ret=x.back(); x.pop_back(); return ret;} #ifdef __DEBUG #define de(var) {cerr << #var << ": "; debug_view(var);} #define de2(var1,var2) {cerr<<#var1<<' '<<#var2<<": "; debug_view(var1,var2);} #define de3(var1,var2,var3) {cerr<<#var1<<' '<<#var2<<' '<<#var3<<": "; debug_view(var1,var2,var3);} #define de4(var1,var2,var3,var4) {cerr<<#var1<<' '<<#var2<<' '<<#var3<<' '<<#var4<<": "; debug_view(var1,var2,var3,var4);} #define de5(var1,var2,var3,var4,var5) {cerr<<#var1<<' '<<#var2<<' '<<#var3<<' '<<#var4<<' '<<#var5<<": "; debug_view(var1,var2,var3,var4,var5);} template inline void debug_view(T e){cerr << e << endl;} template inline void debug_view(T1 e1, T2 e2){cerr< inline void debug_view(T1 e1, T2 e2, T3 e3){cerr< inline void debug_view(T1 e1, T2 e2, T3 e3, T4 e4){cerr< inline void debug_view(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5){cerr< inline void debug_view(pair &p){cerr<<"{"< inline void debug_view(vector> &v){for(auto [a,b]: v){cerr<<"{"< inline void debug_view(set> &s){for(auto [a,b]: s){cerr<<"{"< inline void debug_view(tuple t){cerr<(t)<<' '<(t)<<' '<(t)<< endl;} template inline void debug_view(queue q){while(!q.empty()) {cerr << q.front() << " "; q.pop();}cerr << endl;} template inline void debug_view(deque q){while(!q.empty()) {cerr << q.front() << " "; q.pop_front();}cerr << endl;} template inline void debug_view(set s){for(auto x:s){cerr << x << ' ';}cerr << endl;} template inline void debug_view(set> s){for(auto x:s){cerr << x << ' ';}cerr << endl;} template inline void debug_view(unordered_set s){for(auto x:s){cerr << x << ' ';}cerr << endl;} template inline void debug_view(multiset s){for(auto x:s){cerr << x << ' ';}cerr << endl;} template inline void debug_view(multiset> s){for(auto x:s){cerr << x << ' ';}cerr << endl;} template inline void debug_view(vector> &v){for(auto [a,b]: v){cerr<<"{"< &v){cerr << "----" << endl; for(auto s: v) debug_view(s);} template inline void debug_view(vector &v){for(auto e: v){cerr << e << " ";} cerr << endl;} template inline void debug_view(vector>> &vv){cerr << "----" << endl;for(auto &v: vv){debug_view(v);} cerr << "--------" << endl;} template inline void debug_view(vector> &vv){cerr << "----" << endl;for(auto &v: vv){debug_view(v);} cerr << "--------" << endl;} template inline void debug_view(map &mp){cerr << "----" << endl;for(auto [k,v]: mp){cerr << k << ' ' << v << endl;} cerr << "--------" << endl;} template inline void debug_view(unordered_map &mp){cerr << "----" << endl;for(auto [k,v]: mp){cerr << k << ' ' << v << endl;} cerr << "--------" << endl;} template inline void debug_view(map> &mp){cerr<<"----"< inline void debug_view(map,T3> &mp){cerr << "----" << endl;for(auto [p,v]: mp){cerr<<'{'< inline void debugb_view(T e){bitset<20> b(e); cerr< inline void debugb_view(vector &v){cerr<<"----"< inline void ch1(T &x){if(x==INF)x=-1;} const double PI = acos(-1); const double EPS = 1e-8; //eg) if x=1e6, EPS >= 1e6/1e14(=1e-8) const vi di = {0, 1, 0, -1}; const vi dj = {1, 0, -1, 0}; const vp dij = {{0,1},{1,0},{0,-1},{-1,0}}; const vp hex0 = {{-1,-1},{-1,0},{0,-1},{0,1},{1,-1},{1,0}}; // tobide const vp hex1 = {{-1,0},{-1,1},{0,-1},{0,1},{1,0},{1,1}}; // hekomi const vi di8 = {-1, -1, -1, 0, 0, 1, 1, 1}; const vi dj8 = {-1, 0, 1, -1, 1, -1, 0, 1}; const vp dij8 = {{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; Pr operator+ (Pr a, Pr b) {return {a.first+b.first, a.second+b.second};} Pr operator- (Pr a, Pr b) {return {a.first-b.first, a.second-b.second};} Pr operator* (Pr a, Pr b) {return {a.first*b.first, a.second*b.second};} Pr operator/ (Pr a, Pr b) {return {a.first/b.first, a.second/b.second};} //! return {gcd(a,b), x, y}, where ax + by = gcd(a, b) //! IF a<0||b<0, gcd(a,b) COULD BE NEGATIVE VALUE!!! tuple extgcd(long long a, long long b) { if (b == 0) return make_tuple(a, 1, 0); auto [g, x, y] = extgcd(b, a%b); return make_tuple(g, y, x - a/b*y); } void solve() { LONG(D, a, b); if(a>b) swap(a,b); if(a==0) { ll ans = b*D; Out(ans); return; } auto [g,x0,y0] = extgcd(a,b); ll rh = a*a + b*b; if(rh%g!=0) { Out(0); return; } sll k = rh/g; sll ad = a/g, bd = b/g; sll mn = -INF*(sll)INF, mx = INF*(sll)INF; { sll l = Divceil(-k*x0,bd); sll r = Div(D-k*x0,bd); chmax(mn, l); chmin(mx, r); } { sll l = Divceil(k*y0-D,ad); sll r = Div(k*y0,ad); chmax(mn, l); chmin(mx, r); } // de2(ad,bd) // de2(mn,mx) if(mn>mx) { Out(0); return; } for(ll t=mn; t<=mx; ++t) { de2(ll(k*x0+bd*t), ll(k*y0-ad*t)) } ll ans = 0; auto calc=[&](sll t) { ll ret = b*k*x0 - a*k*y0 + (b*bd+a*ad)*t; return abs(ret); }; chmax(ans, calc(mn)); chmax(ans, calc(mx)); Out(ans); } int main () { // ios::sync_with_stdio(false); cin.tie(nullptr); LONG(T); rep(i, T) solve(); } // ### test.cpp ###