import strutils, sequtils
import algorithm
# import nre,options

let get = iterator: string {.closure.} =
  for s in stdin.readAll.split: yield s
proc read: int = get().parseInt
template rep(p: untyped, n: int): seq = newSeqWith(n, p)

let n = read()
var input_a = read().rep(n)
let q = read()

let bc = read().rep(2).rep(q)

for i in 0..<q:
  var sum = 0
  for j in 0..<n:
    if input_a[j] == bc[i][0]:
      input_a[j] = bc[i][1]
    sum += input_a[j]
  echo sum