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 = List.fold_left add IntMap.empty aa

let replace map (b, c) = 
  let b_cnt = find map b in
  let c_cnt = find map c in
  map |> IntMap.remove b |> IntMap.add c (c_cnt + b_cnt)

let () =
  ignore(Array.fold_left (fun map t ->
    let map = replace map t in
    Printf.printf "%d\n" @@ IntMap.fold (fun k v sum -> sum + k * v) map 0;
    map
  ) map bc)