let N = Int(readLine()!)!
let A = readLine()!.split(separator: " ").map { Int($0)! }
let Q = Int(readLine()!)!

var table: [Int: Int] = [:]

var sum = 0
for num in A {
  sum += num
  table[num, default: 0] += 1
}

for _ in 0..<Q {
  let inputs = readLine()!.split(separator: " ").map { Int($0)! }
  
  let from = inputs[0]
  let to = inputs[1]
  
  if let count = table[from] {
    let diff = (to - from) * count
    sum += diff
    table[from] = nil
    table[to, default: 0] += count
  }
  print(sum)
}