#!/usr/bin/env python3
import sys
import numpy as np
sys.setrecursionlimit(1000000000)
MOD = 10 ** 9 + 7


def main():
    N = int(input())
    A = np.fromstring(input(), 'int', sep=' ')
    Q = int(input())
    BC = [list(map(int, input().split())) for _ in range(Q)]

    for B, C in BC:
        A = np.where(A == B, C, A)
        print(np.sum(A))


if __name__ == "__main__":
    main()
