#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<map>
using namespace std;
const int INF = 1e8;
int sign(int x) {
if (x > 0) {
return 1;
}
if (x < 0) {
return -1;
}
return 0;
}
bool judge(int x, int y) {
x = sign(x), y = sign(y);
if (x * y < 0) {
return 0;
}
return 1;
}
int solve() {
string a, b;
cin >> a >> b;
vector<int>v(4, 0);
for (int i = 0; i < 4; i++) {
v[i] = b[i] - a[i];
}
queue<vector<int>>q;
vector<vector<int>>Vector;
map<vector<int>, int>vis;
vis[v] = 1;
q.push(v);
Vector.push_back(v);
int idx = 0;
while (!q.empty()) {
vector<int>pre = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
vector<int>now = pre;
now[i] = (now[i] + 10) % 10;
if (!vis[now]) {
vis[now] = 1;
q.push(now);
Vector.push_back(now);
}
now[i] -= 10;
if (!vis[now]) {
vis[now] = 1;
q.push(now);
Vector.push_back(now);
}
}
}
int ans = INF;
for (int i = 0; i < (int)Vector.size(); i++) {
int res = 0;
while (true) {
int val = INT_MAX;
int j = 0;
while (j < 4 && (!Vector[i][j])) {
j++;
}
for (; j < 4; j++) {
if ((val == INT_MAX || val == 0) || (sign(Vector[i][j]) == sign(val) && abs(Vector[i][j]) < abs(val))) {
val = Vector[i][j];
}
else if (sign(Vector[i][j]) != sign(val)) {
break;
}
}
if ((val == INT_MAX) || (val == 0)) {
break;
}
res += abs(val);
for (int k = 0; k < j; k++) {
if (Vector[i][k] == 0) {
continue;
}
Vector[i][k] -= val;
}
}
ans = min(ans, res);
}
return ans;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 0;
cin >> t;
while (t--) {
int ans = solve();
cout << ans << endl;
}
return 0;
}