n = int(input())
dic = {}
for i in input().split():
    if i not in dic:
        dic[i] = 0
    dic[i] += 1
q = int(input())
for j in range(q):
    b,c = (str(x) for x in input().split())
    if b not in dic:
        pass
    else:
        if c not in dic:
            dic[c] = dic[b]
            dic[b] = 0
        else:
            dic[c] += dic[b]
            dic[b] = 0
    ans = 0
    for k in dic:
        ans += int(k)*dic[k]
    print(ans)
