stdin.ReadLine() |> ignore

let ary =
    stdin.ReadLine().Split() |> Array.map int

let q = stdin.ReadLine() |> int

let counts =
    let countAsTuples = ary |> Array.countBy (id)

    let cnt = Array.zeroCreate 100001

    for (key, num) in countAsTuples do
        cnt.[key] <- num

    cnt

let initSum =
    [| for i in 1 .. 100000 -> int64 i * int64 counts.[i] |]
    |> Array.sum

let rec replace i sum counts =
    if i <= 0 then
        ()
    else
        let counts = Array.copy counts

        let b, c =
            stdin.ReadLine().Split()
            |> Array.map int
            |> fun ary -> ary.[0], ary.[1]

        let sum =
            sum + (int64 c - int64 b) * int64 counts.[b]

        counts.[c] <- counts.[b] + counts.[c]
        counts.[b] <- 0

        printfn "%d" sum

        replace (i - 1) sum counts

replace q initSum counts
