n = int(input())
a = list(map(int,input().split()))
q = int(input())
a_dic = {}

for elem in a:
    if elem not in a_dic:
        a_dic[elem] = 1
    else:
        a_dic[elem] += 1

for i in range(q):
    b, c = map(int,input().split())
    if b in a_dic:
        if c in a_dic:
            a_dic[c] += a_dic[b]
            del a_dic[b]
        else:
            a_dic[c] = a_dic[b]
            del a_dic[b]
    ans = 0
    for ai in a_dic:
        ans += ai * a_dic[ai]
    print(ans)