//#define DBG

#include <bits/stdc++.h>

using namespace std;
const int N_CNTST = 26;
int D;
vector <long long> C;
vector <vector<long long> > S;
long long CSum = 0;

vector<int> LAST(26, 0);

vector<int> T;
int main(){
    cin >> D;
    cin.ignore();
    
    long long tmp;
    for(int i = 0; i < N_CNTST; i++){
        cin >> tmp;
        C.push_back(tmp);
        CSum += tmp;
    }
    cin.ignore();
    for(int i = 0; i < D; i++){
        vector<long long> S_;
        for(int j = 0; j < N_CNTST; j++){
            cin >> tmp;
            S_.push_back(tmp);
        }
        cin.ignore();
        S.push_back(S_);
    }

    // tを算出
    long long sum = 0;
    long long msum = 0; // 減算Sum
    for(int i = 0; i < D; i++){
        cin >> tmp;
        T.push_back((int)tmp);
        cin.ignore();
        
        int idx = ((int) tmp) - 1;
        msum += CSum - C[idx] * (i - LAST[idx] + 1);
        LAST[idx] = i+1;
        //cout << CSum - C[idx] << endl;
        //cout << msum << endl;
        sum += S[i][idx] - msum;
#ifdef DBG
        cout << sum << endl;
#endif
    }

#ifdef DBG
    printf("T is:");
    for(int i = 0; i < T.size(); i++){
        printf("%d:%d, ", i, T[i]);
    }
    printf("\n");
#endif

    int M;
    cin >> M;
    cin.ignore();
    int o_idx, chg;
    for(int i = 0; i < M; i++){
        cin >> o_idx >> chg;
        cin.ignore();

        --o_idx;
        int prb = T[o_idx];
        int o_s_idx;
        bool s_found = false;
        for(int j = o_idx - 1; j >= 0; j--){
            if(prb == T[j]){
                o_s_idx = j;
                s_found = true;
                break;
            }
        }
        if(!s_found) o_s_idx = -1;
        int o_e_idx;
        bool e_found = false;
        for(int j = o_idx + 1; j < D; j++){
            if(prb == T[j]){
                o_e_idx = j;
                e_found = true;
                break;
            }
        }
        if(!e_found) o_e_idx = D;
        int chg_s_idx;
        bool chg_s_found = false;
        for(int j = o_idx - 1; j >= 0; j--){
            if(chg == T[j]){
                chg_s_idx = j;
                chg_s_found = true;
                break;
            }
        }
        if(!chg_s_found) chg_s_idx = -1;
        int chg_e_idx;
        bool chg_e_found = false;
        for(int j = o_idx + 1; j < D; j++){
            if(chg == T[j]){
                chg_e_idx = j;
                chg_e_found = true;
                break;
            }
        }
        if(!chg_e_found) chg_e_idx = D;

        int s_days = o_idx - o_s_idx;
        //int s_n_sum = s_days * (1 + s_days) / 2;
        int s_n_sum = s_days;

#ifdef DBG
        printf("s_days %d o_idx %d o_s_idx %d\n", s_days, o_idx, o_s_idx);
#endif
        int e_days = o_e_idx - o_idx;
#ifdef DBG
        printf("e_days %d o_e_idx %d o_idx %d\n", e_days, o_e_idx, o_idx);
#endif
        sum -= s_n_sum * C[T[o_idx]-1] * e_days;
#ifdef DBG
        printf("tempsum %lld s_n_sum %d COST %lld e_days %d\n", sum, s_n_sum, C[T[o_idx]-1], e_days);
#endif

        int chg_s_days = o_idx - chg_s_idx;
#ifdef DBG
        printf("chg_s_days %d o_idx %d chg_s_idx %d\n", chg_s_days, o_idx, chg_s_idx);
#endif
        //int chg_n_sum = chg_s_days * (1 + chg_s_days) / 2;
        int chg_n_sum = chg_s_days;
        int chg_e_days = chg_e_idx - o_idx;
#ifdef DBG
        printf("chg_e_days %d chg_e_idx %d o_idx %d\n", chg_e_days, chg_e_idx, o_idx);
#endif
        sum += chg_n_sum * C[chg-1] * chg_e_days;
#ifdef DBG
        printf("tempsum %lld chg_n_sum %d COST %lld chg_e_days %d\n", sum, chg_n_sum, C[chg-1], chg_e_days);
#endif
        sum += - S[o_idx][T[o_idx]-1] + S[o_idx][chg-1];
        cout << sum << endl;
        T[o_idx] = chg;
    }
    return 0;
}


Measured memory_bytes: 3656