let () = Scanf.scanf "%d\n" @@ fun n ->
  let sum = ref 0 in
  let count = Array.make 100001 0 in
  for i = 0 to n - 1 do
    Scanf.scanf "%d " @@ fun a ->
      sum := a + !sum;
      count.(a) <- 1 + count.(a)
  done;
  Scanf.scanf "%d\n" @@ fun q ->
    for i = 0 to q - 1 do
      Scanf.scanf "%d %d\n" @@ fun b c ->
        sum := !sum + (c - b) * count.(b);
        count.(c) <- count.(b) + count.(c);
        count.(b) <- 0;
        Printf.printf "%d\n" !sum
    done;
