s=eval(input())

print((sum(s in 2*eval(input())for _ in[0]*int(eval(input())))))
s=eval(input())

print((sum(len((2*eval(input())).split(s))>1 for _ in range(int(eval(input()))))))
s = eval(input())
print((sum(s in 2 * eval(input()) for _ in [0] * int(eval(input())))))

s = eval(input())
print((sum(len((2 * eval(input())).split(s)) > 1 for _ in range(int(eval(input()))))))

-print((sum(s in 2 * eval(input()) for _ in [0] * int(eval(input()))))) +print((sum(len((2 * eval(input())).split(s)) > 1 for _ in range(int(eval(input()))))))