use std::collections::BTreeSet;

fn main() {
    let d: usize = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        buf.trim_end().parse().unwrap()
    };
    let c: Vec<i64> = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let iter = buf.split_whitespace();
        iter.map(|x| x.parse().unwrap()).collect()
    };
    let mut s: Vec<Vec<i64>> = Vec::with_capacity(d);
    for _ in 0..d {
        let a: Vec<i64> = {
            let mut buf = String::new();
            std::io::stdin().read_line(&mut buf).unwrap();
            let iter = buf.split_whitespace();
            iter.map(|x| x.parse().unwrap()).collect()
        };
        s.push(a);
    }
    let s = s;

    let mut satisfy: i64 = 0;
    let mut ord: Vec<usize> = Vec::with_capacity(d);
    let mut sets: Vec<BTreeSet<usize>> = vec![BTreeSet::new(); 26];
    let mut last: Vec<usize> = vec![0; 26];
    for i in 1..=d {
        let t: usize = {
            let mut buf = String::new();
            std::io::stdin().read_line(&mut buf).unwrap();
            buf.trim_end().parse::<usize>().unwrap() - 1
        };
        satisfy += s[i - 1][t];
        last[t] = i;
        for j in 0..26 {
            satisfy -= c[j] * ((i - last[j]) as i64);
        }
        ord.push(t);
        sets[t].insert(i);
    }

    let m: usize = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        buf.trim_end().parse().unwrap()
    };

    for _ in 0..m {
        let (e, q): (usize, usize) = {
            let mut buf = String::new();
            std::io::stdin().read_line(&mut buf).unwrap();
            let mut iter = buf.split_whitespace();
            (
                iter.next().unwrap().parse::<usize>().unwrap(),
                iter.next().unwrap().parse::<usize>().unwrap() - 1,
            )
        };

        let p = ord[e - 1];
        match sets[p].range((e + 1)..).next() {
            Some(x) => {
                match sets[p].range(..(e)).rev().next() {
                    Some(y) => {
                        satisfy -= c[p] * ((e - y) * (x - e)) as i64;
                    },
                    None => {
                        satisfy -= c[p] * (e * (x - e)) as i64;
                    },
                }
            },
            None => {
                match sets[p].range(..(e)).rev().next() {
                    Some(y) => {
                        satisfy -= c[p] * ((e - y) * (d + 1 - e)) as i64;
                    },
                    None => {
                        satisfy -= c[p] * (e * (d + 1 - e)) as i64;
                    },
                }
            }
        }
        sets[p].remove(&e);
        satisfy -= s[e - 1][p];

        match sets[q].range((e + 1)..).next() {
            Some(x) => {
                match sets[q].range(..(e)).rev().next() {
                    Some(y) => {
                        satisfy += c[q] * ((e - y) * (x - e)) as i64;
                    },
                    None => {
                        satisfy += c[q] * (e * (x - e)) as i64;
                    },
                }
            },
            None => {
                match sets[q].range(..e).rev().next() {
                    Some(y) => {
                        satisfy += c[q] * ((e - y) * (d + 1 - e)) as i64;
                    },
                    None => {
                        satisfy += c[q] * (e * (d + 1 - e)) as i64;
                    },
                }
            }
        }
        sets[q].insert(e);
        satisfy += s[e - 1][q];

        ord[e - 1] = q;

        println!("{}", satisfy);
    }
}

Measured memory_bytes: 2224