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

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

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

    [| for i in 1 .. 100000 -> i * counts.[i] |]
    |> Array.sum
    |> printfn "%d"
