#003:字符计数
#http://cxsjsxmooc.
n=int(input())
for i in range(n):
a=input().upper()
b=input().upper().split()
for i in range(len(b)):
m=0
for k in b[i]:
if a==k:
m+=1
print(m,end=' ')
print()