const main = input => {
  const inputArray = input.split('\n');
  const n = inputArray[0];
  let a = inputArray[1].split(' ').map(x => parseInt(x, 10));
  const q = inputArray[2];

  const counter = {};
  let sum = 0;

  for (let ai of a) {
    sum += ai;
    counter[ai] = ~~counter[ai] + 1;
  }

  for (i = 0; i < q; i ++) {
    const [bi, ci] = inputArray[i + 3].split(' ');
    const diff = ci - bi;
    sum += diff * ~~counter[bi];
    counter[ci] = ~~counter[ci] + ~~counter[bi];
    counter[bi] = 0;
    console.log(sum);
  }
}

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