use proconio::{input, marker::Usize1};
use std::iter::once;

const ALPHA: usize = 26;

#[derive(Debug, Clone)]
struct Contest {
    pub miss: i64,
    pub happiness: Vec<i64>,
    pub schedule: Vec<bool>,
}

impl Contest {
    fn with_args(miss: i64, happiness: Vec<i64>, schedule: Vec<bool>) -> Self {
        Self {
            miss,
            happiness,
            schedule,
        }
    }

    #[allow(dead_code)]
    fn day_score(&self, d: usize) -> i64 {
        if self.schedule[d] {
            self.happiness[d]
        } else {
            -(self.schedule[0..=d]
                .iter()
                .rev()
                .copied()
                .chain(once(true))
                .take_while(|x| !x)
                .count() as i64)
                * self.miss
        }
    }

    fn score(&self) -> i64 {
        self.happiness
            .iter()
            .zip(self.schedule.iter())
            .scan(0, |state, (&h, &s)| {
                Some(if s {
                    *state = 0;
                    h
                } else {
                    *state += 1;
                    -self.miss * *state
                })
            })
            .sum()
    }

    fn unset_slow(&mut self, d: usize) -> i64 {
        let ret = self.score();
        assert!(self.schedule[d]);
        self.schedule[d] = false;
        self.score() - ret
    }

    fn set_slow(&mut self, d: usize) -> i64 {
        let ret = self.score();
        assert!(!self.schedule[d]);
        self.schedule[d] = true;
        self.score() - ret
    }
}

fn main() {
    input!(
        dmax: usize,
        c: [i64; ALPHA],
        s: [[i64; ALPHA]; dmax],
        mut t: [Usize1; dmax],
        q: usize,
        dq: [(Usize1, Usize1); q],
    );

    let mut contests: Vec<_> = (0..ALPHA)
        .map(|i| {
            let c = Contest::with_args(
                c[i],
                s.iter().map(|row| row[i]).collect(),
                t.iter().copied().map(|j| j == i).collect(),
            );
            c
        })
        .collect();

    let mut score = contests.iter().map(|c| c.score()).sum::<i64>();
    for (d, q) in dq.into_iter() {
        let p = t[d];
        t[d] = q;
        score += contests[p].unset_slow(d);
        score += contests[q].set_slow(d);
        println!("{}", score);
    }
}

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

    #[test]
    fn test_day_score() {
        let miss = 4;
        let happiness = vec![10, 20, -10, 20, 0];
        let schedule = vec![false, false, true, true, false];
        let contest = Contest {
            miss,
            happiness,
            schedule,
        };
        assert_eq!(contest.day_score(0), -4);
        assert_eq!(contest.day_score(1), -8);
        assert_eq!(contest.day_score(2), -10);
        assert_eq!(contest.day_score(3), 20);
        assert_eq!(contest.day_score(4), -4);
    }

    #[test]
    fn test_score() {
        let dmax = 5;
        let miss = 4;
        let happiness = vec![10, 20, -10, 20, 0];
        let schedule = vec![false, false, true, true, false];
        let contest = Contest {
            miss,
            happiness,
            schedule,
        };
        assert_eq!(
            contest.score(),
            (0..dmax).map(|d| contest.day_score(d)).sum()
        );
    }

    #[test]
    fn test_unset_set_slow() {
        let miss = 4;
        let happiness = vec![10, 20];
        let schedule = vec![true, true];
        let mut contest = Contest {
            miss,
            happiness,
            schedule,
        };
        assert_eq!(contest.unset_slow(1), -24);
        assert_eq!(contest.set_slow(1), 24);
    }
}


Measured memory_bytes: 4332