s=eval(input())

print((sum(1 for _ in range(int(eval(input())))if s in 2*eval(input()))))
s=eval(input())

print((sum(s in 2*eval(input())for _ in range(int(eval(input()))))))
s = eval(input())
print((sum(1 for _ in range(int(eval(input()))) if s in 2 * eval(input()))))

s = eval(input())
print((sum(s in 2 * eval(input()) for _ in range(int(eval(input()))))))

-print((sum(1 for _ in range(int(eval(input()))) if s in 2 * eval(input())))) +print((sum(s in 2 * eval(input()) for _ in range(int(eval(input()))))))