import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
q = ni()
c = [na() for _ in range(q)]

for i in c:
  a = [i[1] if i[0] == l else l for l in a]
  print(sum(a))