#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
void solve() {
  int m[12];
  memset(m, 0, sizeof(m));
  int n, k, dem = 0;
  cin >> n >> k;
  while (n >= 1) {
    int x = n % 10;
    m[x]++;
    n /= 10;
    dem++;
  }
  while (k--) {
    for (int i = 10; i >= 1; i--) {
      if (m[i - 1] > 0) {
        m[i] = m[i - 1];
        m[i - 1] = 0;
      }
    }
    if (m[10] > 0) {
      dem += m[10];
      m[10] = 0;
      m[1]++;
      m[0]++;
    }
  }
  cout << dem << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
