import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0; i < t; i++){ long n = sc.nextLong(); long a = sc.nextLong(); long b = sc.nextLong(); long na = floorSum(n/a, 1, a, n - a * (n/a)); long nb = floorSum(n, b, 1, 0); long nab = floorSum(n/a, b, a, n - a * (n/a)); long ans = na + nb - nab; System.out.println(ans); } } public static long floorSum(long n, long m, long a, long b){ long ret = 0; if(a >= m){ ret += (n - 1) * n * (a/m)/2; a %= m; } if(b >= m){ ret += n * (b/m); b %= m; } long ym = (a * n + b) / m; long xm = (ym * m - b); if(ym == 0) return ret; ret += (n - (xm + a - 1)/a) * ym; ret += floorSum(ym, a, m, (a - xm % a) % a); return ret; } }