#include <cstdio>
#include <cstring>
char X[1005], Y[1005], Z[505], A[505];
int main() {
int cases;
scanf("%d", &cases);
while(cases-- > 0) {
scanf("%s", X);
int X_right_end = strlen(X);
int X_pivot_left = 0;
int X_pivot_right = 2;
if(X_right_end % 2 != 0) {
X_pivot_right = 1;
}
int Z_pivot_right = 2;
int Z_pivot_left = 1;
int Y_pivot_left = -1;
int Y_pivot_right = -1;
int A_right_end = 1;
for(int i = 0; i < X_right_end; ++i) {
X[i] -= '0';
}
for(int i = 0; i < 505; ++i) {
Z[i] = 0;
}
do {
while(true) {
int up = 9, bottom = 0;
int k = (up + bottom) / 2;
bool break_loop = false;
bool the_same_length = false;
bool X_bigger_than_Y = false;
while(true) {
Z[Z_pivot_right - 1] = k;
int carry_num = 0, i, j;
Y_pivot_right = X_pivot_right;
for(i = Z_pivot_right - 1, j = Y_pivot_right - 1; i >= Z_pivot_left; --i, --j) {
int mul = Z[i] * k + carry_num;
carry_num = mul / 10;
Y[j] = mul % 10;
}
if(carry_num != 0) {
if(j < 0 || j < X_pivot_left) {
the_same_length = false;
X_bigger_than_Y = false;
} else if(j >= X_pivot_left) {
Y[j] = carry_num;
Y_pivot_left = j;
for(int zero = X_pivot_left; zero < Y_pivot_left; ++zero) {
Y[zero] = 0;
}
the_same_length = true;
X_bigger_than_Y = true;
}
} else if(carry_num == 0) {
if(j < X_pivot_left - 1) {
the_same_length = false;
X_bigger_than_Y = false;
} else if(j >= X_pivot_left - 1) {
Y_pivot_left = j + 1;
for(int zero = X_pivot_left; zero < Y_pivot_left; ++zero) {
Y[zero] = 0;
}
the_same_length = true;
X_bigger_than_Y = true;
}
}
if(break_loop) {
break;
}
if(X_bigger_than_Y) {
for(int i = X_pivot_left, j = X_pivot_left; i < X_pivot_right; ++i, ++j) {
if(X[i] < Y[j]) {
X_bigger_than_Y = false;
break;
} else if(X[i] > Y[j]) {
break;
}
}
}
if(X_bigger_than_Y) {
if(up == bottom) {
break_loop = true;
continue;
}
bottom = k + 1;
if(up < bottom) {
bottom = up;
}
k = (up + bottom) / 2;
} else {
if(up == bottom) {
k -= 1;
if(k < 0) {
k = 0;
}
break_loop = true;
continue;
}
up = k - 1;
if(up < bottom) {
up = bottom;
}
k = (up + bottom) / 2;
}
}
if(k > 0) {
for(int i = X_pivot_right - 1; i >= X_pivot_left; --i) {
X[i] -= Y[i];
if(X[i] < 0) {
X[i] += 10;
X[i - 1] -= 1;
}
}
}
X_pivot_right += 2;
Z[Z_pivot_right - 1] += k;
int i, carry_num = 0;
for(i = Z_pivot_right - 1; i >= Z_pivot_left; --i) {
if(Z[i] >= 10) {
Z[i] -= 10;
Z[i - 1] += 1;
carry_num = 1;
} else {
carry_num = 0;
}
}
if(carry_num != 0) {
Z_pivot_left -= 1;
}
Z[Z_pivot_right] = 0;
Z_pivot_right += 1;
while(X_pivot_left < X_pivot_right &&
(X_pivot_right - X_pivot_left) > (Z_pivot_right - Z_pivot_left)) {
if(X[X_pivot_left] == 0) {
++X_pivot_left;
} else {
break;
}
}
A[A_right_end - 1] = (char)(k + '0');
A_right_end += 1;
break;
}
} while(X_pivot_right <= X_right_end);
A[A_right_end - 1] = 0;
puts(A);
if(cases > 0) {
puts("");
}
}
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CgpjaGFyIFhbMTAwNV0sIFlbMTAwNV0sIFpbNTA1XSwgQVs1MDVdOwoKaW50IG1haW4oKSB7CiAgICBpbnQgY2FzZXM7CiAgICBzY2FuZigiJWQiLCAmY2FzZXMpOwogICAgd2hpbGUoY2FzZXMtLSA+IDApIHsKICAgICAgICBzY2FuZigiJXMiLCBYKTsKICAgICAgICBpbnQgWF9yaWdodF9lbmQgPSBzdHJsZW4oWCk7CiAgICAgICAgaW50IFhfcGl2b3RfbGVmdCA9IDA7CiAgICAgICAgaW50IFhfcGl2b3RfcmlnaHQgPSAyOwogICAgICAgIGlmKFhfcmlnaHRfZW5kICUgMiAhPSAwKSB7CiAgICAgICAgICAgIFhfcGl2b3RfcmlnaHQgPSAxOwogICAgICAgIH0KICAgICAgICBpbnQgWl9waXZvdF9yaWdodCA9IDI7CiAgICAgICAgaW50IFpfcGl2b3RfbGVmdCA9IDE7CiAgICAgICAgaW50IFlfcGl2b3RfbGVmdCA9IC0xOwogICAgICAgIGludCBZX3Bpdm90X3JpZ2h0ID0gLTE7CiAgICAgICAgaW50IEFfcmlnaHRfZW5kID0gMTsKCiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IFhfcmlnaHRfZW5kOyArK2kpIHsKICAgICAgICAgICAgWFtpXSAtPSAnMCc7CiAgICAgICAgfQogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCA1MDU7ICsraSkgewogICAgICAgICAgICBaW2ldID0gMDsKICAgICAgICB9CgogICAgICAgIGRvIHsKICAgICAgICAgICAgd2hpbGUodHJ1ZSkgewogICAgICAgICAgICAgICAgaW50IHVwID0gOSwgYm90dG9tID0gMDsKICAgICAgICAgICAgICAgIGludCBrID0gKHVwICsgYm90dG9tKSAvIDI7CiAgICAgICAgICAgICAgICBib29sIGJyZWFrX2xvb3AgPSBmYWxzZTsKICAgICAgICAgICAgICAgIGJvb2wgdGhlX3NhbWVfbGVuZ3RoID0gZmFsc2U7CiAgICAgICAgICAgICAgICBib29sIFhfYmlnZ2VyX3RoYW5fWSA9IGZhbHNlOwogICAgICAgICAgICAgICAgd2hpbGUodHJ1ZSkgewogICAgICAgICAgICAgICAgICAgIFpbWl9waXZvdF9yaWdodCAtIDFdID0gazsKICAgICAgICAgICAgICAgICAgICBpbnQgY2FycnlfbnVtID0gMCwgaSwgajsKICAgICAgICAgICAgICAgICAgICBZX3Bpdm90X3JpZ2h0ID0gWF9waXZvdF9yaWdodDsKICAgICAgICAgICAgICAgICAgICBmb3IoaSA9IFpfcGl2b3RfcmlnaHQgLSAxLCBqID0gWV9waXZvdF9yaWdodCAtIDE7IGkgPj0gWl9waXZvdF9sZWZ0OyAtLWksIC0taikgewogICAgICAgICAgICAgICAgICAgICAgICBpbnQgbXVsID0gWltpXSAqIGsgKyBjYXJyeV9udW07CiAgICAgICAgICAgICAgICAgICAgICAgIGNhcnJ5X251bSA9IG11bCAvIDEwOwogICAgICAgICAgICAgICAgICAgICAgICBZW2pdID0gbXVsICUgMTA7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmKGNhcnJ5X251bSAhPSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKGogPCAwIHx8IGogPCBYX3Bpdm90X2xlZnQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHRoZV9zYW1lX2xlbmd0aCA9IGZhbHNlOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgWF9iaWdnZXJfdGhhbl9ZID0gZmFsc2U7CiAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSBpZihqID49IFhfcGl2b3RfbGVmdCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgWVtqXSA9IGNhcnJ5X251bTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIFlfcGl2b3RfbGVmdCA9IGo7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBmb3IoaW50IHplcm8gPSBYX3Bpdm90X2xlZnQ7IHplcm8gPCBZX3Bpdm90X2xlZnQ7ICsremVybykgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFlbemVyb10gPSAwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgdGhlX3NhbWVfbGVuZ3RoID0gdHJ1ZTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIFhfYmlnZ2VyX3RoYW5fWSA9IHRydWU7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9IGVsc2UgaWYoY2FycnlfbnVtID09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoaiA8IFhfcGl2b3RfbGVmdCAtIDEpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHRoZV9zYW1lX2xlbmd0aCA9IGZhbHNlOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgWF9iaWdnZXJfdGhhbl9ZID0gZmFsc2U7CiAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSBpZihqID49IFhfcGl2b3RfbGVmdCAtIDEpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIFlfcGl2b3RfbGVmdCA9IGogKyAxOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgZm9yKGludCB6ZXJvID0gWF9waXZvdF9sZWZ0OyB6ZXJvIDwgWV9waXZvdF9sZWZ0OyArK3plcm8pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBZW3plcm9dID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIHRoZV9zYW1lX2xlbmd0aCA9IHRydWU7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBYX2JpZ2dlcl90aGFuX1kgPSB0cnVlOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmKGJyZWFrX2xvb3ApIHsKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmKFhfYmlnZ2VyX3RoYW5fWSkgewogICAgICAgICAgICAgICAgICAgICAgICBmb3IoaW50IGkgPSBYX3Bpdm90X2xlZnQsIGogPSBYX3Bpdm90X2xlZnQ7IGkgPCBYX3Bpdm90X3JpZ2h0OyArK2ksICsraikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYoWFtpXSA8IFlbal0pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBYX2JpZ2dlcl90aGFuX1kgPSBmYWxzZTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSBpZihYW2ldID4gWVtqXSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmKFhfYmlnZ2VyX3RoYW5fWSkgewogICAgICAgICAgICAgICAgICAgICAgICBpZih1cCA9PSBib3R0b20pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrX2xvb3AgPSB0cnVlOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgYm90dG9tID0gayArIDE7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKHVwIDwgYm90dG9tKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBib3R0b20gPSB1cDsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBrID0gKHVwICsgYm90dG9tKSAvIDI7CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYodXAgPT0gYm90dG9tKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBrIC09IDE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZihrIDwgMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGsgPSAwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWtfbG9vcCA9IHRydWU7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB1cCA9IGsgLSAxOwogICAgICAgICAgICAgICAgICAgICAgICBpZih1cCA8IGJvdHRvbSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgdXAgPSBib3R0b207CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgayA9ICh1cCArIGJvdHRvbSkgLyAyOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmKGsgPiAwKSB7CiAgICAgICAgICAgICAgICAgICAgZm9yKGludCBpID0gWF9waXZvdF9yaWdodCAtIDE7IGkgPj0gWF9waXZvdF9sZWZ0OyAtLWkpIHsKICAgICAgICAgICAgICAgICAgICAgICAgWFtpXSAtPSBZW2ldOwogICAgICAgICAgICAgICAgICAgICAgICBpZihYW2ldIDwgMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgWFtpXSArPSAxMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIFhbaSAtIDFdIC09IDE7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBYX3Bpdm90X3JpZ2h0ICs9IDI7CiAgICAgICAgICAgICAgICBaW1pfcGl2b3RfcmlnaHQgLSAxXSArPSBrOwogICAgICAgICAgICAgICAgaW50IGksIGNhcnJ5X251bSA9IDA7CiAgICAgICAgICAgICAgICBmb3IoaSA9IFpfcGl2b3RfcmlnaHQgLSAxOyBpID49IFpfcGl2b3RfbGVmdDsgLS1pKSB7CiAgICAgICAgICAgICAgICAgICAgaWYoWltpXSA+PSAxMCkgewogICAgICAgICAgICAgICAgICAgICAgICBaW2ldIC09IDEwOwogICAgICAgICAgICAgICAgICAgICAgICBaW2kgLSAxXSArPSAxOwogICAgICAgICAgICAgICAgICAgICAgICBjYXJyeV9udW0gPSAxOwogICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGNhcnJ5X251bSA9IDA7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYoY2FycnlfbnVtICE9IDApIHsKICAgICAgICAgICAgICAgICAgICBaX3Bpdm90X2xlZnQgLT0gMTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIFpbWl9waXZvdF9yaWdodF0gPSAwOwogICAgICAgICAgICAgICAgWl9waXZvdF9yaWdodCArPSAxOwogICAgICAgICAgICAgICAgd2hpbGUoWF9waXZvdF9sZWZ0IDwgWF9waXZvdF9yaWdodCAmJiAKICAgICAgICAgICAgICAgICAgICAoWF9waXZvdF9yaWdodCAtIFhfcGl2b3RfbGVmdCkgPiAoWl9waXZvdF9yaWdodCAtIFpfcGl2b3RfbGVmdCkpIHsKICAgICAgICAgICAgICAgICAgICBpZihYW1hfcGl2b3RfbGVmdF0gPT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICArK1hfcGl2b3RfbGVmdDsKICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBBW0FfcmlnaHRfZW5kIC0gMV0gPSAoY2hhcikoayArICcwJyk7CiAgICAgICAgICAgICAgICBBX3JpZ2h0X2VuZCArPSAxOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICB9IHdoaWxlKFhfcGl2b3RfcmlnaHQgPD0gWF9yaWdodF9lbmQpOwoKICAgICAgICBBW0FfcmlnaHRfZW5kIC0gMV0gPSAwOwogICAgICAgIHB1dHMoQSk7CgogICAgICAgIGlmKGNhc2VzID4gMCkgewogICAgICAgICAgICBwdXRzKCIiKTsKICAgICAgICB9CiAgICB9Cn0=