#![allow(unused_imports)]
#![allow(unused_macros)]
use std::cmp::{max, min};
use std::collections::*;
use std::io::{stdin, Read};

#[allow(unused_macros)]
macro_rules! parse {
    ($it: ident ) => {};
    ($it: ident, ) => {};
    ($it: ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, $var:ident $($r:tt)*) => {
        let $var = parse_val!($it, usize);
        parse!($it $($r)*);
    };
}

#[allow(unused_macros)]
macro_rules! parse_val {
    ($it: ident, [$t:tt; $len:expr]) => {
        (0..$len).map(|_| parse_val!($it, $t)).collect::<Vec<_>>();
    };
    ($it: ident, ($($t: tt),*)) => {
        ($(parse_val!($it, $t)),*)
    };
    ($it: ident, u1) => {
        $it.next().unwrap().parse::<usize>().unwrap() -1
    };
    ($it: ident, $t: ty) => {
        $it.next().unwrap().parse::<$t>().unwrap()
    };
}

#[cfg(debug_assertions)]
macro_rules! debug {
    ($( $args:expr ),*) => { eprintln!( $( $args ),* ); }
}

#[cfg(not(debug_assertions))]
macro_rules! debug {
    ($( $args:expr ),*) => {
        ()
    };
}

const K: usize = 26;

#[derive(Debug)]
struct Input {
    d: usize,
    c: Vec<i64>,
    s: Vec<Vec<i64>>,
}

fn get_input<'a>(it: &mut impl Iterator<Item = &'a str>) -> Input {
    parse!(it, d: usize, c: [i64; K]);
    let mut s = vec![];
    for _ in 0..d {
        parse!(it, sl: [i64; K]);
        s.push(sl);
    }
    Input { d, c, s }
}

#[derive(Debug)]
struct State<'a> {
    out: Vec<usize>,
    score: i64,
    ds: Vec<Vec<usize>>,
    input: &'a Input,
}

impl<'a> State<'a> {
    fn new(input: &'a Input, out: Vec<usize>) -> Self {
        calc_score(input, out)
    }
    fn change(&mut self, d: usize, q: usize) {
        // remove p
        let p = self.out[d];
        self.score -= self.input.s[d][p];
        let i = self.ds[p].iter().position(|&x| x == d).unwrap();
        let l = i.checked_sub(1).map(|i| self.ds[p][i]);
        let r = self.ds[p][i + 1];
        self.score -= change_penalty(l, r, d, self.input.c[p]);
        self.ds[p].remove(i);

        // replace to q
        self.score += self.input.s[d][q];
        self.out[d] = q;
        let i = self.ds[q].iter().position(|&x| x > d).unwrap();
        self.ds[q].insert(i, d);
        let l = i.checked_sub(1).map(|i| self.ds[q][i]);
        let r = self.ds[q][i + 1];
        self.score += change_penalty(l, r, d, self.input.c[q]);
    }
}

fn change_penalty(l: Option<usize>, r: usize, p: usize, c: i64) -> i64 {
    assert!(p < r);
    assert!(l == None && p < r || l.unwrap() < p && p < r);
    let mut ret = calc_penalty(l, r, c);
    ret -= calc_penalty(l, p, c);
    ret -= calc_penalty(Some(p), r, c);
    ret
}

fn calc_penalty(prev: Option<usize>, v: usize, c: i64) -> i64 {
    let d = if let Some(prev) = prev {
        v - prev
    } else {
        v + 1
    };
    (d * (d - 1) / 2) as i64 * c
}

fn calc_score(input: &Input, out: Vec<usize>) -> State {
    let mut score = 0;
    let mut ds = vec![vec![]; K];
    for i in 0..out.len() {
        score += input.s[i][out[i]];
        ds[out[i]].push(i);
    }
    for v in ds.iter_mut() {
        v.push(out.len())
    }
    for (q, set) in ds.iter().enumerate() {
        let mut prev: Option<usize> = None;
        for &v in set {
            score -= calc_penalty(prev, v, input.c[q]);
            prev = Some(v);
        }
    }
    State {
        out,
        score,
        ds,
        input,
    }
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    let input = get_input(&mut it);
    parse!(it, mut t: [u1; input.d], m: usize);
    let mut state = State::new(&input, t);
    for _ in 0..m {
        parse!(it, d: u1, q: u1);
        state.change(d, q);
        println!("{}", state.score);
    }
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}


Measured memory_bytes: 2596