module IntMap = Map.Make(struct type t = int let compare = compare end)
let split_string ?(pattern="") = Str.split @@ Str.regexp pattern

let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n
let aa = read_line () |> split_string ~pattern:" " |> List.map int_of_string
let q = Scanf.sscanf (read_line ()) "%d" @@ fun q -> q
let bc = Array.init q @@ fun _ -> Scanf.sscanf (read_line ()) "%d %d" @@ fun b c -> (b, c)

let find map n = try IntMap.find n map with Not_found -> 0

let add map n =
  let v = find map n in
  IntMap.add n (v + 1) map
let map = ref @@ List.fold_left add IntMap.empty aa

let () =
  let sum = ref @@ IntMap.fold (fun k v sum -> sum + k * v) !map 0 in
  Array.iter (fun (b, c) ->
    let b_cnt = find !map b in
    let c_cnt = find !map c in
    sum := !sum + b_cnt * (c - b);
    Printf.printf "%d\n" !sum;
    map := !map |> IntMap.remove b |> IntMap.add c (c_cnt + b_cnt)
  ) bc