#include <bits/stdc++.h>
using namespace std;
#define int long long
auto pi = new char[10000000];
auto po = new char[10000000];
auto po0 = po;
int ru() {
while (!isdigit(*pi)) {
pi++;
}
int res = 0;
while (isdigit(*pi)) {
res *= 10;
res += *pi - '0';
pi++;
}
return res;
}
void wc(char c) {
*po++ = c;
}
void wu(int i) {
int x = 1;
int c = 1;
while (x <= i / 10) {
x *= 10;
c++;
}
while (c--) {
int d = i / x;
wc('0' + d);
i -= d * x;
i *= 10;
}
}
int E(int n,int k,int j,int l){
int ans=0;
while(true){
while(j>0&&l>0&&k/l==n/j){
n-=n/j*j;
k-=k/l*l;
l/=10;
j/=10;
}
if(j==0||l>0&&k/l>n/j){
break;
}
if(l==0){
ans+=n+1;
break;
}
ans+=(n/j-(k/l+1))*j;
ans+=n%j+1;
n=j-1;
k-=k/l*l;
l/=10;
j/=10;
}
return ans;
}
int FF[20];
void F(int K,int L){
int O=1;
for(int i=1;i<20;i++){
int o=O;
int k=K;
int l=L;
int ans=0;
while(true){
if(l==0){
ans+=o*10;
break;
}
ans+=(10-(k/l+1))*o;
k-=k/l*l;
l/=10;
o/=10;
}
FF[i]=ans;
O*=10;
O+=1;
}
}
int H(int n,int k){
if(n==0){
return 0;
}
int j=1,o=1;
while(j*10<=n){
j*=10;
o++;
}
int l=1;
while(l*10<=k){
l*=10;
}
return E(n,k,j,l)+FF[o-1];
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifdef CB
freopen("be2.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
fread(pi,1,10000000,stdin);
int T=ru();
while(T--){
int K=ru(),P=ru();
int ll=1;
while(ll*10<=K){
ll*=10;
}
F(K,ll);
int l=K,h=1e18;
while(l!=h){
int m=(l+h)/2;
if(m-H(m,K)>=P){
h=m;
}
else{
l=m+1;
}
}
wu(h-H(h,K)!=P?0:h);wc('\n');
}
fwrite(po0,1,po-po0,stdout);
return 0;
}