import sys
import collections

input = sys.stdin.readline


def main():
    N = int(input())
    A =list(map(int,input().split()))
    AD = collections.Counter(A)
    Q = int(input())
    for i in range(Q):
        b, c = map(int,input().split())
        if b not in AD.keys():
            AD[c] = AD[b]
            AD[b] =0
        else:
            AD[c] += AD[b]
            AD[b] =0
        ans =0
        for key,item in AD.items():
            ans += key *item

        print(ans)






if __name__ == "__main__":
    main()