// Arup Guha
// 2/19/2024
// Solution to 2024 COP 4516 Individual Contest Problem: Divisibility

import java.util.*;

public class div {

	public static int n;
	public static char[] num;
	public static int d;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {
			num = stdin.next().toCharArray();
			n = num.length;
			d = stdin.nextInt();
			System.out.println(go(0,0));
		}
	}
	
	// Returns the number of solutions with the first k digits fixed where 
	// their current mod value is curmod.
	public static int go(int k, int curmod) {
	
		// Got to the end...see what curmod is.
		if (k == n)
			return curmod == 0 ? 1 : 0;
			
		// This is the forced case, make sure to pass in the updated mod.
		if (num[k] != '_') return go(k+1, (10*curmod + num[k]-'0')%d);
		
		// The first possible starting digit.
		int start = k == 0 ? 1 : 0;
		
		// Store ans here.
		int res = 0;
		
		// i is next digit. Plug in each option, and add to res.
		for (int i=start; i<10; i++) 
			res += go(k+1, (10*curmod + i)%d);
		
		// Ta da!
		return res;
	}
}