_ = input()
s = list(map(str, input().split()))
n = int(input())
maps={}
for _ in range(n):
  k,v = map(str, input().split())
  maps[k]=v
key_list = list(maps.keys())
flg=True

for k in key_list:
  for i in range(len(s)):
    if s[i]==k:
      s[i] = maps[s[i]]
  ans = 0
  for num in s:
    ans += int(num)
  print(ans)

