n = int(input())
an_list = list(map(str, input().split()))

q = int(input())
an_str = '+'.join(an_list)
for _ in range(q):
    b, c = map(str, input().split())
    an_str = an_str.replace(b, c)
    print(sum([int(x) for x in an_str.split('+')]))