import java.util.*;

public class Super {
	public static void main(String[] args){
		long[][] dp = new long[10][13];
		for (int F=0; F<10; F++) dp[F][0] = 1;
		for (int L=1; L<13; L++)
			for (int F=0; F<10; F++) if (F!=4) 
				for (int D=0; D<10; D++)
					if (D!=4 && !(F==1 && D==3))
						dp[F][L] += dp[D][L-1];

		Scanner scan = new Scanner(System.in);
		int T = scan.nextInt();
		while (T-- > 0){
			char[] k = scan.next().toCharArray();
			long floors = 0;
			for (int i=0; i<k.length; i++)
				for (int F=0; F < k[i]-'0'; F++)
					if (F!=4 && !(i>0 && k[i-1]=='1' && F==3))
						floors += dp[F][k.length-i-1];
			System.out.println(floors * scan.nextInt());
		}
	}
}
