use std::io;
use std::cmp;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::f64::consts::PI;

use std::ops::{AddAssign, SubAssign};

#[derive(Clone)]
pub struct BIT<T> {
    n: usize,
    bits: Vec<T>,
}
 
impl<T: Default + Copy + AddAssign + SubAssign> BIT<T> {
    pub fn new(n: usize) -> Self {
        let mut m = 1;
        while m < n {
            m *= 2;
        }
        BIT {
            n: m,
            bits: vec![T::default(); m + 1],
        }
    }
 
    pub fn add(&mut self, a: usize, w: T) {
        let mut x = a as i32 + 1;
        while x <= self.n as i32 {
            self.bits[(x as usize)] += w;
            x += x&-x;
        }
    }
    
    pub fn sub(&mut self, a: usize, w: T) {
        let mut x = a as i32 + 1;
        while x <= self.n as i32 {
            self.bits[(x as usize)] -= w;
            x += x&-x;
        }
    }
 
    pub fn sum(&self, a: usize) -> T {
        let mut ret = T::default();
        let mut x = a as i32 + 1;
        while x > 0 {
            ret += self.bits[x as usize];
            x -= x&-x;
        }
        ret
    }
}

fn read_line() -> String {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).ok();
    buf
}

fn find_last(bit: &BIT<u64>, d: usize, D: usize) -> usize {
    let val = bit.sum(d);
    //println!("val: {} {}", val, d);
    let mut l = 0;
    let mut r = D;
    while r - l > 1 {
        let m = (l + r) / 2;
        let tmp = bit.sum(m);
        if tmp < val {
            l = m;
        } else {
            r = m;
        }
    }
    if val == bit.sum(l) {
        l
    } else {
        r
    }
}

fn find_next(bit: &BIT<u64>, d: usize, D: usize) -> usize {
    let val = bit.sum(d);
    let mut l = d;
    let mut r = D;
    while r - l > 1 {
        let m = (l + r) / 2;
        let tmp = bit.sum(m);
        if tmp <= val {
            l = m;
        } else {
            r = m;
        }
    }
    r
}

fn func(a: i64, b: i64) -> i64 {
    let n = b-a;
    n * (n - 1) / 2
}

fn main() {
    let line: Vec<usize> = read_line().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let D = line[0];
    let cs: Vec<i64> = read_line().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let mut s = Vec::with_capacity(26);
    for i in 0..D {
        let line: Vec<i64> = read_line().split_whitespace().map(|s| s.parse().unwrap()).collect();
        s.push(line);
    }
    let mut ts = vec![0; D];
    for i in 0..D {
        let line: Vec<usize> = read_line().split_whitespace().map(|s| s.parse().unwrap()).collect();
        ts[i] = line[0];
    }

    let mut last = vec![0; 26];
    let mut hist = vec![BIT::new(D+2); 26];
    
    let mut score = 0;
    for d in 0..D {
        let t = ts[d] - 1;
        let mut tmp = s[d][t];
        for i in 0..26 {
            if i!= t {
                tmp -= cs[i] * ((d as i64) + 1 - last[i]);
            }
        }
        last[t] = d as i64 + 1;
        hist[t].add(d+1, 1u64);
        //println!("hist {} {} {}", t, d, hist[t].sum(d+1));
        score += tmp;
    }
    for i in 0..26 {
        hist[i].add(D+1, 1);
    }
    let line: Vec<usize> = read_line().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let M = line[0];
    for i in 0..M {
        let line: Vec<usize> = read_line().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let d = line[0];
        let q = line[1]-1;
        let p = ts[d-1] - 1;
        score += s[d-1][q] - s[d-1][p];
        let lp = (find_last(&hist[p], d-1, D+1)) as i64;
        let rp = (find_next(&hist[p], d, D+1)) as i64;
        //println!("{} {}", lp, rp);
        score += cs[p] * (func(d as i64, rp) + func(lp, d as i64) - func(lp, rp));
        let lp = (find_last(&hist[q], d, D+1)) as i64;
        let rp = (find_next(&hist[q], d, D+1)) as i64;
        //println!("{} {}", lp, rp);
        score += cs[q] * (func(lp, rp) - func(lp, d as i64) - func(d as i64, rp));

        hist[p].sub(d, 1);
        hist[q].add(d, 1);
        ts[d-1] = q + 1;
        println!("{}", score);
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_find() {
        let mut bit = BIT::new(8);
        bit.add(1, 1);
        bit.add(2, 1);
        bit.add(3, 1);
        bit.add(7, 1);

        assert_eq!(1, bit.sum(1));
        assert_eq!(2, bit.sum(2));
        assert_eq!(3, bit.sum(3));
        assert_eq!(3, bit.sum(4));
        assert_eq!(4, bit.sum(7));

        assert_eq!(1, find_last(&bit, 1, 8));
        assert_eq!(2, find_last(&bit, 2, 8));
        assert_eq!(3, find_last(&bit, 3, 8));
        assert_eq!(3, find_last(&bit, 4, 8));
        assert_eq!(3, find_last(&bit, 5, 8));
        assert_eq!(3, find_last(&bit, 6, 8));
        assert_eq!(7, find_last(&bit, 7, 8));

        assert_eq!(2, find_next(&bit, 1, 8));
        assert_eq!(3, find_next(&bit, 2, 8));
        assert_eq!(7, find_next(&bit, 3, 8));
        assert_eq!(7, find_next(&bit, 4, 8));
        assert_eq!(7, find_next(&bit, 5, 8));
        assert_eq!(7, find_next(&bit, 6, 8));
    }
}

Measured memory_bytes: 2364