root = {}
def insert(string):
p = root
for char in string:
p = p.setdefault(char, {})
p['*'] = p.get('*', 0) + 1
def query(string):
p = root
for char in string:
if char not in p: return 0
p = p[char]
if '*' in p: return p['*']
return 0
N = int(input())
for i in range(N):
q, string = list(map(str, input().strip().split()))
if q == 'I':
insert(string)
elif q == 'Q':
print(query(string))