<?php
fscanf(STDIN, "%d", $n);
$a = explode(' ', trim(fgets(STDIN)));
fscanf(STDIN, "%d", $q);
$sum = [];

for ($i = 0; $i < $q; $i++) {
  fscanf(STDIN, "%d %d", $b, $c);
  $a = str_replace($b, $c, $a);
  echo array_sum($a) . PHP_EOL;
}