#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
map<char, string> hex_to_bin;
map<string, char> bin_to_oct;
map<char, string> hex_to_oct;
void init() {
hex_to_bin['0'] = "0000"; hex_to_bin['1'] = "0001";
hex_to_bin['2'] = "0010"; hex_to_bin['3'] = "0011";
hex_to_bin['4'] = "0100"; hex_to_bin['5'] = "0101";
hex_to_bin['6'] = "0110"; hex_to_bin['7'] = "0111";
hex_to_bin['8'] = "1000"; hex_to_bin['9'] = "1001";
hex_to_bin['A'] = "1010"; hex_to_bin['B'] = "1011";
hex_to_bin['C'] = "1100"; hex_to_bin['D'] = "1101";
hex_to_bin['E'] = "1110"; hex_to_bin['F'] = "1111";
bin_to_oct["000"] = '0'; bin_to_oct["001"] = '1';
bin_to_oct["010"] = '2'; bin_to_oct["011"] = '3';
bin_to_oct["100"] = '4'; bin_to_oct["101"] = '5';
bin_to_oct["110"] = '6'; bin_to_oct["111"] = '7';
hex_to_oct['0'] = "0"; hex_to_oct['1'] = "1";
hex_to_oct['2'] = "2"; hex_to_oct['3'] = "3";
hex_to_oct['4'] = "4"; hex_to_oct['5'] = "5";
hex_to_oct['6'] = "6"; hex_to_oct['7'] = "7";
hex_to_oct['8'] = "10"; hex_to_oct['9'] = "11";
hex_to_oct['A'] = "12"; hex_to_oct['B'] = "13";
hex_to_oct['C'] = "14"; hex_to_oct['D'] = "15";
hex_to_oct['E'] = "16"; hex_to_oct['F'] = "17";
}
int main()
{
init();
int n;
scanf("%d", &n);
while (n--) {
string s;
cin >> s;
string bin;
for (int i = 0; i < s.size(); ++i)
bin += hex_to_bin[s[i]];
if (s == "0") {
cout << "0" << endl;
continue;
}
if (bin.size() % 3 == 1)
bin = "00" + bin;
if (bin.size() % 3 == 2)
bin = "0" + bin;
string ans;
for (int i = 0; i < bin.size(); i += 3)
ans += bin_to_oct[bin.substr(i, 3)];
int ind = 0;
while (ans[ind] == '0')
++ind;
for ( ; ind < ans.size(); ++ind)
cout << ans[ind];
cout << endl;
}
return 0;
}
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
long long t;
while (scanf("%I64x", &t) != EOF) //note:use %I64
printf("%I64d\n", t);
return 0;
}
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
long long t;
while (scanf("%I64d", &t) != EOF)
printf("%I64X\n", t);
return 0;
}