using System;
using System.Collections.Generic;

var q = int.Parse(Console.ReadLine());
var prefix = new Dictionary<string, int>()
{
	{ "0b", 2 },
	{ "0o", 8 },
	{ "0x", 16 }
};

// 8進数ない
// あと **リテラルでは** 0b, 0x を使えるのに文字列パースでは使えない
// なんやねんこの言語...
while (q-- > 0)
{
	var s = Console.ReadLine();
	var ok = false;
	foreach (var (key, value) in prefix)
	{
		if (s.StartsWith(key))
		{
			Console.WriteLine(Convert.ToInt64(s.Substring(2), value));
			ok = true;
		}
	}
	if (!ok)
	{
		Console.WriteLine(long.Parse(s));
	}
}