n = int(input())
a = [int(i) for i in input().split(' ')]
q = int(input())
j = []
a.sort()
a2 = {}
for i in a:
    if i in a2:
        a2[i] += 1
    else:
        a2[i] = 1

d = 0
for i in range(q):
    b, c = (int(i) for i in input().split(' '))
    if b in a2:
        if c in a2:
            a2[c] += a2[b]
        else:
            a2[c] = a2[b]
        del a2[b]
        d = 0
        for k in a2:
            d += k * a2[k]
    j.append(d)
 
print("\n".join([str(i) for i in j]))