import sys
input = sys.stdin.readline
import collections

def main():
    n = int(input())
    a_li = list(map(int, input().split()))
    a_counter = collections.Counter(a_li)
    q = int(input())

    bc_li = []

    for _ in range(q):
        b, c = map(int, input().split())
        bc_li.append([b, c])


    for b, c in bc_li:
        if b in a_counter:
            ans = 0

            b_num = a_counter.pop(b)

            if c in a_counter:
                a_counter[c] += b_num
            else:
                a_counter[c] = b_num

            for x, y in a_counter.items():
                ans += x * y
            
        print(ans)
        


if __name__ == '__main__':
    main()
