N = int(input())
val2ind = {}
for one in list(map(int, input().split())):
    if one not in val2ind:
        val2ind[one] = 0
    val2ind[one]+=1

Q = int(input())

for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in val2ind:
        cand = val2ind[B]
        val2ind[B] = 0
        if C not in val2ind:
            val2ind[C] = cand
        else:
            val2ind[C] += cand
    print(sum([key*val2ind[key] for key in val2ind]))