const [nStr, aStr, qStr, ...bcStrs] = require('fs').readFileSync('/dev/stdin', 'utf8').split(/\n/)
let s = 0;
const aMap = aStr.split(/\s/).reduce((acc, curr) => {
  const v = Number(curr)
  acc[v] = (acc[v] || 0) + 1
  s += v
  return acc
}, [])
for (const bc of bcStrs) {
  if (!bc) {
    continue
  }
  const [b, c] = bc.split(/\s/).map(Number)
  const originalB = aMap[b] || 0
  s += (c - b) * originalB
  console.log(s)
  aMap[c] = (aMap[c] || 0) + originalB
  aMap[b] = 0
}