def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())

    MAX = 10 ** 5 + 10
    k = [0] * MAX
    # v = [0] * MAX

    for a in A:
        k[a] += 1

    # print(k[:10])

    for i in range(Q):
        ans = 0
        b, c = map(int, input().split())
        if k[b] > 0:
            k[c] += k[b]
            k[b] = 0
        for i in range(MAX):
            ans += i * k[i]
        print(ans)


if __name__ == "__main__":
    main()
