#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
typedef long long ll;
typedef long double ld;
#define debug(a) cout<<a<<'\n'
#define Pll pair<ll,ll>
#define PII pair<int,int>
#define ft first
#define sd second
#define vec vector
#define pushk push_back
#define ump unordered_map
#define pl p<<1
#define pr p<<1|1
using namespace std;
const int N = 15 + 10, M = 1e4 + 10, mod = 1e9 + 7;
const ll inf = 1e18;
const ld eps = 1e-13;
int mov[4][2] = { {0,1},{1,0},{-1,0},{0,-1} }, dx, dy, _ = 1, __ = 1;
void bout(bool f) {
if (f)cout << "Yes\n";
else cout << "No\n";
}
ll n, m, k;
ull p[25],dp[25][2],pre[25][2];
void ini() {
p[0]=1;
for(int i=1;i<=19;++i)p[i]=10*p[i-1];
}
void solve() {
string s;
cin>>s>>k;
reverse(s.begin(),s.end());
memset(dp,0x3f,sizeof dp);
dp[0][0]=0;
int x,y,z;
for(int i=0;i<k;++i){
memcpy(pre,dp,sizeof dp);
memset(dp,0x3f,sizeof dp);
if(i<s.length())x=s[i]-'0';
else x=0;
if(x==9){
for(int j=1;j<=i+1;++j){
dp[j][0]=pre[j][0];
dp[j][1]=min(pre[j-1][0]+p[i],pre[j-1][1]);
}
}else{
for(int j=1;j<=i+1;++j){
dp[j][0]=min(pre[j][0],pre[j][1]);
dp[j][1]=min(pre[j-1][0]+(10-x)*p[i],pre[j-1][1]+(9-x)*p[i]);
}
}
dp[0][0]=0;
}
if(!k)cout<<1<<'\n';
else cout<<min(dp[k][0],dp[k][1])<<'\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
streambuf* cinbackup = cin.rdbuf(), * coutbackup = cout.rdbuf();
ifstream fin("in.txt");
ofstream fout("out.txt");
cin.rdbuf(fin.rdbuf());
cout.rdbuf(fout.rdbuf());
#endif
cin >> _;
__ = _;
ini();
while (_--) {
solve();
}
return 0;
}