n = int(input())
a = list(map(int, input().split()))
q = int(input())

bc = [list(map(int, input().split())) for i in range(q)]

a.sort()
uni = list(set(a))

kosu = [a.count(i) for i in uni]

for i in bc:
    b = i[0]
    c = i[1]

    if b in uni:
        # remove original
        tar = uni.index(b)
        uni.pop(tar)
        num = kosu.pop(tar)
        
        # add new
        if c in uni:
            idx = uni.index(c)
            kosu[idx] += num
        else:
            uni.append(c)
            kosu.append(num)

    ans = 0
    for ii in range(len(uni)):
        ans += uni[ii] * kosu[ii]

    print(ans)