N = int(input())
Ac = {}
for a in input().strip().split(' '):
    if a in Ac:
        Ac[a] += int(a)
    else:
        Ac[a] = int(a)

Q = int(input())
total = sum(Ac.values())
for q in range(Q):
    B, C = input().strip().split(' ')
    
    if B in Ac:
        if C in Ac:
            Ac[C] += Ac[B] // int(B) * int(C)
        else:
            Ac[C] = Ac[B] // int(B) * int(C)
        del Ac[B]
    else:
        print(total)
    total = sum(Ac.values())
    print(total)