// package com.company;
import java.util.*;
import java.lang.*;
import java.io.*;
//****Use Integer Wrapper Class for Arrays.sort()****
public class EK5 {
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
    public static void main(String[] Args)throws Exception{
        FastReader scan=new FastReader(System.in);
        int t=1;
        t=scan.nextInt();
        while(t-->0){
            long m=scan.nextInt();
            long d=scan.nextInt();
            long w=scan.nextInt();
            long from=Math.min(d,m);
            if(d-1==0){
                out.println((from*(from-1))/2);
            }else{
                long gcd=gcd(d-1,w);
                long min=w/gcd;
                long l=1;
                long r=(long)1e9+1;
                long in=-1;
                while(l<=r){
                    long mid=(l+r)/2;
                    if(min*mid<from){
                        in=mid;
                        l=mid+1;
                    }else{
                        r=mid-1;
                    }
                }
                if(in==-1){
                    out.println(0);
                }else{
                    out.println(in*from-min*((in*(in+1))/2));
                }
            }
        }
        out.flush();
        out.close();
    }
    public static long gcd(long a,long b){
        if(a==0||b==0){
            return 1;
        }
        if(a%b==0||b%a==0){
            return a%b==0?b:a;
        }
        return a>b?gcd(b,a%b):gcd(a,b%a);
    }
    static class FastReader {

        byte[] buf = new byte[2048];
        int index, total;
        InputStream in;

        FastReader(InputStream is) {
            in = is;
        }

        int scan() throws IOException {
            if (index >= total) {
                index = 0;
                total = in.read(buf);
                if (total <= 0) {
                    return -1;
                }
            }
            return buf[index++];
        }

        String next() throws IOException {
            int c;
            for (c = scan(); c <= 32; c = scan()) ;
            StringBuilder sb = new StringBuilder();
            for (; c > 32; c = scan()) {
                sb.append((char) c);
            }
            return sb.toString();
        }

        int nextInt() throws IOException {
            int c, val = 0;
            for (c = scan(); c <= 32; c = scan()) ;
            boolean neg = c == '-';
            if (c == '-' || c == '+') {
                c = scan();
            }
            for (; c >= '0' && c <= '9'; c = scan()) {
                val = (val << 3) + (val << 1) + (c & 15);
            }
            return neg ? -val : val;
        }
        long nextLong() throws IOException {
            int c;
            long val = 0;
            for (c = scan(); c <= 32; c = scan()) ;
            boolean neg = c == '-';
            if (c == '-' || c == '+') {
                c = scan();
            }
            for (; c >= '0' && c <= '9'; c = scan()) {
                val = (val << 3) + (val << 1) + (c & 15);
            }
            return neg ? -val : val;
        }
    }
}
//


// package cf;

import java.util.*;
import java.io.*;
public class calendar {
    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        String next() { // reads in the next string
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() { // reads in the next int
            return Integer.parseInt(next());
        }

        public long nextLong() { // reads in the next long
            return Long.parseLong(next());
        }

        public double nextDouble() { // reads in the next double
            return Double.parseDouble(next());
        }
    }
    static PrintWriter pw = new PrintWriter(System.out);
    static InputReader r = new InputReader(System.in);

    public static void main(String[] args){
        int n=r.nextInt();
        for(int i=0; i<n; i++){
            int a=r.nextInt(), b=r.nextInt(), c=r.nextInt();
            int m=Math.min(a,b);
            int k=gcd(b-1,c);
            k=c/k;

            int d=m%k;
            int e=m/k;
            long count = k*((e)*(e+1))/2;
            count -= (k-d)*e;
            pw.println(count);
        }
        pw.close();
    }

    public static int gcd(int a, int b){
        if(a==0 || b==0){
            if(a==0){
                return b;
            }
            else{
                return a;
            }
        }
        else{
            if(a>=b){
                a -= b*(a/b);
                return gcd(a,b);
            }
            else{
                b -= a*(b/a);
                return gcd(a,b);
            }
        }
    }
}


import java.util.*;
import java.io.*;
public class calendar {
    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        String next() { // reads in the next string
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() { // reads in the next int
            return Integer.parseInt(next());
        }

        public long nextLong() { // reads in the next long
            return Long.parseLong(next());
        }

        public double nextDouble() { // reads in the next double
            return Double.parseDouble(next());
        }
    }
    static PrintWriter pw = new PrintWriter(System.out);
    static InputReader r = new InputReader(System.in);

    public static void main(String[] args){
        int n=r.nextInt();
        for(int i=0; i<n; i++){
            int a=r.nextInt(), b=r.nextInt(), c=r.nextInt();
            int m=Math.min(a,b);
            int k=gcd(b-1,c);
            k=c/k;

            int count=0;
            for(int x=1; x<=m; x++){
                if(Math.max(0,(m-x)/k)==0){
                    break;
                }
                count+= Math.max(0, (m-x)/k);
            }
            pw.println(count);
        }
        pw.close();
    }

    public static int gcd(int a, int b){
        if(a==0 || b==0){
            if(a==0){
                return b;
            }
            else{
                return a;
            }
        }
        else{
            if(a>=b){
                a -= b*(a/b);
                return gcd(a,b);
            }
            else{
                b -= a*(b/a);
                return gcd(a,b);
            }
        }
    }
}


// package cf;

import java.util.*;
import java.io.*;
public class calendar {
    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        String next() { // reads in the next string
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() { // reads in the next int
            return Integer.parseInt(next());
        }

        public long nextLong() { // reads in the next long
            return Long.parseLong(next());
        }

        public double nextDouble() { // reads in the next double
            return Double.parseDouble(next());
        }
    }
    static PrintWriter pw = new PrintWriter(System.out);
    static InputReader r = new InputReader(System.in);

    public static void main(String[] args){
        int n=r.nextInt();
        for(int i=0; i<n; i++){
            int a=r.nextInt(), b=r.nextInt(), c=r.nextInt();
            int m=Math.min(a,b);
            long k=gcd(b-1,c);
            k=c/k;

            long d=m%k;
            long e=m/k;
            long count = k*((e)*(e+1))/2;
            count -= (k-d)*e;
            pw.println(count);
        }
        pw.close();
    }

    public static int gcd(int a, int b){
        if(a==0 || b==0){
            if(a==0){
                return b;
            }
            else{
                return a;
            }
        }
        else{
            if(a>=b){
                a -= b*(a/b);
                return gcd(a,b);
            }
            else{
                b -= a*(b/a);
                return gcd(a,b);
            }
        }
    }
}


//package cf;

import java.util.*;
import java.io.*;
public class calendar {
    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        String next() { // reads in the next string
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() { // reads in the next int
            return Integer.parseInt(next());
        }

        public long nextLong() { // reads in the next long
            return Long.parseLong(next());
        }

        public double nextDouble() { // reads in the next double
            return Double.parseDouble(next());
        }
    }
    static PrintWriter pw = new PrintWriter(System.out);
    static InputReader r = new InputReader(System.in);

    public static void main(String[] args){
        int n=r.nextInt();
        for(int i=0; i<n; i++){
            int a=r.nextInt(), b=r.nextInt(), c=r.nextInt();
            int m=Math.min(a,b);
            int k=gcd(b-1,c);
            k=c/k;

            int count=0;
            for(int x=1; x<=m; x++){
                count+= Math.max(0, (m-x)/k);
            }
            pw.println(count);
        }
        pw.close();
    }

    public static int gcd(int a, int b){
        if(a==0 || b==0){
            if(a==0){
                return b;
            }
            else{
                return a;
            }
        }
        else{
            if(a>=b){
                a -= b*(a/b);
                return gcd(a,b);
            }
            else{
                b -= a*(b/a);
                return gcd(a,b);
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        FastReader sc = new FastReader();
        int tt = sc.nextInt();
        while(tt-- > 0){
            int m = sc.nextInt();
            int d = sc.nextInt();
            int w = sc.nextInt();

            if(d == 1){
                System.out.println(0);
                continue;
            }
            --d; // 0 based index

            // derived formula : (d - 1) * (x - y) = 0 (mod w)
            //removing common factors from (d - 1) and w
            int g = gcd(d, w);
            int w1 = w / g;

            // x = y ( mod w1)
            // z = k * w1 + r
            int z = Math.min(d + 1, m);
            int k = z / w1;
            int r = z % w1;

            long ans = ((k * (long)(k - 1)) / 2) * w1;
            ans += (r * k);
            System.out.println(ans);

        }
    }

    private static int gcd(int a, int b){
        if (a == 0)
            return b;
        return gcd(b%a, a);
    }

    private static class FastReader {
        private BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        FastReader sc = new FastReader();
        int tt = sc.nextInt();
        while(tt-- > 0){
            int m = sc.nextInt();
            int d = sc.nextInt();
            int w = sc.nextInt();

            if(d == 1){
                System.out.println(0);
                continue;
            }
            --d; // 0 based index

            // derived formula : (d - 1) * (x - y) = 0 (mod w)
            //removing common factors from (d - 1) and w
            int g = gcd(d, w);
            int w1 = w / g;

            // x = y ( mod w1)
            // z = k * w1 + r
            int z = Math.min(d + 1, m);
            int k = z / w1;
            int r = z % w1;

            int ans = (int)((k * (long)(k - 1)) / 2) * w1;
            ans += (r * k);
            System.out.println(ans);

        }
    }

    private static int gcd(int a, int b){
        if (a == 0)
            return b;
        return gcd(b%a, a);
    }

    private static class FastReader {
        private BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
     static class FastReader{
	BufferedReader br;
	StringTokenizer st;
      public FastReader(){
         br=new BufferedReader(new InputStreamReader(System.in));
         }
      public FastReader(String s)throws FileNotFoundException{
        br=new BufferedReader(new FileReader(new File(s)));
        }
       String next(){
          while(st==null || !st.hasMoreElements()){
            try{
                  st=new StringTokenizer(br.readLine());
                  }
            catch(IOException e){
                   e.printStackTrace();
		}

               }
             return st.nextToken();
         }
        String nextLine(){
             String str="";
             try{
                 str=br.readLine();
                 }
            catch(IOException e){
              e.printStackTrace();
                     }
                return str;
           }
	int nextInt(){
            return Integer.parseInt(next());
                }
         long nextLong(){
            return Long.parseLong(next());
             }
         double nextDouble(){
            return Double.parseDouble(next());

            }
  }
   public static long gcd(long a,long b){
      if(a==0) return b;
      if(b==0) return a;
      while(b!=0){
         long t=b;
         b=a%b;
         a=t;
      }
      return a;
   }
  public static void main(String[] args)throws IOException {
     FastReader in=new FastReader();
      int T=in.nextInt();
       while(T-->0){
          long m=in.nextLong(); long d=in.nextLong();
          long w=in.nextLong();
          long diff=w/gcd((d-1),w);
          long mn=Math.min(m,d);
          long n=mn/diff;
          System.out.println((2*(mn-diff)-diff*(n-1))*n/2);
       }
  }
}


import java.util.*;
import java.io.*;

public class Solution{
    public static class pair{
        int x;
        int y;
        int z;
    }
    public static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
        public String next() {
			while (st == null || !st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		}public double nextDouble() {
			return Double.parseDouble(next());
		}
    }

    public static void main(String[] args) throws IOException{
        FastScanner fs=new FastScanner();
		int t=fs.nextInt();
		PrintWriter out=new PrintWriter(System.out);
		while(t-->0){
		    int m=fs.nextInt();
		    int d=fs.nextInt();
		    int w=fs.nextInt();
		    int wd=gcd(d-1,w);
		    int min=Math.min(m,d);
		    wd=w/wd;
		    int val=min/wd;
		    long ans=(long)val*(long)min;
		    long f=(val*(val+1))/2;
		    ans-=(wd*f);
		    System.out.println(ans);
		}
        out.close();
    }
    public static long east(int l1,int r1,int l2,int r2,int n,int k){
        if(n==0){
            return Integer.MAX_VALUE;
        }
        if(l2>=l1&&r2<=r1){
		        long cost=(l2-l1)+(r1-r2);
		        if(k<=(r1-l1)){
		            return (long)k;
		        }
		        k-=(r1-l1);
		        cost=Math.min(2*k,east(l1,r1,l2,r2,n-1,k))+cost;
		        return cost;
		    }else if(r1<l2){
		        long pc=l2-r1;
		        long cost=(r1-l1)+(r2-r1);
		        if(k<=(r2-l1)){
		            return (long)k+pc;
		        }
		        k-=(r2-l1);
		        cost=Math.min(2*k,east(l1,r1,l2,r2,n-1,k))+pc+cost;
		        return cost;
		    }else{
		        long cost=(l2-l1)+(r2-r1);
		        if(k<=(r2-l1)){
		            return k;
		        }
		        k-=(r2-l1);
		        cost=Math.min(2*k,east(l1,r1,l2,r2,n-1,k))+cost;
		        return cost;
		    }
    }
    static long maxSubArraySum(int a[])
    {
        int size = a.length;
        long max_so_far = Long.MIN_VALUE, max_ending_here = 0;
        int max=Integer.MIN_VALUE;
        for (int i = 0; i < size; i++)
        {
            max_ending_here = max_ending_here + a[i];
            if (max_so_far < max_ending_here)
                max_so_far = max_ending_here;
            if (max_ending_here < 0)
                max_ending_here = 0;
            max=Math.max(max,a[i]);
        }
        if(max<0){
            return (long)0;
        }
        return max_so_far;
    }
    public static class Comp implements Comparator<pair>{
        public int compare(pair a,pair b){
            if(a.x!=b.x){
                return b.x-a.x;
            }else{
                return a.y-b.y;
            }
        }
    }
    public static int gcd(int a,int b){
        if (b == 0)
        return a;
        return gcd(b, a % b);
    }
    public static int lcm(int a,int b){
        int x=Math.max(a,b);
        int y=Math.min(a,b);
        long ans=x;
        while(ans%y!=0){
            ans+=x;
        }
        if(ans>Integer.MAX_VALUE){
            return -1;
        }
        return (int)ans;
    }
    public static long fact(int n){
        long ans=1;
        for(int i=1;i<=n;i++){
            ans*=i;
        }
        return ans;
    }
}


import java.util.*;
import java.io.*;

public class Solution{
    public static class pair{
        int x;
        int y;
        int z;
    }
    public static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
        public String next() {
			while (st == null || !st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		}public double nextDouble() {
			return Double.parseDouble(next());
		}
    }

    public static void main(String[] args) throws IOException{
        FastScanner fs=new FastScanner();
		int t=fs.nextInt();
		PrintWriter out=new PrintWriter(System.out);
		while(t-->0){
		    int m=fs.nextInt();
		    int d=fs.nextInt();
		    int w=fs.nextInt();
		    long wd=gcd((long)(d-1),(long)w);
		    int min=Math.min(m,d);
		    wd=w/wd;
		    long val=(long)min/(long)wd;
		    long ans=(long)val*(long)min;
		    long f=(val*(val+1))/2;
		    ans-=(wd*f);
		    System.out.println(ans);
		}
        out.close();
    }
    public static long east(int l1,int r1,int l2,int r2,int n,int k){
        if(n==0){
            return Integer.MAX_VALUE;
        }
        if(l2>=l1&&r2<=r1){
		        long cost=(l2-l1)+(r1-r2);
		        if(k<=(r1-l1)){
		            return (long)k;
		        }
		        k-=(r1-l1);
		        cost=Math.min(2*k,east(l1,r1,l2,r2,n-1,k))+cost;
		        return cost;
		    }else if(r1<l2){
		        long pc=l2-r1;
		        long cost=(r1-l1)+(r2-r1);
		        if(k<=(r2-l1)){
		            return (long)k+pc;
		        }
		        k-=(r2-l1);
		        cost=Math.min(2*k,east(l1,r1,l2,r2,n-1,k))+pc+cost;
		        return cost;
		    }else{
		        long cost=(l2-l1)+(r2-r1);
		        if(k<=(r2-l1)){
		            return k;
		        }
		        k-=(r2-l1);
		        cost=Math.min(2*k,east(l1,r1,l2,r2,n-1,k))+cost;
		        return cost;
		    }
    }
    static long maxSubArraySum(int a[])
    {
        int size = a.length;
        long max_so_far = Long.MIN_VALUE, max_ending_here = 0;
        int max=Integer.MIN_VALUE;
        for (int i = 0; i < size; i++)
        {
            max_ending_here = max_ending_here + a[i];
            if (max_so_far < max_ending_here)
                max_so_far = max_ending_here;
            if (max_ending_here < 0)
                max_ending_here = 0;
            max=Math.max(max,a[i]);
        }
        if(max<0){
            return (long)0;
        }
        return max_so_far;
    }
    public static class Comp implements Comparator<pair>{
        public int compare(pair a,pair b){
            if(a.x!=b.x){
                return b.x-a.x;
            }else{
                return a.y-b.y;
            }
        }
    }
    public static long gcd(long a,long b){
        if (b == 0)
        return a;
        return gcd(b, a % b);
    }
    public static int lcm(int a,int b){
        int x=Math.max(a,b);
        int y=Math.min(a,b);
        long ans=x;
        while(ans%y!=0){
            ans+=x;
        }
        if(ans>Integer.MAX_VALUE){
            return -1;
        }
        return (int)ans;
    }
    public static long fact(int n){
        long ans=1;
        for(int i=1;i<=n;i++){
            ans*=i;
        }
        return ans;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main
{
	public static void main(String[] args) {
		new Thread(null, new Runnable() {
			public void run() {
                solve();
            }
        }, "1", 1 << 26).start();
	}
	static void solve () {
		FastReader fr =new FastReader();	PrintWriter op =new PrintWriter(System.out);

 		int t =fr.nextInt() ;	long m ,d ,w ,n ,x ,ct ,l ,ans ;
 		while (t-- > 0) {
 			m =fr.nextLong() ;	d =fr.nextLong() ;	w =fr.nextLong() ;	n =Math.min(m,d) ;
 			if (n==1)	op.println("0") ;
 			else {
 				l =((--d)*w) / gcd (d,w) ;	x =l/d ;	ct =(n-1)/x ;
 				ans =ct*n - (ct*x*(1+ct))/2l ;	op.println(ans) ;
 			}
 		}
		op.flush();	op.close();
	}
	static long gcd (long num , long den) {
		if (num<den)	{	num^=den ;	den^=num ;	num^=den ;	}
		return (num%den == 0l ? den : gcd (den,num%den)) ;
	}
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br =new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st==null || (!st.hasMoreElements()))
			{
				try
				{
					st =new StringTokenizer(br.readLine());
				}
				catch(IOException e)
				{
					e.printStackTrace();
				}

			}
			return st.nextToken();
		}

		String nextLine() {
			String str ="";

			try
			{
				str =br.readLine();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}

			return str;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next()) ;
		}
	}
}


import java.util.*;
import java.io.*;

public class E1389 {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int t = sc.nextInt();
		while (t-- > 0) {
			long m = sc.nextLong();
			long d = sc.nextLong();
			long w = sc.nextLong();
			long z = d - 1;
			if (z == 0) {
				pw.println(0);
				continue;
			}
			long lcm = lcm(w, z);
			long u = lcm / z;
			long s = Math.min(m, d);
			long lim = (s - 1) / u;
			long ans = s * lim - u * (lim + 1) * lim / 2;
			pw.println(ans);
		}
		pw.close();
	}

	public static long gcd(long a, long b) {
		return b == 0 ? a : gcd(b, a % b);
	}

	public static long lcm(long a, long b) {
		return a / gcd(a, b) * b;
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader f) {
			br = new BufferedReader(f);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(next());
			}
			return arr;
		}

	}

}


//JHope
import java.util.*;
import java.io.*;
import java.math.*;

   public class E
   {
      public static void main(String staygold[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int T = Integer.parseInt(st.nextToken());
         StringBuilder sb = new StringBuilder();
         while(T-->0)
         {
            st = new StringTokenizer(infile.readLine());
            long M = Long.parseLong(st.nextToken());
            long D = Long.parseLong(st.nextToken());
            long W = Long.parseLong(st.nextToken());
            long min = Math.min(M,D);
            if(false)
               sb.append("0\n");
            else
            {
               //y-x = kw/(d-1)
               long denom = D-1;
               long numer = W;
               while(denom%2 == 0 && numer%2 == 0)
               {
                  numer /= 2;
                  denom /= 2;
               }
               for(long div=3L; div <= (long)(Math.sqrt(denom)+0.0000001); div += 2)
                  while(denom%div == 0 && numer%div == 0)
                  {
                     numer /= div;
                     denom /= div;
                  }
               if(denom > 1 && numer%denom == 0)
                  numer /= denom;
               //K = denom
               long cnt = (min-1)/numer;
               long diff = numer;
               long start = (min-1)%numer+1;
               long res = cnt*(2*start+diff*(cnt-1));
               res /= 2;
               sb.append(res+"\n");
            }
         }
         System.out.print(sb);
      }
   }


//JHope
import java.util.*;
import java.io.*;
import java.math.*;

   public class E
   {
      public static void main(String staygold[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int T = Integer.parseInt(st.nextToken());
         StringBuilder sb = new StringBuilder();
         while(T-->0)
         {
            st = new StringTokenizer(infile.readLine());
            long M = Long.parseLong(st.nextToken());
            long D = Long.parseLong(st.nextToken());
            long W = Long.parseLong(st.nextToken());
            long min = Math.min(M,D);
            if(M == 1)
               sb.append("0\n");
            else
            {
               //y-x = kw/(d-1)
               long denom = D-1;
               long numer = W;
               while(denom%2 == 0 && numer%2 == 0)
               {
                  numer /= 2;
                  denom /= 2;
               }
               long sqrt = denom;
               for(long div=3L; div <= (long)(Math.sqrt(sqrt)+0.0000001); div += 2)
                  while(denom%div == 0 && numer%div == 0)
                  {
                     numer /= div;
                     denom /= div;
                  }
               if(denom > 1 && numer%denom == 0)
                  numer /= denom;
               //K = denom
               long cnt = (min-1)/numer;
               long diff = numer;
               long start = min-cnt*numer;
               long res = cnt*(2L*start+diff*(cnt-1));
               res /= 2;
               sb.append(res+"\n");
            }
         }
         System.out.print(sb);
      }
   }


//JHope
import java.util.*;
import java.io.*;
import java.math.*;

   public class E
   {
      public static void main(String staygold[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int T = Integer.parseInt(st.nextToken());
         StringBuilder sb = new StringBuilder();
         while(T-->0)
         {
            st = new StringTokenizer(infile.readLine());
            long M = Long.parseLong(st.nextToken());
            long D = Long.parseLong(st.nextToken());
            long W = Long.parseLong(st.nextToken());
            long min = Math.min(M,D);
            if(min == 1)
               sb.append("0\n");
            else if(W == 1)
               sb.append(min*(min-1)/2).append("\n");
            else
            {
               //y-x = kw/(d-1)
               long denom = D-1;
               long numer = W;
               while(denom%2 == 0 && numer%2 == 0)
               {
                  numer /= 2;
                  denom /= 2;
               }
               for(long div=3L; div <= (int)(Math.sqrt(denom)+0.0000001); div += 2)
                  while(denom%div == 0 && numer%div == 0)
                  {
                     numer /= div;
                     denom /= div;
                  }
               if(denom > 1 && numer%denom == 0)
                  numer /= denom;
               //K = denom
               long cnt = (min-1)/numer;
               long diff = numer;
               long start = (min-1)%numer+1;
               long res = cnt*(2*start+diff*(cnt-1));
               res /= 2;
               sb.append(res+"\n");
            }
         }
         System.out.print(sb);
      }
   }


//JHope
import java.util.*;
import java.io.*;
import java.math.*;

   public class E
   {
      public static void main(String staygold[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int T = Integer.parseInt(st.nextToken());
         StringBuilder sb = new StringBuilder();
         while(T-->0)
         {
            st = new StringTokenizer(infile.readLine());
            long M = Long.parseLong(st.nextToken());
            long D = Long.parseLong(st.nextToken());
            long W = Long.parseLong(st.nextToken());
            long min = Math.min(M,D);
            if(min == 1)
               sb.append("0\n");
            else
            {
               //y-x = kw/(d-1)
               long denom = D-1;
               long numer = W;
               numer /= gcd(numer, denom);
               //K = denom
               long cnt = (min-1)/numer;
               long diff = numer;
               long start = min-cnt*numer;
               long res = cnt*(2L*start+diff*(cnt-1));
               res /= 2;
               sb.append(res+"\n");
            }
         }
         System.out.print(sb);
      }
      public static long gcd(long a, long b)
      {
         if(a > b)
         {
            long t = a;
            a = b;
            b = t;
         }
         if(a == 0L)
            return b;
         return gcd(b%a, a);
      }
   }


//JHope
import java.util.*;
import java.io.*;
import java.math.*;

   public class E
   {
      public static void main(String staygold[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int T = Integer.parseInt(st.nextToken());
         StringBuilder sb = new StringBuilder();
         while(T-->0)
         {
            st = new StringTokenizer(infile.readLine());
            long M = Long.parseLong(st.nextToken());
            long D = Long.parseLong(st.nextToken());
            long W = Long.parseLong(st.nextToken());
            long min = Math.min(M,D);
            if(min == 1)
               sb.append("0\n");
            else
            {
               //y-x = kw/(d-1)
               long denom = D-1;
               long numer = W;
               while(denom%2 == 0 && numer%2 == 0)
               {
                  numer /= 2;
                  denom /= 2;
               }
               long sqrt = denom;
               for(long div=3L; div <= (long)(Math.sqrt(sqrt)+0.0000001); div += 2)
                  while(denom%div == 0 && numer%div == 0)
                  {
                     numer /= div;
                     denom /= div;
                  }
               if(denom > 1 && numer%denom == 0)
                  numer /= denom;
               //K = denom
               long cnt = (min-1)/numer;
               long diff = numer;
               long start = min-cnt*numer;
               long res = cnt*(2*start+diff*(cnt-1));
               res /= 2;
               sb.append(res+"\n");
            }
         }
         System.out.print(sb);
      }
   }


//JHope
import java.util.*;
import java.io.*;
import java.math.*;

   public class E
   {
      public static void main(String staygold[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int T = Integer.parseInt(st.nextToken());
         StringBuilder sb = new StringBuilder();
         while(T-->0)
         {
            st = new StringTokenizer(infile.readLine());
            long M = Long.parseLong(st.nextToken());
            long D = Long.parseLong(st.nextToken());
            long W = Long.parseLong(st.nextToken());
            long min = Math.min(M,D);
            if(min == 1)
               sb.append("0\n");
            else
            {
               //y-x = kw/(d-1)
               long denom = D-1;
               long numer = W;
               while(denom%2 == 0 && numer%2 == 0)
               {
                  numer /= 2;
                  denom /= 2;
               }
               long sqrt = denom;
               for(long div=3L; div <= (long)(Math.sqrt(sqrt)+0.0000001); div += 2)
                  while(denom%div == 0 && numer%div == 0)
                  {
                     numer /= div;
                     denom /= div;
                  }
               if(denom > 1 && numer%denom == 0)
                  numer /= denom;
               //K = denom
               long cnt = (min-1)/numer;
               long diff = numer;
               long start = min-cnt*numer;
               long res = cnt*(2L*start+diff*(cnt-1));
               res /= 2;
               sb.append(res+"\n");
            }
         }
         System.out.print(sb);
      }
   }


//JHope
import java.util.*;
import java.io.*;
import java.math.*;

   public class E
   {
      public static void main(String staygold[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int T = Integer.parseInt(st.nextToken());
         StringBuilder sb = new StringBuilder();
         while(T-->0)
         {
            st = new StringTokenizer(infile.readLine());
            long M = Long.parseLong(st.nextToken());
            long D = Long.parseLong(st.nextToken());
            long W = Long.parseLong(st.nextToken());
            long min = Math.min(M,D);
            if(min == 1)
               sb.append("0\n");
            else if(false)
               sb.append(min*(min-1)/2).append("\n");
            else
            {
               //y-x = kw/(d-1)
               long denom = D-1;
               long numer = W;
               while(denom%2 == 0 && numer%2 == 0)
               {
                  numer /= 2;
                  denom /= 2;
               }
               for(long div=3L; div <= (int)(Math.sqrt(denom)+0.0000001); div += 2)
                  while(denom%div == 0 && numer%div == 0)
                  {
                     numer /= div;
                     denom /= div;
                  }
               if(denom > 1 && numer%denom == 0)
                  numer /= denom;
               //K = denom
               long cnt = (min-1)/numer;
               long diff = numer;
               long start = (min-1)%numer+1;
               long res = cnt*(2*start+diff*(cnt-1));
               res /= 2;
               sb.append(res+"\n");
            }
         }
         System.out.print(sb);
      }
   }


//JHope
import java.util.*;
import java.io.*;
import java.math.*;

   public class E
   {
      public static void main(String staygold[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int T = Integer.parseInt(st.nextToken());
         StringBuilder sb = new StringBuilder();
         while(T-->0)
         {
            st = new StringTokenizer(infile.readLine());
            long M = Long.parseLong(st.nextToken());
            long D = Long.parseLong(st.nextToken());
            long W = Long.parseLong(st.nextToken());
            long min = Math.min(M,D);
            if(false)
               sb.append("0\n");
            else
            {
               //y-x = kw/(d-1)
               long denom = D-1;
               long numer = W;
               while(denom%2 == 0 && numer%2 == 0)
               {
                  numer /= 2;
                  denom /= 2;
               }
               long sqrt = denom;
               for(long div=3L; div <= (long)(Math.sqrt(sqrt)+0.0000001); div += 2)
                  while(denom%div == 0 && numer%div == 0)
                  {
                     numer /= div;
                     denom /= div;
                  }
               if(denom > 1 && numer%denom == 0)
                  numer /= denom;
               //K = denom
               long cnt = (min-1)/numer;
               long diff = numer;
               long start = (min-1)%numer+1;
               long res = cnt*(2*start+diff*(cnt-1));
               res /= 2;
               sb.append(res+"\n");
            }
         }
         System.out.print(sb);
      }
   }


import java.io.*;
import java.util.*;

public class Main extends PrintWriter {

    private void solve() {
        int tt = sc.nextInt();
        for(int tc = 1; tc <= tt; tc++) {
            long m = sc.nextLong();
            long d = sc.nextLong();
            long w = sc.nextLong();
            long t = w / gcd(d-1, w);
            long q = Math.min(m, d) / t;
            long r = Math.min(m, d) % t;
            long ans = t * (q * (q - 1)) / 2L + q * r;
            println(ans);
        }
    }

    long gcd(long a, long b){
        while(b > 0){
            long t = b;
           b = a%b;
           a = t;
        }
        return a;
    }

//  Solution() throws FileNotFoundException { super(new File("output.txt")); }
//  InputReader sc = new InputReader(new FileInputStream("test_input.txt"));

  Main() { super(System.out); }
  InputReader sc = new InputReader(System.in);
  static class InputReader {
      InputReader(InputStream in) { this.in = in; } InputStream in;

      private byte[] buf = new byte[16384];
      private int    curChar;
      private int    numChars;

      public int read() {
          if (numChars == -1)
              throw new InputMismatchException();
          if (curChar >= numChars) {
              curChar = 0;
              try {
                  numChars = in.read(buf);
              } catch (IOException e) {
                  throw new InputMismatchException();
              }
              if (numChars <= 0)
                  return -1;
          }
          return buf[curChar++];
      }

      public String nextLine() {
          int c = read();
          while (isSpaceChar(c))
              c = read();
          StringBuilder res = new StringBuilder();
          do {
              res.appendCodePoint(c);
              c = read();
          } while (!isEndOfLine(c));
          return res.toString();
      }

      public String nextString() {
          int c = read();
          while (isSpaceChar(c))
              c = read();
          StringBuilder res = new StringBuilder();
          do {
              res.appendCodePoint(c);
              c = read();
          } while (!isSpaceChar(c));
          return res.toString();
      }

      public long nextLong() {
          int c = read();
          while (isSpaceChar(c))
              c = read();
          int sgn = 1;
          if (c == '-') {
              sgn = -1;
              c = read();
          }
          long res = 0;
          do {
              if (c < '0' || c > '9')
                  throw new InputMismatchException();
              res *= 10;
              res += c - '0';
              c = read();
          } while (!isSpaceChar(c));
          return res * sgn;
      }

      public int nextInt() {
          int c = read();
          while (isSpaceChar(c))
              c = read();
          int sgn = 1;
          if (c == '-') {
              sgn = -1;
              c = read();
          }
          int res = 0;
          do {
              if (c < '0' || c > '9')
                  throw new InputMismatchException();
              res *= 10;
              res += c - '0';
              c = read();
          } while (!isSpaceChar(c));
          return res * sgn;
      }

      private boolean isSpaceChar(int c) {
          return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
      }

      private boolean isEndOfLine(int c) {
          return c == '\n' || c == '\r' || c == -1;
      }
  }

    public static void main(String[] $) {
        new Thread(null, new Runnable() {
            public void run() {
                long start = System.nanoTime();
                try {Main solution = new Main(); solution.solve(); solution.close();}
                catch (Exception e) {e.printStackTrace(); System.exit(1);}
                System.err.println((System.nanoTime()-start)/1E9);
            }
        }, "1", 1 << 27).start();

    }

}


import java.util.*;
import java.io.*;

public class Codeforces_1389_E {
	static FastReader sc;
	static PrintWriter pw;

	public static void main(String[] args) throws FileNotFoundException {
		boolean submit = true;
		sc = new FastReader(submit);
		pw = new PrintWriter(System.out);

		int T = sc.nextInt();
		for (int t = 0; t < T; t++) {

			solve();
			pw.flush();
		}

		pw.close();

	}

	static void solve() {
		// Solution
		long M = _long();
		long D = _long();
		long W = _long();

		long XY_max = Math.min(M, D);

		long div = W / gcd(W, D - 1);

		long k = XY_max / div;

		long temp = (k + 1) * XY_max - div * ((k * (k + 1)) / 2);

		Println( temp - XY_max);

	}

	static long gcd(long a, long b) {
		if (b > a) {
			return gcd(b, a);
		}
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}
	/*
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 */

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader(boolean b) throws FileNotFoundException {
			if (b) {
				br = new BufferedReader(new InputStreamReader(System.in));
			} else {
				br = new BufferedReader(new FileReader("src/input.txt"));
			}

		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	static boolean isPrime(long n) {
		if (n <= 1)
			return false;
		if (n <= 3)
			return true;

		if (n % 2 == 0 || n % 3 == 0)
			return false;

		for (long i = 5; i * i <= n; i = i + 6)
			if (n % i == 0 || n % (i + 2) == 0)
				return false;

		return true;
	}

	static void flush() {
		pw.flush();
	}

	static int _int() {
		return sc.nextInt();
	}

	static int[] _int(int n) {
		int[] Arr = new int[n];
		for (int i = 0; i < n; i++) {
			Arr[i] = sc.nextInt();
		}
		return Arr;
	}

	static int[][] _int(int m, int n) {
		int[][] Arr = new int[m][n];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				Arr[i][j] = sc.nextInt();
			}
		}
		return Arr;
	}

	static long _long() {
		return sc.nextLong();
	}

	static long[] _long(int n) {
		long[] Arr = new long[n];
		for (int i = 0; i < n; i++) {
			Arr[i] = sc.nextLong();
		}
		return Arr;
	}

	static long[][] _long(int m, int n) {
		long[][] Arr = new long[m][n];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				Arr[i][j] = sc.nextLong();
			}
		}
		return Arr;
	}

	static String _string() {
		return sc.next();
	}

	static String[] _string(int n) {
		String[] s = new String[n];
		for (int i = 0; i < n; i++) {
			s[i] = sc.next();
		}
		return s;
	}

	static char _char() {
		return sc.next().charAt(0);
	}

	static char[] _chars() {
		return sc.next().toCharArray();
	}

	static char[] _char(int n) {
		char[] c = new char[n];
		for (int i = 0; i < n; i++) {
			c[i] = sc.next().charAt(0);
		}
		return c;
	}

	static char[][] _chars(int n) {
		char[][] C = new char[n][];
		for (int i = 0; i < n; i++) {
			C[i] = _chars();
		}
		return C;
	}

	static char[][] _char(int m, int n) {
		char[][] C = new char[m][n];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				C[m][n] = sc.next().charAt(0);
			}
		}
		return C;
	}

	static int max(int m, int n) {
		return m > n ? m : n;
	}

	static int min(int m, int n) {
		return m < n ? m : n;
	}

	static long max(long m, long n) {
		return m > n ? m : n;
	}

	static long min(long m, long n) {
		return m < n ? m : n;
	}

	static int max(Iterable<Integer> A) {
		int ans = Integer.MIN_VALUE;
		for (int i : A) {
			if (i > ans) {
				ans = i;
			}
		}
		return ans;
	}

	static int min(Iterable<Integer> A) {
		int ans = Integer.MAX_VALUE;
		for (int i : A) {
			if (i < ans) {
				ans = i;
			}
		}
		return ans;
	}

	static int max(int... A) {
		int ans = Integer.MIN_VALUE;
		for (int i : A) {
			if (i > ans) {
				ans = i;
			}
		}
		return ans;
	}

	static int min(int... A) {
		int ans = Integer.MAX_VALUE;
		for (int i : A) {
			if (i < ans) {
				ans = i;
			}
		}
		return ans;
	}

	static long max(long... A) {
		long ans = Long.MIN_VALUE;
		for (long i : A) {
			if (i > ans) {
				ans = i;
			}
		}
		return ans;
	}

	static long min(long... A) {
		long ans = Long.MAX_VALUE;
		for (long i : A) {
			if (i < ans) {
				ans = i;
			}
		}
		return ans;
	}

	static void Println() {
		pw.println();
	}

	static void Println(Object X) {
		pw.println(X.toString());
	}

	static void Print(long x) {
		pw.print(x);
	}

	static void Println(long x) {
		pw.println(x);
	}

	static void Print(String s) {
		pw.print(s);
	}

	static void Println(String s) {
		pw.println(s);
	}

	static void Println(String[] A) {
		for (String s : A) {
			pw.println(s);
		}
	}

	static void Print(String[] A) {
		for (String s : A) {
			pw.print(s + " ");
		}
		pw.println();
	}

	static void Print(int[] A) {
		for (int i : A) {
			pw.print(i + " ");
		}
		pw.println();
	}

	static void Println(int[] A) {
		for (int i : A) {
			pw.println(i);
		}
	}

	static void Println(int[][] A) {
		for (int[] a : A) {
			for (int i : a) {
				pw.print(i + " ");
			}
			pw.println();
		}
	}

	static void Sort(int[] A) {
		Arrays.sort(A);
	}

	static void Sort(List<Integer> A) {
		Collections.sort(A);
	}

	static void Sortr(int[] A) {
		Arrays.sort(A);
		reverse(A);
	}

	static void reverse(int[] A) {
		int n = A.length;
		if (n < 2)
			return;
		int temp;
		for (int i = 0; i < n / 2; i++) {
			temp = A[i];
			A[i] = A[n - i - 1];
			A[n - i - 1] = temp;
		}
	}

	static void Sortr(List<Integer> A) {
		Collections.sort(A, Collections.reverseOrder());
	}

}


import java.util.*;
import java.io.*;

public class E {
    public static void main(String[] args) throws IOException{
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int t = Integer.parseInt(f.readLine());
        while(t-->0){
            StringTokenizer st = new StringTokenizer(f.readLine());
            long m = Long.parseLong(st.nextToken());
            long d = Long.parseLong(st.nextToken());
            long w = Long.parseLong(st.nextToken());
            long diff = w/gcd((d-1), w);
            long mn = Math.min(m,d);
            long n = mn/diff;
            out.println((2 * (mn - diff) - diff * (n - 1)) * n / 2);

        }
        out.close();
    }
    public static long gcd(long a, long b){
        if(a == 0) return b;
        if(b == 0) return a;
        while(b != 0){
            long t = b;
            b = a%b;
            a = t;
        }
        return a;
    }

}


import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class E{
public static long mod = 1000000007;
public static int dx[] = {-1,0,1,0,-1,-1,1,1,-1};//primero ady, despues esq
public static int dy[] = {0,1,0,-1,1,1,-1,-1};
public static void main(String args[]) throws IOException{
StringBuilder sb = new StringBuilder("");
BufferedReader lector = new BufferedReader(new InputStreamReader(System.in));
//String tmp[] = lector.readLine().split(" ");
int cas = Integer.parseInt(lector.readLine());
for(int nn = 0;nn<cas;nn++){
String t[] = lector.readLine().split(" ");
long tt[] = new long[3];
for(int n = 0;n<3;n++)tt[n]=Long.parseLong(t[n]);
long r = tt[1]-1;
if(r==0){//nop, is everything!
long l = tt[0]<tt[1]?tt[0]:tt[1];//Math.min(tt[0],tt[1]);
sb.append((l*(l-1))/2l+"\n");
continue;
}
long gcd = tt[2]/gcd(r,tt[2]);
long min = tt[0]<tt[1]?tt[0]:tt[1];//Math.min(tt[0],tt[1]);
long res = 0;
long res1=0;
//for(long x = gcd;x<=min-1;x++){
//System.out.println("-->"+x/gcd+"  "+gcd);
//res+=((x)/gcd);
//}
long rrr = (min-1)/gcd;
res1 = (((rrr-1)*rrr)/2L)*gcd+rrr*((min-1)%gcd+1);
sb.append(res1+"\n");
}
System.out.print(sb);
}
public static long gcd(long a,long b){
if(b==0)return a;
return gcd(b,a%b);
}
}
class p implements Comparable{
public int a;
public p(int a){
this.a = a;
}
public String toString(){
return "";
}
public int compareTo(Object o){
p pp = (p)o;
return (int)Math.signum(a-pp.a);
}
}
//sergio1


import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class E{
public static long mod = 1000000007;
public static int dx[] = {-1,0,1,0,-1,-1,1,1,-1};//primero ady, despues esq
public static int dy[] = {0,1,0,-1,1,1,-1,-1};
public static void main(String args[]) throws IOException{
StringBuilder sb = new StringBuilder("");
BufferedReader lector = new BufferedReader(new InputStreamReader(System.in));
//String tmp[] = lector.readLine().split(" ");
int cas = Integer.parseInt(lector.readLine());
for(int nn = 0;nn<cas;nn++){
String t[] = lector.readLine().split(" ");
long tt[] = new long[3];
for(int n = 0;n<3;n++)tt[n]=Long.parseLong(t[n]);
long r = tt[1]-1;
if(r==0){//nop, is everything!
long l = tt[0]<tt[1]?tt[0]:tt[1];//Math.min(tt[0],tt[1]);
sb.append((l*(l-1))/2l+"\n");
continue;
}
long gcd = tt[2]/gcd(r,tt[2]);
long min = tt[0]<tt[1]?tt[0]:tt[1];//Math.min(tt[0],tt[1]);
long res = 0;
for(long x = 1;x<=min;x++){
//for(long y = x+1;y<=min;y++)
res+=((min-x)/gcd);//if((y-x)%(gcd)==0)res++;
}
sb.append(res+"\n");
}
System.out.print(sb);
}
public static long gcd(long a,long b){
if(b==0)return a;
return gcd(b,a%b);
}
}
class p implements Comparable{
public int a;
public p(int a){
this.a = a;
}
public String toString(){
return "";
}
public int compareTo(Object o){
p pp = (p)o;
return (int)Math.signum(a-pp.a);
}
}
//sergio1


import java.io.*;
import java.util.*;
import static java.lang.Math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class CalendarAmbiguity {
    public static void main(String[] args) throws IOException {
    	InputStream inputStream = System.in;
        OutputStream outputStream = System.out;

        // InputStream inputStream = new FileInputStream("input.txt");
        // OutputStream outputStream = new FileOutputStream("output.txt");

        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(in.nextInt(), in, out);
        out.close();
    }
    static class TaskA {
        long mod = (long)(998244353);
        long fact[];
        int depth[];
        int parentTable[][];
        int degree[];
        ArrayList<Integer> leaves;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;

        public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
            while(testNumber-->0){
            	long m = in.nextLong();
            	long d = in.nextLong();
            	long w = in.nextLong();
            	HashMap<Long , Integer> wFactor = find(w);
            	HashMap<Long , Integer> dFactor = find(d-1);
            	long rm = 1;
            	for(long i:wFactor.keySet()){
            		long p = wFactor.get(i);
            		if(dFactor.containsKey(i))
            			p -= dFactor.get(i);
            		if(p>0)
            			rm = rm*pow(i , p);
            	}

            	long total = min(m , d);
            	long ai = 1+rm;
            	long n = (total - ai)/rm + 1;
            	long an = ai + (n-1)*rm;

            	long add = n*total + n;
            	long sub = n*(ai+an)/2;
            	out.println(max(0 , add-sub));

            }
        }

        public HashMap<Long , Integer> find(long n){
        	HashMap<Long , Integer> ans = new HashMap<>();
        	for(long i = 2;i*i<=n;i++){
        		if(n%i == 0){
        			int count = 0;
        			while(n%i == 0){
        				n/=i;
        				count++;
        			}
        			ans.put(i , count);
        		}
        	}
        	if(n!=1){
        		ans.put(n , 1);
        	}
        	return ans;
        }

        public void add(HashMap<Integer , HashSet<Integer>> a , int u , int v){
        	if(!a.containsKey(u))
        		a.put(u , new HashSet<>());
        	a.get(u).add(v);
        }

        class Pair{
            long x;
            long y;
            public Pair(long x , long y){
                this.x = x;
                this.y = y;
            }
        }

        class Sort implements Comparator<Pair>{
        	public int compare(Pair a , Pair b){
        		if(a.x < b.x)
        			return -1;
        		if(a.x == b.x && a.y<b.y)
        			return -1;
        		if(a.x == b.x && a.y == b.y)
        			return 0;
        		return 1;
        	}
        }

        public void print1d(char a[] , PrintWriter out){
        	for(int i=0;i<a.length;i++)
        		out.print(a[i]);
        	out.println();
        }

        public int[][] multiply(int a[][] , int b[][]){
            int c[][] = new int[a.length][b[0].length];
            for(int i=0;i<a.length;i++){
                for(int j=0;j<b[0].length;j++){
                    for(int k=0;k<b.length;k++)
                        c[i][j] += a[i][k]*b[k][j];
                }
            }
            return c;
        }

        public int[][] multiply(int a[][] , int b[][] , int mod){
            int c[][] = new int[a.length][b[0].length];
            for(int i=0;i<a.length;i++){
                for(int j=0;j<b[0].length;j++){
                    for(int k=0;k<b.length;k++){
                        c[i][j] += a[i][k]*b[k][j];
                        c[i][j]%=mod;
                    }
                }
            }
            return c;
        }

        public int[][] pow(int a[][] , long b){
            int res[][] = new int[a.length][a[0].length];
            for(int i=0;i<a.length;i++)
                res[i][i] = 1;
            while(b>0){
                if((b&1) == 1)
                    res = multiply(res , a , 10);
                a = multiply(a , a , 10);
                b>>=1;
            }
            return res;
        }

        public int distance(ArrayList<ArrayList<Integer>> a , int u , int v){
            return depth[u]+depth[v] - 2*depth[lca(a , u , v)];
        }

        public int lca(ArrayList<ArrayList<Integer>> a , int u , int v){
            if(depth[v]<depth[u]){
                int x = u;
                u = v;
                v = x;
            }
            int diff = depth[v] - depth[u];
            for(int i=0;i<parentTable[v].length;i++){
                // checking whether the ith bit is set in the diff variable
                if(((diff>>i)&1) == 1)
                    v = parentTable[v][i];
            }
            if(v == u)
                return v;
            for(int i=parentTable[v].length-1;i>=0;i--){
                if(parentTable[u][i] != parentTable[v][i]){
                    v = parentTable[v][i];
                    u = parentTable[u][i];
                }
            }
            return parentTable[u][0];
        }

        // for the min max problems
        public void build(int lookup[][] , int arr[], int n) {
            for (int i = 0; i < n; i++)
                lookup[i][0] = arr[i];
            for (int j = 1; (1 << j) <= n; j++) {
                for (int i = 0; (i + (1 << j) - 1) < n; i++) {
                    if (lookup[i][j - 1] > lookup[i + (1 << (j - 1))][j - 1])
                        lookup[i][j] = lookup[i][j - 1];
                    else
                        lookup[i][j] = lookup[i + (1 << (j - 1))][j - 1];
                }
            }
        }
        public int query(int lookup[][] , int L, int R) {
            int j = (int)(Math.log(R - L + 1)/Math.log(2));
            if (lookup[L][j] >= lookup[R - (1 << j) + 1][j])
                return lookup[L][j];
            else
                return lookup[R - (1 << j) + 1][j];
        }

        // for printing purposes
        public void print1d(int a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
        }
        public void print1d(long a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
        }
        public void print2d(int a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            // out.println();
        }
        public void print2d(long a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            // out.println();
        }

        public void sieve(int a[]){
            a[0] = a[1] = 1;
            int i;
            for(i=2;i*i<=a.length;i++){
                if(a[i] != 0)
                    continue;
                a[i] = i;
                for(int k = (i)*(i);k<a.length;k+=i){
                    if(a[k] != 0)
                        continue;
                    a[k] = i;
                }
            }
        }

        public long nCrPFermet(int n , int r , long p){
            if(r==0)
                return 1l;
            // long fact[] = new long[n+1];
            // fact[0] = 1;
            // for(int i=1;i<=n;i++)
            //     fact[i] = (i*fact[i-1])%p;
            long modInverseR = pow(fact[r] , p-2 , p);
            long modInverseNR = pow(fact[n-r] , p-2 , p);
            long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
            return w;
        }
        public long pow(long a, long b, long m) {
            a %= m;
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a % m;
                a = a * a % m;
                b >>= 1;
            }
            return res;
        }
        public long pow(long a, long b) {
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a;
                a = a * a;
                b >>= 1;
            }
            return res;
        }
        public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) {
            if (start > end) {
                return;
            }
            int mid = (start + end) / 2;
            a.add(mid);
            sortedArrayToBST(a, start, mid - 1);
            sortedArrayToBST(a, mid + 1, end);
        }
        public int lowerLastBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>=x)
                return -1;
            if(a.get(r)<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid-1)<x)
                    return mid-1;
                else if(a.get(mid)>=x)
                    r = mid-1;
                else if(a.get(mid)<x && a.get(mid+1)>=x)
                    return mid;
                else if(a.get(mid)<x && a.get(mid+1)<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(ArrayList<Integer> a , Integer x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>x)
                return l;
            if(a.get(r)<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid+1)>x)
                    return mid+1;
                else if(a.get(mid)<=x)
                    l = mid+1;
                else if(a.get(mid)>x && a.get(mid-1)<=x)
                    return mid;
                else if(a.get(mid)>x && a.get(mid-1)>x)
                    r = mid-1;
            }
            return mid;
        }
        public int lowerLastBound(int a[] , int x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>=x)
                return -1;
            if(a[r]<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid-1]<x)
                    return mid-1;
                else if(a[mid]>=x)
                    r = mid-1;
                else if(a[mid]<x && a[mid+1]>=x)
                    return mid;
                else if(a[mid]<x && a[mid+1]<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(long a[] , long x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>x)
                return l;
            if(a[r]<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid+1]>x)
                    return mid+1;
                else if(a[mid]<=x)
                    l = mid+1;
                else if(a[mid]>x && a[mid-1]<=x)
                    return mid;
                else if(a[mid]>x && a[mid-1]>x)
                    r = mid-1;
            }
            return mid;
        }
        public long log(float number , int base){
            return (long) Math.ceil((Math.log(number) / Math.log(base)) + 1e-9);
        }
        public long gcd(long a , long b){
            if(a<b){
                long c = b;
                b = a;
                a = c;
            }
            while(b!=0){
                long c = a;
                a = b;
                b = c%a;
            }
            return a;
        }
        public long[] gcdEx(long p, long q) {
            if (q == 0)
                return new long[] { p, 1, 0 };
            long[] vals = gcdEx(q, p % q);
            long d = vals[0];
            long a = vals[2];
            long b = vals[1] - (p / q) * vals[2];
            // 0->gcd 1->xValue 2->yValue
            return new long[] { d, a, b };
        }
        public void sievePhi(int a[]){
            a[0] = 0;
            a[1] = 1;
            for(int i=2;i<a.length;i++)
                a[i] = i-1;
            for(int i=2;i<a.length;i++)
                for(int j = 2*i;j<a.length;j+=i)
                    a[j] -= a[i];
        }
        public void lcmSum(long a[]){
            int sievePhi[] = new int[(int)1e6 + 1];
            sievePhi(sievePhi);
            a[0] = 0;
            for(int i=1;i<a.length;i++)
                for(int j = i;j<a.length;j+=i)
                    a[j] += (long)i*sievePhi[i];
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}


import java.io.*;
import java.util.*;
import static java.lang.Math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class CalendarAmbiguity {
    public static void main(String[] args) throws IOException {
    	InputStream inputStream = System.in;
        OutputStream outputStream = System.out;

        // InputStream inputStream = new FileInputStream("input.txt");
        // OutputStream outputStream = new FileOutputStream("output.txt");

        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(in.nextInt(), in, out);
        out.close();
    }
    static class TaskA {
        long mod = (long)(998244353);
        long fact[];
        int depth[];
        int parentTable[][];
        int degree[];
        ArrayList<Integer> leaves;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;

        public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
            while(testNumber-->0){
            	long m = in.nextLong();
            	long d = in.nextLong();
            	long w = in.nextLong();
            	HashMap<Long , Integer> wFactor = find(w);
            	HashMap<Long , Integer> dFactor = find(d-1);
            	long rm = 1;
            	for(long i:wFactor.keySet()){
            		long p = wFactor.get(i);
            		if(dFactor.containsKey(i))
            			p -= dFactor.get(i);
            		if(p>0)
            			rm = rm*pow(i , p);
            	}

            	long total = min(m , d);
            	long ai = 1+rm;
            	long n = (total - ai)/rm + 1;
            	long an = ai + (n-1)*rm;

            	long add = n*total + n;
            	long sub = n*(ai+an)/2;
            	out.println(add-sub);

            }
        }

        public HashMap<Long , Integer> find(long n){
        	HashMap<Long , Integer> ans = new HashMap<>();
        	for(long i = 2;i*i<=n;i++){
        		if(n%i == 0){
        			int count = 0;
        			while(n%i == 0){
        				n/=i;
        				count++;
        			}
        			ans.put(i , count);
        		}
        	}
        	if(n!=1){
        		ans.put(n , 1);
        	}
        	return ans;
        }

        public void add(HashMap<Integer , HashSet<Integer>> a , int u , int v){
        	if(!a.containsKey(u))
        		a.put(u , new HashSet<>());
        	a.get(u).add(v);
        }

        class Pair{
            long x;
            long y;
            public Pair(long x , long y){
                this.x = x;
                this.y = y;
            }
        }

        class Sort implements Comparator<Pair>{
        	public int compare(Pair a , Pair b){
        		if(a.x < b.x)
        			return -1;
        		if(a.x == b.x && a.y<b.y)
        			return -1;
        		if(a.x == b.x && a.y == b.y)
        			return 0;
        		return 1;
        	}
        }

        public void print1d(char a[] , PrintWriter out){
        	for(int i=0;i<a.length;i++)
        		out.print(a[i]);
        	out.println();
        }

        public int[][] multiply(int a[][] , int b[][]){
            int c[][] = new int[a.length][b[0].length];
            for(int i=0;i<a.length;i++){
                for(int j=0;j<b[0].length;j++){
                    for(int k=0;k<b.length;k++)
                        c[i][j] += a[i][k]*b[k][j];
                }
            }
            return c;
        }

        public int[][] multiply(int a[][] , int b[][] , int mod){
            int c[][] = new int[a.length][b[0].length];
            for(int i=0;i<a.length;i++){
                for(int j=0;j<b[0].length;j++){
                    for(int k=0;k<b.length;k++){
                        c[i][j] += a[i][k]*b[k][j];
                        c[i][j]%=mod;
                    }
                }
            }
            return c;
        }

        public int[][] pow(int a[][] , long b){
            int res[][] = new int[a.length][a[0].length];
            for(int i=0;i<a.length;i++)
                res[i][i] = 1;
            while(b>0){
                if((b&1) == 1)
                    res = multiply(res , a , 10);
                a = multiply(a , a , 10);
                b>>=1;
            }
            return res;
        }

        public int distance(ArrayList<ArrayList<Integer>> a , int u , int v){
            return depth[u]+depth[v] - 2*depth[lca(a , u , v)];
        }

        public int lca(ArrayList<ArrayList<Integer>> a , int u , int v){
            if(depth[v]<depth[u]){
                int x = u;
                u = v;
                v = x;
            }
            int diff = depth[v] - depth[u];
            for(int i=0;i<parentTable[v].length;i++){
                // checking whether the ith bit is set in the diff variable
                if(((diff>>i)&1) == 1)
                    v = parentTable[v][i];
            }
            if(v == u)
                return v;
            for(int i=parentTable[v].length-1;i>=0;i--){
                if(parentTable[u][i] != parentTable[v][i]){
                    v = parentTable[v][i];
                    u = parentTable[u][i];
                }
            }
            return parentTable[u][0];
        }

        // for the min max problems
        public void build(int lookup[][] , int arr[], int n) {
            for (int i = 0; i < n; i++)
                lookup[i][0] = arr[i];
            for (int j = 1; (1 << j) <= n; j++) {
                for (int i = 0; (i + (1 << j) - 1) < n; i++) {
                    if (lookup[i][j - 1] > lookup[i + (1 << (j - 1))][j - 1])
                        lookup[i][j] = lookup[i][j - 1];
                    else
                        lookup[i][j] = lookup[i + (1 << (j - 1))][j - 1];
                }
            }
        }
        public int query(int lookup[][] , int L, int R) {
            int j = (int)(Math.log(R - L + 1)/Math.log(2));
            if (lookup[L][j] >= lookup[R - (1 << j) + 1][j])
                return lookup[L][j];
            else
                return lookup[R - (1 << j) + 1][j];
        }

        // for printing purposes
        public void print1d(int a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
        }
        public void print1d(long a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
        }
        public void print2d(int a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            // out.println();
        }
        public void print2d(long a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            // out.println();
        }

        public void sieve(int a[]){
            a[0] = a[1] = 1;
            int i;
            for(i=2;i*i<=a.length;i++){
                if(a[i] != 0)
                    continue;
                a[i] = i;
                for(int k = (i)*(i);k<a.length;k+=i){
                    if(a[k] != 0)
                        continue;
                    a[k] = i;
                }
            }
        }

        public long nCrPFermet(int n , int r , long p){
            if(r==0)
                return 1l;
            // long fact[] = new long[n+1];
            // fact[0] = 1;
            // for(int i=1;i<=n;i++)
            //     fact[i] = (i*fact[i-1])%p;
            long modInverseR = pow(fact[r] , p-2 , p);
            long modInverseNR = pow(fact[n-r] , p-2 , p);
            long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
            return w;
        }
        public long pow(long a, long b, long m) {
            a %= m;
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a % m;
                a = a * a % m;
                b >>= 1;
            }
            return res;
        }
        public long pow(long a, long b) {
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a;
                a = a * a;
                b >>= 1;
            }
            return res;
        }
        public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) {
            if (start > end) {
                return;
            }
            int mid = (start + end) / 2;
            a.add(mid);
            sortedArrayToBST(a, start, mid - 1);
            sortedArrayToBST(a, mid + 1, end);
        }
        public int lowerLastBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>=x)
                return -1;
            if(a.get(r)<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid-1)<x)
                    return mid-1;
                else if(a.get(mid)>=x)
                    r = mid-1;
                else if(a.get(mid)<x && a.get(mid+1)>=x)
                    return mid;
                else if(a.get(mid)<x && a.get(mid+1)<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(ArrayList<Integer> a , Integer x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>x)
                return l;
            if(a.get(r)<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid+1)>x)
                    return mid+1;
                else if(a.get(mid)<=x)
                    l = mid+1;
                else if(a.get(mid)>x && a.get(mid-1)<=x)
                    return mid;
                else if(a.get(mid)>x && a.get(mid-1)>x)
                    r = mid-1;
            }
            return mid;
        }
        public int lowerLastBound(int a[] , int x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>=x)
                return -1;
            if(a[r]<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid-1]<x)
                    return mid-1;
                else if(a[mid]>=x)
                    r = mid-1;
                else if(a[mid]<x && a[mid+1]>=x)
                    return mid;
                else if(a[mid]<x && a[mid+1]<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(long a[] , long x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>x)
                return l;
            if(a[r]<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid+1]>x)
                    return mid+1;
                else if(a[mid]<=x)
                    l = mid+1;
                else if(a[mid]>x && a[mid-1]<=x)
                    return mid;
                else if(a[mid]>x && a[mid-1]>x)
                    r = mid-1;
            }
            return mid;
        }
        public long log(float number , int base){
            return (long) Math.ceil((Math.log(number) / Math.log(base)) + 1e-9);
        }
        public long gcd(long a , long b){
            if(a<b){
                long c = b;
                b = a;
                a = c;
            }
            while(b!=0){
                long c = a;
                a = b;
                b = c%a;
            }
            return a;
        }
        public long[] gcdEx(long p, long q) {
            if (q == 0)
                return new long[] { p, 1, 0 };
            long[] vals = gcdEx(q, p % q);
            long d = vals[0];
            long a = vals[2];
            long b = vals[1] - (p / q) * vals[2];
            // 0->gcd 1->xValue 2->yValue
            return new long[] { d, a, b };
        }
        public void sievePhi(int a[]){
            a[0] = 0;
            a[1] = 1;
            for(int i=2;i<a.length;i++)
                a[i] = i-1;
            for(int i=2;i<a.length;i++)
                for(int j = 2*i;j<a.length;j+=i)
                    a[j] -= a[i];
        }
        public void lcmSum(long a[]){
            int sievePhi[] = new int[(int)1e6 + 1];
            sievePhi(sievePhi);
            a[0] = 0;
            for(int i=1;i<a.length;i++)
                for(int j = i;j<a.length;j+=i)
                    a[j] += (long)i*sievePhi[i];
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}


import java.io.*;
import java.util.*;
import static java.lang.Math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class CalendarAmbiguity {
    public static void main(String[] args) throws IOException {
    	InputStream inputStream = System.in;
        OutputStream outputStream = System.out;

        // InputStream inputStream = new FileInputStream("input.txt");
        // OutputStream outputStream = new FileOutputStream("output.txt");

        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(in.nextInt(), in, out);
        out.close();
    }
    static class TaskA {
        long mod = (long)(998244353);
        long fact[];
        int depth[];
        int parentTable[][];
        int degree[];
        ArrayList<Integer> leaves;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;

        public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
            while(testNumber-->0){
            	long m = in.nextLong();
            	long d = in.nextLong();
            	long w = in.nextLong();

            	long rm = w / gcd(w , d-1);

            	long total = min(m , d);
            	long ai = 1+rm;
            	long n = (total - ai)/rm + 1;
            	long an = ai + (n-1)*rm;

            	long add = n*total + n;
            	long sub = n*(ai+an)/2;
            	out.println(max(0 , add-sub));

            }
        }

        public HashMap<Long , Integer> find(long n){
        	HashMap<Long , Integer> ans = new HashMap<>();
        	for(long i = 2;i*i<=n;i++){
        		if(n%i == 0){
        			int count = 0;
        			while(n%i == 0){
        				n/=i;
        				count++;
        			}
        			ans.put(i , count);
        		}
        	}
        	if(n!=1){
        		ans.put(n , 1);
        	}
        	return ans;
        }

        public void add(HashMap<Integer , HashSet<Integer>> a , int u , int v){
        	if(!a.containsKey(u))
        		a.put(u , new HashSet<>());
        	a.get(u).add(v);
        }

        class Pair{
            long x;
            long y;
            public Pair(long x , long y){
                this.x = x;
                this.y = y;
            }
        }

        class Sort implements Comparator<Pair>{
        	public int compare(Pair a , Pair b){
        		if(a.x < b.x)
        			return -1;
        		if(a.x == b.x && a.y<b.y)
        			return -1;
        		if(a.x == b.x && a.y == b.y)
        			return 0;
        		return 1;
        	}
        }

        public void print1d(char a[] , PrintWriter out){
        	for(int i=0;i<a.length;i++)
        		out.print(a[i]);
        	out.println();
        }

        public int[][] multiply(int a[][] , int b[][]){
            int c[][] = new int[a.length][b[0].length];
            for(int i=0;i<a.length;i++){
                for(int j=0;j<b[0].length;j++){
                    for(int k=0;k<b.length;k++)
                        c[i][j] += a[i][k]*b[k][j];
                }
            }
            return c;
        }

        public int[][] multiply(int a[][] , int b[][] , int mod){
            int c[][] = new int[a.length][b[0].length];
            for(int i=0;i<a.length;i++){
                for(int j=0;j<b[0].length;j++){
                    for(int k=0;k<b.length;k++){
                        c[i][j] += a[i][k]*b[k][j];
                        c[i][j]%=mod;
                    }
                }
            }
            return c;
        }

        public int[][] pow(int a[][] , long b){
            int res[][] = new int[a.length][a[0].length];
            for(int i=0;i<a.length;i++)
                res[i][i] = 1;
            while(b>0){
                if((b&1) == 1)
                    res = multiply(res , a , 10);
                a = multiply(a , a , 10);
                b>>=1;
            }
            return res;
        }

        public int distance(ArrayList<ArrayList<Integer>> a , int u , int v){
            return depth[u]+depth[v] - 2*depth[lca(a , u , v)];
        }

        public int lca(ArrayList<ArrayList<Integer>> a , int u , int v){
            if(depth[v]<depth[u]){
                int x = u;
                u = v;
                v = x;
            }
            int diff = depth[v] - depth[u];
            for(int i=0;i<parentTable[v].length;i++){
                // checking whether the ith bit is set in the diff variable
                if(((diff>>i)&1) == 1)
                    v = parentTable[v][i];
            }
            if(v == u)
                return v;
            for(int i=parentTable[v].length-1;i>=0;i--){
                if(parentTable[u][i] != parentTable[v][i]){
                    v = parentTable[v][i];
                    u = parentTable[u][i];
                }
            }
            return parentTable[u][0];
        }

        // for the min max problems
        public void build(int lookup[][] , int arr[], int n) {
            for (int i = 0; i < n; i++)
                lookup[i][0] = arr[i];
            for (int j = 1; (1 << j) <= n; j++) {
                for (int i = 0; (i + (1 << j) - 1) < n; i++) {
                    if (lookup[i][j - 1] > lookup[i + (1 << (j - 1))][j - 1])
                        lookup[i][j] = lookup[i][j - 1];
                    else
                        lookup[i][j] = lookup[i + (1 << (j - 1))][j - 1];
                }
            }
        }
        public int query(int lookup[][] , int L, int R) {
            int j = (int)(Math.log(R - L + 1)/Math.log(2));
            if (lookup[L][j] >= lookup[R - (1 << j) + 1][j])
                return lookup[L][j];
            else
                return lookup[R - (1 << j) + 1][j];
        }

        // for printing purposes
        public void print1d(int a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
        }
        public void print1d(long a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
        }
        public void print2d(int a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            // out.println();
        }
        public void print2d(long a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            // out.println();
        }

        public void sieve(int a[]){
            a[0] = a[1] = 1;
            int i;
            for(i=2;i*i<=a.length;i++){
                if(a[i] != 0)
                    continue;
                a[i] = i;
                for(int k = (i)*(i);k<a.length;k+=i){
                    if(a[k] != 0)
                        continue;
                    a[k] = i;
                }
            }
        }

        public long nCrPFermet(int n , int r , long p){
            if(r==0)
                return 1l;
            // long fact[] = new long[n+1];
            // fact[0] = 1;
            // for(int i=1;i<=n;i++)
            //     fact[i] = (i*fact[i-1])%p;
            long modInverseR = pow(fact[r] , p-2 , p);
            long modInverseNR = pow(fact[n-r] , p-2 , p);
            long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
            return w;
        }
        public long pow(long a, long b, long m) {
            a %= m;
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a % m;
                a = a * a % m;
                b >>= 1;
            }
            return res;
        }
        public long pow(long a, long b) {
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a;
                a = a * a;
                b >>= 1;
            }
            return res;
        }
        public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) {
            if (start > end) {
                return;
            }
            int mid = (start + end) / 2;
            a.add(mid);
            sortedArrayToBST(a, start, mid - 1);
            sortedArrayToBST(a, mid + 1, end);
        }
        public int lowerLastBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>=x)
                return -1;
            if(a.get(r)<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid-1)<x)
                    return mid-1;
                else if(a.get(mid)>=x)
                    r = mid-1;
                else if(a.get(mid)<x && a.get(mid+1)>=x)
                    return mid;
                else if(a.get(mid)<x && a.get(mid+1)<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(ArrayList<Integer> a , Integer x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>x)
                return l;
            if(a.get(r)<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid+1)>x)
                    return mid+1;
                else if(a.get(mid)<=x)
                    l = mid+1;
                else if(a.get(mid)>x && a.get(mid-1)<=x)
                    return mid;
                else if(a.get(mid)>x && a.get(mid-1)>x)
                    r = mid-1;
            }
            return mid;
        }
        public int lowerLastBound(int a[] , int x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>=x)
                return -1;
            if(a[r]<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid-1]<x)
                    return mid-1;
                else if(a[mid]>=x)
                    r = mid-1;
                else if(a[mid]<x && a[mid+1]>=x)
                    return mid;
                else if(a[mid]<x && a[mid+1]<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(long a[] , long x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>x)
                return l;
            if(a[r]<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid+1]>x)
                    return mid+1;
                else if(a[mid]<=x)
                    l = mid+1;
                else if(a[mid]>x && a[mid-1]<=x)
                    return mid;
                else if(a[mid]>x && a[mid-1]>x)
                    r = mid-1;
            }
            return mid;
        }
        public long log(float number , int base){
            return (long) Math.ceil((Math.log(number) / Math.log(base)) + 1e-9);
        }
        public long gcd(long a , long b){
            if(a<b){
                long c = b;
                b = a;
                a = c;
            }
            while(b!=0){
                long c = a;
                a = b;
                b = c%a;
            }
            return a;
        }
        public long[] gcdEx(long p, long q) {
            if (q == 0)
                return new long[] { p, 1, 0 };
            long[] vals = gcdEx(q, p % q);
            long d = vals[0];
            long a = vals[2];
            long b = vals[1] - (p / q) * vals[2];
            // 0->gcd 1->xValue 2->yValue
            return new long[] { d, a, b };
        }
        public void sievePhi(int a[]){
            a[0] = 0;
            a[1] = 1;
            for(int i=2;i<a.length;i++)
                a[i] = i-1;
            for(int i=2;i<a.length;i++)
                for(int j = 2*i;j<a.length;j+=i)
                    a[j] -= a[i];
        }
        public void lcmSum(long a[]){
            int sievePhi[] = new int[(int)1e6 + 1];
            sievePhi(sievePhi);
            a[0] = 0;
            for(int i=1;i<a.length;i++)
                for(int j = i;j<a.length;j+=i)
                    a[j] += (long)i*sievePhi[i];
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author xwchen
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int T = in.nextInt();
            while (T-- > 0) {
                long m = in.nextLong();
                long d = in.nextLong();
                long w = in.nextLong();
                long min = Math.min(m, d);
                long gcd = MathUtil.getGCD(d - 1, w);
                w /= gcd;
                long cnt = min / w;

                long res = min * cnt - w * (1 + cnt) / 2 * cnt;
                out.println(res);
            }
        }

    }

    static class MathUtil {
        public static long getGCD(long a, long b) {
            if (a < b) {
                long t = a;
                a = b;
                b = t;
            }
            if (b == 0) {
                return a;
            } else {
                return getGCD(b, a % b);
            }
        }

    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer = new StringTokenizer("");

        public InputReader(InputStream inputStream) {
            this.reader = new BufferedReader(
                    new InputStreamReader(inputStream));
        }

        public String next() {
            while (!tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}