#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i = 0; (i) < ((int)(n)); ++(i))

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

    vector<int> t(D);
    rep(i, D) {
        cin >> t[i];
        t[i]--;
    }

    vector<int> ans(D);
    vector<vector<ll>> Cost(D, vector<ll>(26, 0));
    rep(i, 26) Cost[0][i] = c[i];
    ll v = 0;
    rep(i, D) {
        int index = t[i];
        Cost[i][index] = 0;
        v += s[i][index];
        rep(j, 26) {
            v -= Cost[i][j];
            if(i + 1 < D)
                Cost[i + 1][j] = Cost[i][j] + c[j];
        }
        // cout << v << endl;
    }
    // cout << v << endl;
    int M;
    cin >> M;
    rep(i, M) {
        int d, q;
        cin >> d >> q;
        d--;
        q--;
        int x = d + 1;
        v += s[d][q] - s[d][t[d]];
        v += Cost[d][q];
        Cost[d][q] = 0;
        while(x < D && t[x] != q) {
            v += Cost[x][q];
            Cost[x][q] = Cost[x - 1][q] + c[q];
            v -= Cost[x][q];
            x++;
        }

        x = d + 1;
        int y = t[d];
        if(d == 0) {
            Cost[d][y] = c[y];
            v -= Cost[d][y];
        } else {
            Cost[d][y] = Cost[d - 1][y] + c[y];
            v -= Cost[d][y];
        }
        while(x < D && t[x] != y) {
            v += Cost[x][y];
            Cost[x][y] = Cost[x - 1][y] + c[y];
            v -= Cost[x][y];
            x++;
        }
        t[d] = q;
        cout << v << endl;
    }
}