#include<iostream>
#include<cstring>
#include<cstdio>
#include <string>
#include <windows.h>
using namespace std;
string transformtwo(char c);
string transformthree(string str);
string transform(string str);
string transform(string str)//16转2
{
string dec = "";
int len = str.size();
for (int i = 0; i < len; i++)
{
dec = dec + transformtwo(str[i]);
}
return dec;
}
string transformtwo(char c)
{
if (c =='0')
return "0000";
else if (c == '1')
return "0001";
else if (c == '2')
return "0010";
else if (c == '3')
return "0011";
else if (c == '4')
return "0100";
else if (c == '5')
return "0101";
else if (c == '6')
return "0110";
else if (c == '7')
return "0111";
else if (c == '8')
return "1000";
else if (c =='9')
return "1001";
else if (c == 'A')
return "1010";
else if (c == 'B')
return "1011";
else if (c == 'C')
return "1100";
else if (c == 'D')
return "1101";
else if (c == 'E')
return "1110";
else if (c == 'F')
return "1111";
}
string transformeight(string str)//2转8
{
string dec = "";
string cstring = "";
int len = str.size();
while(len % 3 != 0)
{
str = "0" + str;
len++;
}
for (int i = 0; i < len; i += 3)
{
cstring = str.substr(i, 3);
dec = dec + transformthree(cstring);
}
return dec;
}
string transformthree(string str)
{
if (str == "000")
return "0";
if (str == "001")
return "1";
if (str == "010")
return "2";
if (str == "011")
return "3";
if (str == "100")
return "4";
if (str == "101")
return "5";
if (str == "110")
return "6";
if (str == "111")
return "7";
}
int main() {
DWORD start, end;
start = GetTickCount();
int n;
string str[200],dec[200];
cin >> n;
for (int i = 0; i <= n; i++)
{
getline(cin, str[i]);
dec[i] = transformeight(transform(str[i]));
}
for (int i = 0; i <= n; i++)
{
if (dec[i].substr(0, 1) == "0")
cout << dec[i].substr(1) << endl;
else if (dec[i].substr(0, 2) == "00")
cout << dec[i].substr(2) << endl;
}
end = GetTickCount() - start;
cout << end << endl;
}