#![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<BTreeSet<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];
        self.ds[p].remove(&d);
        let l = self.ds[p].range(..d).rev().next().cloned();
        let r = *self.ds[p].range(d..).next().unwrap();
        self.score -= change_penalty(l, r, d, self.input.c[p]);

        // replace to q
        self.score += self.input.s[d][q];
        self.out[d] = q;
        let l = self.ds[q].range(..d).rev().next().cloned();
        let r = *self.ds[q].range(d..).next().unwrap();
        self.score += change_penalty(l, r, d, self.input.c[q]);
        self.ds[q].insert(d);
    }
}

fn change_penalty(l: Option<usize>, r: usize, p: usize, c: i64) -> i64 {
    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![BTreeSet::new(); K];
    for set in ds.iter_mut() {
        set.insert(out.len());
    }
    for i in 0..out.len() {
        score += input.s[i][out[i]];
        ds[out[i]].insert(i);
    }
    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);
    debug!("{:?}", input); // DEBUG
    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: 2748