#include <iostream>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
typedef long long ll;
const int MAX_Y = 1e5 + 1000; // Maximum value of y plus buffer
const int MAX_K = 20; // Since 2^20 > 1e5, this is sufficient
const int INF = 1e9 + 7;
int sum_of_digits(int x) {
int sum = 0;
while (x > 0) {
sum += x % 10;
x /= 10;
}
return sum;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
cin >> q;
vector<pair<int, int>> queries(q);
int max_y = 0;
for (int i = 0; i < q; ++i) {
int x, y;
cin >> x >> y;
queries[i] = {x, y};
max_y = max(max_y, y);
}
max_y = min(max_y, (int)1e5); // Ensure y ≤ 1e5
int MAX_X = max_y + 1000; // Slight buffer
vector<int> s(MAX_X + 1, 0);
vector<vector<int>> next(MAX_K, vector<int>(MAX_X + 1, INF));
// Precompute sum of digits
for (int x = 1; x <= MAX_X; ++x) {
s[x] = s[x / 10] + x % 10;
}
// Compute next[0][x] = x + s(x)
for (int x = 1; x <= MAX_X; ++x) {
int nx = x + s[x];
if (nx <= MAX_X) {
next[0][x] = nx;
} else {
next[0][x] = INF;
}
}
// Build jump table
for (int k = 1; k < MAX_K; ++k) {
for (int x = 1; x <= MAX_X; ++x) {
int nx = next[k - 1][x];
if (nx <= MAX_X) {
next[k][x] = next[k - 1][nx];
} else {
next[k][x] = INF;
}
}
}
// Answer queries
for (int i = 0; i < q; ++i) {
int x = queries[i].first;
int y = queries[i].second;
if (x > MAX_X) {
// x is beyond precomputed range
// Simulate steps until x > y
while (x <= y) {
int curr_s = sum_of_digits(x);
x += curr_s;
}
cout << x << '\n';
continue;
}
int curr_x = x;
// Binary lifting
for (int k = MAX_K - 1; k >= 0; --k) {
if (next[k][curr_x] <= y) {
curr_x = next[k][curr_x];
}
}
// After jumps, simulate the remaining steps
while (curr_x <= y) {
curr_x += s[curr_x];
}
cout << curr_x << '\n';
}
return 0;
}