n = int(input())
*a, = map(int, input().split())
q = int(input())
d = {}
for i in a:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
for _ in [0]*q:
    b, c = map(int, input().split())
    if b in d:
        if c in d:
            d[c] += d[b]
        else:
            d[c] = d[b]
        d[b] = 0
    print(sum(i*j for i, j in d.items()))