#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

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

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int D; cin >> D;
	vector<int> c(26, 0);
	rep (i, 26){
		cin >> c[i];
	} // end rep
	vector<vector<int>> s(D, vector<int>(26, 0));
	rep (i, D){
		rep (j, 26){
			cin >> s[i][j];
		} // end rep
	} // end rep

	vector<int> t(D+1, 0);
	rep (i, D){
		cin >> t[i+1];
		t[i+1]--;
	} // end rep

    vector<set<int>> st(26);
    rep (i, 26){
        st[i].insert(0);
        st[i].insert(D+1);
    } // end rep
    vector<int> sat(D+1, 0);
    vector<int> last(26, 0);

    for (int d = 1; d <= D; d++){
        st[t[d]].insert(d);
        sat[d] = sat[d-1] + s[d-1][t[d]];
        last[t[d]] = d;
        rep (j, 26){
            sat[d] -= (d - last[j]) * c[j];
        } // end rep
    } // end for

    int M; cin >> M;
    vector<int> ans(M+1, 0);
    ans[0] = sat[D];

    rep (i, M){
        int d, q; cin >> d >> q;
        q--;
        int p = t[d];
        st[p].erase(d);

        auto itrp = st[p].lower_bound(d), itrq = st[q].lower_bound(d);
        int rp = *itrp, rq = *itrq;
        itrp--; itrq--;
        int lp = *itrp, lq = *itrq;

        int diff = s[d-1][q] - s[d-1][p];
        diff -= (d - lp) * (rp - d) * c[p];
        diff += (d - lq) * (rq - d) * c[q];
        ans[i+1] = ans[i] + diff;

        t[d] = q;
        st[q].insert(d);
    } // end rep

    for (int i = 1; i <= M; i++){
        cout << ans[i] << endl;
    } // end for

	return 0;
}
