#include <bits/stdc++.h>
using namespace std;
int dp[10];
int train(int num) {
  if (num <= 0) return 1;
  if (dp[num] != 0) return dp[num];
  return (dp[num] = (train(num - 10) + train(num - 9)));
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    string str;
    int num, sum = 0;
    cin >> str >> num;
    sort(str.begin(), str.end());
    for (auto it : str) sum += train(num - 57 + it);
    fill_n(dp, 10, 0);
    cout << sum << endl;
  }
  return 0;
}
