{
  const run = (input: string) => {
    let split = input.trim().split("\n")
    const n = parseInt(split[0])
    const tmpList = split[1].split(" ").map(v => parseInt(v))

    let tmpSum = 0
    const list = {}
    for (let i = 0; i < n; i++) {
      const v = tmpList[i]
      list[v] = list[v] ? list[v] + 1 : 1
      tmpSum += v
    }

    const q = parseInt(split[2])

    const result = []
    const max = 3 + q
    for (let i = 3; i < max; i++) {
      const [from, to] = split[i].split(" ").map(v => parseInt(v))
      if (list[from]) {
        list[to] = list[from] + (list[to] || 0)
        list[from] -= 0
        tmpSum = tmpSum - (from * list[from]) + (to * list[from])
      }

      result.push(tmpSum)
    }

    return result.join("\n")
  }

  const Main = (input: string) => {
    console.log(run(input))
  };

  Main(require("fs").readFileSync("/dev/stdin", "utf8"));
}
