import Foundation
// Decimal型の有効桁数は38桁
let N = Int(readLine()!)!
var A = readLine()!.components(separatedBy: " ").map { Int($0)! }.sorted()
let Q = Int(readLine()!)!

let totalA = A.reduce(0) { $0 + $1 }
var S = totalA

for _ in 1...Q {
    let line = readLine()!.components(separatedBy: " ").map { Int($0)! }
    let B = line[0]
    let C = line[1]
    
    guard let firstIndex = A.firstIndex(of: B) else {
        print(S)
        continue
    }
    guard let lastIndex = A.lastIndex(of: B) else {
        print(S)
        continue
    }
    S = S + (C - B) * (lastIndex - firstIndex + 1)
    for _ in firstIndex...lastIndex {
        A[firstIndex] = C
    }
    print(S)
}
