#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<unordered_map>
#include<stack>
#include<numeric>
using namespace std;
const __int128 INF = 1e36;
#define rep(i, a, b) for(int i = a;i <= b;i++)
#define pre(i, a, b) for(int i = a;i >= b;i--)
__int128 x, m;
__int128 dp[40][20][2];
__int128 num[40];
__int128 a[40];
inline static void read(__int128& n) {
__int128 x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
n = x * f;
}
inline static void print(__int128 n) {
if (n < 0) {
putchar('-');
n *= -1;
}
if (n > 9) print(n / 10);
putchar(n % 10 + '0');
}
void solve()
{
memset(dp, 0x3f, sizeof dp);
memset(a, 0, sizeof a);
__int128 cnt = 0;
while (x)
{
a[++cnt] = x % 10;
x /= 10;
}
num[0] = 1;
rep(i, 1, 36) num[i] = num[i - 1] * 10;
dp[0][0][0] = 0;
if (m == 0)
{
rep(i, 1, cnt + 1)
{
if (a[i] != 9) {
print(num[i - 1]);
cout << endl;
return;
}
}
}
rep(i, 1, 36)
{
rep(k, 0, m)
{
if (a[i] + 1 < 10)
{
dp[i][k][0] = min(dp[i - 1][k][1], dp[i - 1][k][0]);
}
else
{
dp[i][k][0] = dp[i - 1][k][0];
}
if (k != 0)
{
if (a[i] != 0) dp[i][k][1] = min((10 - a[i] - 1) * num[i - 1] + dp[i - 1][k - 1][1], (10 - a[i]) * num[i - 1] + dp[i - 1][k - 1][0]);
else dp[i][k][1] = (10 - a[i] - 1) * num[i - 1] + dp[i - 1][k - 1][1];
}
}
}
__int128 ans = INF;
rep(i, cnt, 36) {
ans = min(ans, min(dp[i][m][1], dp[i][m][0]));
}
print(ans);
cout << endl;
return;
}
int main()
{
__int128 t = 1;
read(t);
while (t--)
{
read(x);
read(m);
solve();
}
return 0;
}