#include <iostream>
using namespace std;
const int TEN = 10;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
int reverse = 0;
while (x) {
reverse = reverse * TEN + x % TEN;
x /= TEN;
}
while (y > 1) {
reverse /= TEN;
--y;
}
if (x < 0) {
reverse *= -1;
}
cout << reverse % TEN << '\n';
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IFRFTiA9IDEwOwoKaW50IG1haW4oKSB7CiAgICBpbnQgbjsKICAgIGNpbiA+PiBuOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgICBpbnQgeCwgeTsKICAgICAgICBjaW4gPj4geCA+PiB5OwogICAgICAgIGludCByZXZlcnNlID0gMDsKICAgICAgICB3aGlsZSAoeCkgewogICAgICAgICAgICByZXZlcnNlID0gcmV2ZXJzZSAqIFRFTiArIHggJSBURU47CiAgICAgICAgICAgIHggLz0gVEVOOwogICAgICAgIH0KICAgICAgICB3aGlsZSAoeSA+IDEpIHsKICAgICAgICAgICAgcmV2ZXJzZSAvPSBURU47CiAgICAgICAgICAgIC0teTsKICAgICAgICB9CiAgICAgICAgaWYgKHggPCAwKSB7CiAgICAgICAgCXJldmVyc2UgKj0gLTE7CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgcmV2ZXJzZSAlIFRFTiA8PCAnXG4nOwogICAgfQogICAgcmV0dXJuIDA7Cn0=