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

for ($i = 0; $i < $q; $i++) {
    fscanf(STDIN, "%d %d", $b, $c);
    $next = [];

    foreach ($a as $num) {
        $replace = $num;

        if ((integer) $replace === (integer) $b) {
            $replace = $c;
        }

        $next[] = $replace;
    }

    $a = $next;

    $total = array_sum($a);
    echo "{$total}\n";
}
