import Foundation
var r = Int(readLine()!)!
var arr=readLine()!.split{$0==" "}.map{Int(String($0))!}
var r2 = Int(readLine()!)!
var sum  = 0
for _ in 0..<r2 {
    sum = 0
    let arr2=readLine()!.split{$0==" "}.map{Int(String($0))!}
    let n=arr2[0]
    let a=arr2[1]
    for i in 0 ..< arr.count {
        if arr[i] == n {
            arr[i] = a
        }
    }
    for i in arr {
        sum += Int(i)
    }
    print(sum)
}