#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long long int t, n, m, k;
cin >> t;
while (t--) {
scanf("%x", &n);
cin >> m >> k;
k<<=(32-m);
int date = n; date>>=(32-m);date<<=(32-m);
n -= date;n+=k;
printf("%08x\n", n);
}
return 0;
}