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


for _ in range(q):
    mo = input().split()
    b = mo[0]
    c = mo[1]
    cnt = 0
    for h in range(n):
        if a[h] == b:
            a[h] = c
        cnt += int(a[h])
    print(cnt)