import re
n = int(input())
for _ in range(n):
text = input()
pattern = r"(!)([A-Z][a-z]{2,})\1:\[([A-Za-z]{8,})\]"
matches = re.findall(pattern, text)
if matches:
list_numbers = []
for match in matches:
command = match[1]
for letter in match[2]:
list_numbers.append(str(ord(letter)))
print(f"{command}: {' '.join(list_numbers)}")
else:
print("The message is invalid")