#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <stack>
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <cfloat>
#include <math.h>
#include <numeric>
#include <string.h>
#include <iomanip>
#include <sys/time.h>
#include <random>
#include <cassert>


#define fs first
#define sc second
#define endl "\n"

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<int, int> P;


int get_scores(vector<int>& c, vector<vector<int>>& s, vector<int>& t, int D){
    vector<int> las(26, -1);
    int score = 0;
    for(int i = 0; i < D; i++){
        int contest = t[i];
        int up = s[i][contest];
        int down = 0;
        las[contest] = i;
        for(int j = 0; j < 26; j++){
            down += c[j] * (i - las[j]);
        }
        score += (up - down);
    }
    return score;
}

void make_info1(int j, vector<int>& t, vector<vector<int>>& res1, int D){
    for(int i = 1; i < D; i++){
        if(j == t[i-1]){
            res1[i][j] = i-1;
        }
        else{
            res1[i][j] = res1[i-1][j];
        }
    }
}

void make_info2(int j, vector<int>& t, vector<vector<int>>& res2, int D){
    for(int i = D-2; i >= 0; i--){
        if(j == t[i+1]){
            res2[i][j] = i+1;
        }
        else{
            res2[i][j] = res2[i+1][j];
        }
    }
}

void make_info(vector<int>& t, int D, vector<vector<int>>& res1, vector<vector<int>>& res2){
    for(int j = 0; j < 26; j++){
        make_info1(j, t, res1, D);
    }

    for(int j = 0; j < 26; j++){
        make_info2(j, t, res2, D);
    }
}


int get_dif(int d, int q, vector<int>& c, vector<vector<int>>& s, vector<int>& t, vector<vector<int>>& before, vector<vector<int>>& after, int D){
    int dif1 = s[d][q] - s[d][t[d]];

    int bef1 = before[d][q];
    int nex1 = after[d][q];
    int up1 = (d - bef1) * c[q] * (nex1 - d);

    int bef2 = before[d][t[d]];
    int nex2 = after[d][t[d]];
    int down1 = (d - bef2) * c[t[d]] * (nex2 - d);

    int dif2 = up1 - down1;

    return dif1 + dif2;
}


int main(){
    int D;  cin >> D;
    vector<int> c(26);
    for(int i = 0; i < 26; i++) cin >> c[i];
    vector<vector<int>> s(D, vector<int>(26));
    for(int i = 0; i < D; i++){
        for(int j = 0; j < 26; j++){
            cin >> s[i][j];
        }
    }

    vector<int> t(D);
    for(int i = 0; i < D; i++){
        cin >> t[i];
        t[i]--;
    }

    int score = get_scores(c, s, t, D);

    vector<vector<int>> before(D, vector<int>(26, -1));
    vector<vector<int>> after(D, vector<int>(26, D));
    make_info(t, D, before, after);

    int M;  cin >> M;

    for(int i = 0; i < M; i++){
        int d, q;   cin >> d >> q;
        d--; q--;
        int dif = get_dif(d, q, c, s, t, before, after, D);
        score += dif;
        cout << score << endl;

        int before_q = t[d];
        t[d] = q;
        make_info1(q, t, before, D);
        make_info2(q, t, after, D);
        make_info1(before_q, t, before, D);
        make_info2(before_q, t, after, D);
    }


    return 0;
}


Measured memory_bytes: 3724