#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MOD = 1e9 + 7;
void solve(){
ll a, b;
cin >> a >> b;
string s1 = to_string(a);
string s2 = to_string(b);
ll tmp = 1, ans = b;
for(auto x: s2){
tmp *= (x - '0');
}
for(int i = 0; i < (int)s2.size(); i++){
if(s2[i] == '0')continue;
s2[i]--;
string tmps = "";
ll g = 1;
for(int j = 0; j < (int)s2.size(); j++){
if(j > i){
g *= (9);
tmps += '9';
}
else {
g *= (s2[j] - '0');
tmps += s2[j];
}
}
if(g > tmp && (s2.size() > s1.size() || tmps >= s1)){
tmp = g;
ans = stoll(tmps);
}
s2[i]++;
}
if(s1.size() < s2.size()){
ll g = 1;
string h = "";
for(int i = 0; i < (int)s2.size() - 1; i++){
g *= 9;
h += '9';
}
if(g > tmp){
ans = stoll(h);
}
}
cout << ans << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
for(int i = 1; i <= t; i++){
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1PRCA9IDFlOSArIDc7Cgp2b2lkIHNvbHZlKCl7CglsbCBhLCBiOwoJY2luID4+IGEgPj4gYjsKCQoJc3RyaW5nIHMxID0gdG9fc3RyaW5nKGEpOwoJc3RyaW5nIHMyID0gdG9fc3RyaW5nKGIpOwoJbGwgdG1wID0gMSwgYW5zID0gYjsKCQoJZm9yKGF1dG8geDogczIpewoJCXRtcCAqPSAoeCAtICcwJyk7Cgl9CgkKCWZvcihpbnQgaSA9IDA7IGkgPCAoaW50KXMyLnNpemUoKTsgaSsrKXsKCQlpZihzMltpXSA9PSAnMCcpY29udGludWU7CgkJczJbaV0tLTsKCQlzdHJpbmcgdG1wcyA9ICIiOwoJCWxsIGcgPSAxOwoJCWZvcihpbnQgaiA9IDA7IGogPCAoaW50KXMyLnNp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