<?php 
  $n = trim(fgets(STDIN));
  $a = explode(' ', trim(fgets(STDIN)));
  $Q = trim(fgets(STDIN));
  $count = 0;

  while($count < $Q){
    // code...
    list($b, $c) = explode(' ',(fgets(STDIN)));
    $a = str_replace($b, $c, $a);
    echo array_sum($a);
    echo "\n";
    $count++; 
  }