#include <iostream>
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;
#define MAX 500
#define INF 0x3f3f3f3f
int mmap[MAX][MAX];
int nodeMark[MAX];
#include<iostream>
#include<stdio.h>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
int arr[5000005],coun=0;
LL gcd(LL a,LL b){
  LL temp;
  while(a%b!=0){
    temp=a;
    a=b;
    b=temp%b;
  }
  return b;
}
LL fun(LL a,LL b,LL sum,bool iscir){
  LL tempa=arr[0],tempb=arr[1];
  int xxx=2;
  LL res=0;
  while(tempa<sum && tempb<sum){
    if(tempa>tempb)  res=res+(tempa-tempb)*abs(tempb%a-tempb%b);
    else res=res+(tempb-tempa)*abs(tempa%a-tempa%b);
    tempb<tempa ? tempb=arr[xxx++] : tempa=arr[xxx++];
  }
 // cout<<"*********"<<tempa<<‘ ‘<<tempb<<‘ ‘<<res<<"******\n"<<endl;
  //if(!iscir) sum++;
  if(tempa<sum)
    res=res+(sum-tempa)*abs(tempa%a-tempa%b);
  if(tempb<sum)
     res=res+(sum-tempb)*abs(tempb%a-tempb%b);
  //cout<<"========"<<res<<"========\n"<<endl;
  return res;
}
int main(){
  int T;
  LL A,B,N;
  LL res;
  scanf("%d",&T);
  while(T--){
    scanf("%I64d%I64d%I64d",&N,&A,&B);
    res=0;
    if(A==B){
        printf("0\n");
        continue;
    }
    LL cir=A*B/gcd(A,B);
    LL eend=min(cir,N);
    coun=0;
    for(int i=A;i<=eend;i+=A) arr[coun++]=i;
    for(int i=B;i<eend;i+=B) arr[coun++]=i;
    arr[coun++]=INF;
    sort(arr,arr+coun);
    if(cir<N)
      res+=(N/cir*fun(A,B,cir,1));
    res+=fun(A,B,N%cir,0);
    //if(N%cir) res--;
    //if(A==1||B==1) A==1 ? res-=(B-1) : res-=(A-1);
    printf("%I64d\n",res);
  }
return 0;
}