#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
//typedef vector<vector<ll>> Graph;

const ll mod = 1e9 + 7;
//const ll mod = 998244353;

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}

template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<endl;}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<endl;};

string num2bit(ll num, ll len){
    string bit = "";
    REP(i, len){
    bit += char('0'+(num>>i & 1));
    }
    return bit;
}

ll calc_score(ll D, vector<ll> &c, vector<vector<ll>> &s, vector<ll> &t){
    ll res = 0;
    vector<ll> last(26, -1);
    REP(d, D){
        res += s[d][t[d]];
        last[t[d]] = d;
        REP(i, 26){
            res -= c[i] * (d  - last[i]);
        }
        //cout << res << endl;
    }
    return res;
}

ll change_score(ll D, vector<ll> &c, vector<vector<ll>> &s, vector<ll> &t, ll d, ll q, vector<set<ll>> &dict){
    ll delta = 0;
    ll p = t[d];

    // dump(p)
    // dump(q)

    // single score
    delta -= s[d][p];
    delta += s[d][q];

    // lazy score
    auto itr = dict[p].lower_bound(d);
    ll mp = *(--itr);
    itr++, itr++;
    ll ap = *itr;
    itr--;
    dict[p].erase(itr);

    ll delta1 = (-1) * c[p] * (d-mp) * (ap-d);

    dict[q].insert(d);
    itr = dict[q].lower_bound(d);
    ll mq = *(--itr);
    itr++, itr++;
    ll aq = *itr;

    ll delta2 = c[q] * (d-mq) * (aq-d);

    delta += delta1;
    delta += delta2;

    // update
    t[d] = q;
    return delta;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll D;
    cin >> D;
    vector<ll> c(26);
    vector<vector<ll>> s(D, vector<ll>(26, 0));
    vector<ll> t(D);

    REP(i, 26) cin >> c[i];
    REP(i, D){
        REP(j, 26){
            cin >> s[i][j];
        }
    }
    REP(i, D) cin >> t[i];
    REP(i, D) t[i]--;

    vector<set<ll>> dict(26);
    REP(i, 26) dict[i].insert(-1);
    REP(i, D) dict[t[i]].insert(i);
    REP(i, 26) dict[i].insert(D);

    ll res = calc_score(D, c, s, t);
    //dump(res)

    ll M;
    cin >> M;
    REP(m, M){
        ll d, q;
        cin >> d >> q;
        d--, q--;
        auto delta = change_score(D, c, s, t, d, q, dict);
        res += delta;
        cout << res << endl;
    }


    return 0;
}

Measured memory_bytes: 3780