open System 
open System.Text

type Explicit =          
    static member inline ($) (_:int32, _:Explicit) = int
    static member inline ($) (_:int64, _:Explicit) = int64

let inline convert (value:'a): 'b = 
    (Unchecked.defaultof<'b> $ Unchecked.defaultof<Explicit>) value

let inline fromInt (x: int): 'a = convert x

module NumericLiteralG =
    let inline FromZero(): 'a = LanguagePrimitives.GenericZero
    let inline FromOne(): 'a = LanguagePrimitives.GenericOne
    let inline FromInt32 (n: int): 'a = fromInt n

module String =
    let toCharArray (s: string): char array = s.ToCharArray()
    let rev (s: string): string = new string(toCharArray s |> Array.rev)

let R() = Console.ReadLine()
let RS() = Console.ReadLine().Split()

let f (a: int array) (bc: (int * int) list): int list =
    let sum0 = Array.sum a 
    let m0 = 
        Array.groupBy id a 
        |> Array.map(fun (k, v) -> k, Array.length v)
        |> Map.ofArray
    let rec g sums m xs =
        match xs with 
        | (b,c):: xs' -> 
            let tb = Map.tryFind b m |> function Some v -> v | _ -> 0
            let tc = Map.tryFind c m |> function Some v -> v | _ -> 0
            let preSum = List.tryHead sums |> function Some x -> x | _ -> sum0 
            let sum = preSum - b * tb + c * tb 
            let m' = (Map.add b 0 >> Map.add c (tb + tc)) m 
            g (sum :: sums) m' xs'
        | _ -> sums 
    g [] m0 bc 
    |> List.rev
    
let N = R()
let A = RS() |> Array.map (string >> int)
let Q = int <| R()
let BC = [ for i in 1..Q -> let t = RS() in int t.[0], int t.[1]]

f A BC 
|> List.iter (printfn "%i")