import collections
N = int(input())

Line = [int(s) for s in input().split()]
c = collections.Counter(Line)

# print(c)

Q = int(input())


for _ in range(Q):
    Line = [int(s) for s in input().split()]
    try:
        c[Line[1]] += c[Line[0]]
        c.pop(Line[0])
    except:
        pass
        
        # print('Error')
    # print(c)
    
    s=0
    for i in c:
        # print(i)
        s += i * c[i]
    print(s)    
