// Arup Guha
// 2/24/2025
// Alternate Solution to COP 4516 Final Individual Contest Problem E: Divisor Series

import java.util.*;

public class series {

	public static void main(String[] args) {

		// We just identify prime numbers upto 1000 here.
		boolean[] prime = new boolean[1001];
		Arrays.fill(prime, true);
		for (int i=2; i<=1000; i++)
			for (int j=2*i; j<=1000; j+=i)
				prime[j] = false;
		ArrayList<Integer> smallp = new ArrayList<Integer>();
		for (int i=2; i<=1000; i++)
			if (prime[i])
				smallp.add(i);
			
		// primes[i] will store the prime factorization of the portion of i that includes primes less than 1000.
		int[][] primes = new int[1000001][];
		for (int i=0; i<1000001; i++) primes[i] = new int[0];
		
		// left will store the portion of the number that wasn't factorized in primes. So, if i = 4036 = 2^2 x 1009,
		// primes[4036] = {2, 2} and left[4036] = 1009.
		int[] left = new int[1000001]; Arrays.fill(left, 1);
		
		// Go through all the numbers from 2 to a million, possible range of n.
		for (int i=2; i<=1000000; i++) {
			
			// Temporary storage for prime factorization using primes less than 1000.
			ArrayList<Integer> mylist = new ArrayList<Integer>();
			
			// This should be pretty quick...168 primes less than 10000.
			int tmp = i;
			for (int j=0; j<smallp.size(); j++) {
				int mye = 0;
				while (tmp%smallp.get(j) == 0) {
					mye++;
					tmp /= smallp.get(j);
				}
				
				// Only add valid terms.
				if (mye > 0) {
					mylist.add(smallp.get(j));
					mylist.add(mye);
				}
			}
			
			// Here is the leftover prime greater than 1000, if it exists.
			if (tmp > 1) left[i] = tmp;
			
			// Copy over the rest of the prime factorization here.
			int[] res = new int[mylist.size()];
			for (int j=0; j<mylist.size(); j++)
				res[j] = mylist.get(j);
			primes[i] = res;
		}
		
		// Process cases.
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		for (int loop=0; loop<nC; loop++) {
			
			// Get input.
			int nTerms = stdin.nextInt();
			long d = stdin.nextLong();
			
			// This runs the full prime factorization of d.
			ArrayList<Long> pfact = primefact(d);
			
			long res = 0;
			
			// We are running the whole sum here, from 1 to n.
			for (int i=1; i<=nTerms; i++) {
				
				// Goal here is to find the number of divisors of i*d.
				
				long tmp = 1;
				long mynum = i;
				
				// We know for a fact that these prime numbers will matter.
				for (int j=0; j<pfact.size(); j+=2) {
					
					// This is the contribution from n for this prime, I just recompute it.
					int mye = 0;
					while (mynum%pfact.get(j) == 0) {
						mye++;
						mynum /= pfact.get(j);
					}
					
					// So for prime number pfact.get(j), this is the term we multiply into our answer
					// for number of divisors. Index j+1 stores the exponent in the prime term for d,
					// mye stores the exponent contributed by n for this same prime. Formula has the +1.
					tmp = tmp*(pfact.get(j+1)+1+mye);
				}
				
				// Do rest with left quickly.
				if (primes[(int)mynum] != null) {
					
					// mynum is relatively prime with d, so we just use our precomp for it.
					for (int j=0; j<primes[(int)mynum].length; j+=2)
						tmp = tmp*(primes[(int)mynum][j+1]+1);
				}
				
				// If there's one more term left, it's a prime to a large power, so we double the # of divisors.
				if (left[(int)mynum] > 1) tmp = tmp*2;
				
				// Add to our running sum of number of divisors.
				res += tmp;
			}
			
			// Ta da!
			System.out.println(res);
		}
	}
	
	// Returns the prime factorization of n the usual way.
	public static ArrayList<Long> primefact(long n) {
		
		ArrayList<Long> res = new ArrayList<Long>();
		long i = 2;
		
		// Go to square root.
		while (i*i <= n) {
			
			// Find number of times this prime divides into n.
			long exp =0;
			while (n%i == 0) {
				exp++;
				n = n/i;
			}
			
			// If positive, add to prime factorization.
			if (exp > 0) {
				res.add(i); res.add(exp);
			}
			
			// Go to next.
			i++;
		}
		
		// Add last prime factorized term, if necessary.
		if (n > 1) {
			res.add(n); res.add(1L);
		}
		
		// Ta da!
		return res;
	}
}