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

int main(){
    int d, m;
    cin >> d;
    vector<int> c(26);
    vector<vector<int>> s(d, vector<int>(26));
    vector<int> t(d);
    for(auto &&n : c) cin >> n;
    for(auto &&n1 : s) for(auto &&n2 : n1) cin >> n2;
    for(auto &&n : t){
        cin >> n;
        --n;
    }
    cin >> m;

    vector<int> l(26);

    vector<int> v(26);

    int sum, o, n, temp;
    
    rep(i, d){
        v[t[i]] += s[i][t[i]];
        l[t[i]] = -1;
        rep(j, 26) v[j] -= c[j] * (++l[j]);
        rep(j, 26) sum += v[j];
    }

    rep(i, m){
        sum = 0;
        cin >> temp >> n;
        o = t[--temp];
        t[temp] = --n;
        v[o] = v[n] = l[o] = l[n] = 0;
        rep(j, d){
            if(t[j] == o || t[j] == n){
                v[t[j]] += s[j][t[j]];
                l[t[j]] = -1;
            }
            v[o] -= c[o] * ++l[o];
            v[n] -= c[n] * ++l[n];
        }
        rep(j, 26) sum += v[j];
        cout(sum);
    }
}
