#include #include using namespace std; int main( void ) { int Q; cin >> Q; string S; while( Q-- > 0 ) { cin >> S; if( S.size() == 1 || ( '0' <= S[1] && S[1] <= '9' ) ) { cout << S << endl; } else { long long ans = 0; long long base, cur; if( S[1] == 'b' ) { base = 2; } else if( S[1] == 'o' ) { base = 8; } else { base = 16; } cur = 1; for( int i = S.size() - 1; i > 1; i-- ) { if( '0' <= S[i] && S[i] <= '9' ) { ans += cur * ( S[i] - '0' ); } else { ans += cur * ( S[i] - 'a' + 10 ); } cur *= base; } cout << ans << endl; } } return 0; }