N = int(input())
A = list(map(int, input().split()))
Q = int(input())
bc = [map(int, input().split()) for _ in range(Q)]
b, c = [list(i) for i in zip(*bc)]

mydict = {}
for i in A:
    if i in mydict:
        score = mydict[i]
        mydict[i] = score + 1
    else:
        mydict[i] = 1


    
for i in range(Q):
    if b[i] in mydict:
        if c[i] in mydict:
            score1 = mydict[b[i]]
            score2 = mydict[c[i]]
            del mydict[b[i]]
            mydict[c[i]] = score1 + score2
        else:
            mydict[c[i]] = mydict[b[i]]
            del mydict[b[i]]
            
    count = 0
    for k, v in mydict.items():
        count = count + k*v
    
    print(count)