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())
    ans =0
    for key, item in AD.items():
        ans += key * item


    for i in range(Q):
        b, c = map(int,input().split())
        if b not in AD.keys():
            print(ans)
            break
        if c not in AD.keys():
            ans =ans +(c-b)*AD[b]
            AD[c] = AD[b]
            AD[b] =0
            print(ans)
        else:
            ans = ans + (c - b) * AD[b]
            AD[c] += AD[b]
            AD[b] = 0
            print(ans)






if __name__ == "__main__":
    main()