#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
long long gcd(long long a,long long b)
{
return b ? gcd(b, a % b) : a;
}
long long Count(long long n,long long a,long long b)
{
long long now = 0, ret = 0, tmp, x = 0, y = 0;
while(now < n)
{
tmp = min(a - x, b - y);
if (now + tmp > n)
{
tmp = n - now;
}
ret += tmp * abs(x - y);
x = (x + tmp) % a;
y = (y + tmp) % b;
now += tmp;
}
return ret;
}
int main()
{
std::ios::sync_with_stdio(false);
int i, j, t;
long long a,b,n,l,ans;
cin >> t;
while(t--)
{
cin >> n >> a >> b;
l = a * b / gcd(a,b);
if (l >= n)
{
ans = Count(n, a, b);
}
else
{
ans = Count(l, a, b) * (n / l) + Count(n % l, a, b);
}
cout << ans << endl;
}
return 0;
}