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

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