#include <bits/stdc++.h>
using namespace std;
int main(void){
    int q;
    cin >> q;
    string s;
    for (int i = 0; i < q; i++) {
        cin >> s;
        if (s[1] == 'o') {
            s.erase(1, 1);
            cout << stoull(s, nullptr, 0) << endl;
        }
        else if (s[1] == 'b') {
            bitset<70> bs(s.substr(2, (int)s.size()-2));
            cout << bs.to_ullong() << endl;
        }
        else cout << stoull(s, nullptr, 0) << endl;
    }
}