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 a = 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 i map = try IntMap.find i map with Not_found -> 0

let map = List.fold_left 
    (fun map key -> IntMap.add key (find key map + 1) map)
    IntMap.empty a

let sum = IntMap.fold (fun key value sum -> sum + key * value) map 0

let _ = Array.fold_left (fun (map, sum) (b, c) ->
    let b_cnt = find b map in
    let next = sum + b_cnt * (c - b) in
    Printf.printf "%d\n" next;
    (map |> IntMap.remove b |> IntMap.add c (find c map + b_cnt), next)
  ) (map, sum) bc