Scanf.scanf "%d" (fun n ->
    let hist = Array.make 100001 0 in
    let rec loop i sum =
        if i = n then sum else
            let sum = Scanf.scanf " %d" (fun a -> hist.(a) <- hist.(a) + 1; sum + a) in
            loop (i + 1) sum
    in
    let sum = loop 0 0 in
    Scanf.scanf " %d" (fun q ->
        let rec loop i sum =
            if i = q then () else
                let sum = Scanf.scanf " %d %d" (fun b c ->
                    let sum = sum - b * hist.(b) in
                    let sum = sum + c * hist.(b) in
                    hist.(c) <- hist.(c) + hist.(b);
                    hist.(b) <- 0;
                    sum
                )
                in
                let () = Printf.printf "%d\n" sum in
                loop (i + 1) sum
        in
        loop 0 sum
    )
)