func calculateSatisfactionPoint(D: Int, cs: [Int], ss: [[Int]], ts: [Int]) -> Int {
    var lastContestDate: [Int] = Array(repeating: 0, count: 26)
    var vs: [Int] = [0]
    for d in 1...D {
        let todayContestType: Int = ts[d-1]
        vs.append(vs[d-1] + ss[d-1][todayContestType-1])
        lastContestDate[todayContestType-1] = d
        
        var decreaseSatisfactionPoint: Int = 0
        for i in 0..<26 {
            decreaseSatisfactionPoint -= cs[i]*(d - lastContestDate[i])
        }
        vs[d] += decreaseSatisfactionPoint
    }
    return vs[D]
}

func localOptimizationPiece(d: Int, q: Int) {
    let oldType: Int = ts[d - 1]
    ts[d - 1] = q

    var oldTypeBfd: Int = 0
    if let idx = ts[..<(d - 1)].lastIndex(of: oldType) { oldTypeBfd = idx + 1 }
    var oldTypeAfd: Int = D + 1
    if let idx = ts[d...].firstIndex(of: oldType) { oldTypeAfd = idx + 1 }

    var newTypeBfd: Int = 0
    if let idx = ts[..<(d - 1)].lastIndex(of: q) { newTypeBfd = idx + 1 }
    var newTypeAfd: Int = D + 1
    if let idx = ts[d...].firstIndex(of: q) { newTypeAfd = idx + 1 }
    
    let dScore: Int = ss[d - 1][q - 1] - ss[d - 1][oldType - 1] - (d - oldTypeBfd)*(oldTypeAfd - d)*cs[oldType - 1] + (d - newTypeBfd)*(newTypeAfd - d)*cs[q - 1]
    score += dScore
}

let D = Int(readLine()!)!
let cs: [Int] = readLine()!.split(separator: " ").map { Int($0)! }
let ss: [[Int]] = (1...D).map { _ in readLine()!.split(separator: " ").map { Int($0)! } }

var ts: [Int] = (1...D).map { _ in Int(readLine()!)! }
let M = Int(readLine()!)!
let dq: [[Int]] = (1...M).map { _ in readLine()!.split(separator: " ").map { Int($0)! } }

var score: Int = calculateSatisfactionPoint(D: D, cs: cs, ss: ss, ts: ts)

for i in 0..<M {
    localOptimizationPiece(d: dq[i][0], q: dq[i][1])
    print(score)	// (๑•ૅㅁ•๑)
}