def sum_dict(d, b, c):
    val = 0
    sum = 0
    if b in d:
        val = d[b]
        del d[b]
        d[c] = d.get(c, 0) + val
    for i in d.items():
        sum += (i[0] * i[1])
    return sum, d
    
n = int(input())
a = [int(x) for x in input().split()]
d = {}
for i in a:
    d[i] = d.get(i, 0) + 1
s = sum(a)
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    s, d = sum_dict(d, b, c)
    print(s)