<?php

$N = trim(fgets(STDIN));
$A = trim(fgets(STDIN));
$A = explode(" ", $A);
$Q = trim(fgets(STDIN));
for ($i = 0; $i < $Q; $i++) {
    $hoge = trim(fgets(STDIN));
    $hoge = explode(" ", $hoge);
    $B = $hoge[0];
    $C = $hoge[1];
    $A = str_replace($B, $C, $A);
    $atai[] = array_sum($A);
}
foreach ($atai as $v) {
    echo $v . "\n";
}
