D.solve()


class D {
    static func solve() {
        let N = Int(readLine()!)!
        var As = readLine()!.split(separator: " ").map { Int($0)! }.sorted()
        let Q = Int(readLine()!)!
        for _ in 0 ..< Q {
            let BC = readLine()!.split(separator: " ").map { Int($0)! }
            let B = BC[0]
            let C = BC[1]
            guard var index = As.firstIndex(of: B) else {
                print(As.reduce(into: 0, { (current, value) in
                    current += value
                }))
                continue
            }
            while true {
                if As[index] == B {
                    As[index] = C
                }
                if As.count > index + 1 {
                    index += 1
                } else {
                    break
                }
            }
            print(As.reduce(into: 0, { (current, value) in
                current += value
            }))
        }

    }
}

