def main():
    import sys
    input = sys.stdin.readline
    from collections import Counter
    N = int(input())
    A = list(map(int, input().split()))
    C = Counter(A)
    Q = int(input())
    ans = sum(A)
    for _ in range(Q):
        b,c = map(int, input().split())
        x = C[b]
        ans += (c-b) * x
        C[c] += x
        print(ans)



if __name__ == '__main__':
    main()