use proconio::{fastout, input};

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;

struct State {
    t: Vec<usize>,
    score: i64,
    eventDays: Vec<Vec<usize>>,
}

struct Input {
    d: usize,
    c: Vec<i64>,
    s: Vec<[i64; 26 + 1]>,
}

// 0日目,最終日
fn cost(a: usize, b: usize) -> i64 {
    // println!("{} {}", a, b);
    let d = b - a;
    (d * (1 + d) / 2) as i64
}

impl State {
    fn new(input: &Input, t: Vec<usize>) -> State {
        let mut eventDays = vec![vec![]; 26 + 1];
        for day in 1..=input.d {
            eventDays[t[day]].push(day);
        }
        let score = calc_score(&input, &t);
        State {
            t,
            score,
            eventDays,
        }
    }
    // day: 変更日, new_contest: コンテスト番号
    fn change(&mut self, input: &Input, day: usize, new_contest: usize) {
        let old_contest = self.t[day];
        // println!("event {:?}", self.eventDays[old_contest]);

        let p = self.eventDays[old_contest]
            .iter()
            .position(|a| *a == day)
            .unwrap();
        let prev_day = if p == 0 {
            0
        } else {
            self.eventDays[old_contest].get(p - 1).cloned().unwrap()
        };
        let next_day = self.eventDays[old_contest]
            .get(p + 1)
            .cloned()
            .unwrap_or(input.d + 1);
        self.eventDays[old_contest].remove(p);
        self.score += (cost(prev_day, day - 1) + cost(day, next_day - 1)
            - cost(prev_day, next_day - 1))
            * input.c[old_contest];

        let p = self.eventDays[new_contest]
            .iter()
            .position(|a| *a > day)
            .unwrap_or(self.eventDays[new_contest].len());
        let prev_day = if p == 0 {
            0
        } else {
            self.eventDays[new_contest].get(p - 1).cloned().unwrap()
        };
        let next_day = self.eventDays[new_contest]
            .get(p)
            .cloned()
            .unwrap_or(input.d + 1);
        self.eventDays[new_contest].insert(p, day);
        self.score -= (cost(prev_day, day - 1) + cost(day, next_day - 1)
            - cost(prev_day, next_day - 1))
            * input.c[new_contest];
        self.score += input.s[day][new_contest] - input.s[day][old_contest];
        self.t[day] = new_contest;
    }
}

fn calc_score(input: &Input, t: &Vec<usize>) -> i64 {
    let mut res = 0;
    let mut last: [usize; 26 + 1] = [0; 26 + 1];
    for day in 1..=input.d {
        let now_contest = t[day];
        res += input.s[day][now_contest];
        last[now_contest] = day;
        for contest in 1..=26 {
            res -= input.c[contest] * (day - last[contest]) as i64;
        }
    }
    res
}

#[fastout]
fn main() {
    // input
    input! {
        d: usize,
        _c: [i64; 26],
        _s: [[i64; 26]; d],
        _t: [usize; d],
        m: usize,
        dq: [(usize, usize); m],
    }

    let mut c = vec![0];
    c.extend(_c.iter());

    let mut s = vec![[0; 26 + 1]; d + 1];
    for day in 1..=d {
        for contest in 1..=26 {
            s[day][contest] = _s[day - 1][contest - 1]
        }
    }

    let input = Input { d, c, s };

    let mut t = vec![0];
    t.extend(_t.iter());

    // process
    let mut state = State::new(&input, t);
    for (day, q) in dq {
        state.change(&input, day, q);
        println!("{}", state.score);
    }
}


Measured memory_bytes: 4404