#include int gcd(int a, int b) { if (a == 0) return b; else return gcd(b % a, a); } long long solve(int D, int x, int y) { int l = 0, r = D, m, g = gcd(x, y); long long xx, yy; if (x < y) { x ^= y; y ^= x; x ^= y; } while (l < r) { m = (l + r + 1) / 2; xx = x - (long long)y / g * m; yy = y + (long long)x / g * m; if (xx >= 0 && yy <= D) l = m; else r = m - 1; } long long ans = (long long)x * x + (long long)y * y; return ans * l / g; } int main() { int T, D, x, y; scanf("%d", &T); while (T--) { scanf("%d %d %d", &D, &x, &y); printf("%lld\n", solve(D, x, y)); } fflush(stdout); return 0; }