#!/usr/bin/env python3
import sys
import numpy as np
readline = sys.stdin.buffer.readline

N = int(readline())
A = np.array(list(map(int, readline().split())))
Q = int(readline())

answer = [0] * Q

for i in range(Q):
    B, C = map(int, readline().split())

    A = np.where(A == B, C, A)
    print(np.sum(A))
