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

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rrepn(i, n) for (int i = ((int)(n)); i > 0; --i)
#define SZ(x) ((int)(x).size())
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 (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

int D;
vi c(26);
vvi s(365, vi(26));

int compute_score(const vi &out) {
    vector<int> last(26);
    int score = 0;
    rep(i, D) {
        score += s[i][out[i]];
        last[out[i]] = i + 1;
        int m = 0;
        rep(j, 26) { m += c[j] * (i + 1 - last[j]); }
        score -= m;
        // cout << score << endl;
    }
    return score;
}

struct State {
    vi out;
    int score;

    State(const vector<int> t) : out(t) { score = compute_score(out); }

    void change(int d, int i) {
        out[d] = i;
        score = compute_score(out);
    }
};

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

    vi t(D);
    rep(i, D) {
        cin >> t[i];
        t[i]--;
    }

    int M;
    cin >> M;
    vi d(M), q(M);
    rep(i, M) {
        cin >> d[i] >> q[i];
        d[i]--;
        q[i]--;
    }

    // rep(i, D) {
    //     int now = 0;
    //     rep(j, 26) {
    //         if (now <= s[i][j]) {
    //             now = s[i][j];
    //             t[i] = j;
    //         }
    //     }
    // }

    State state = State(t);
    rep(i, M) {
        state.change(d[i], q[i]);
        cout << state.score << endl;
    }

    // auto chscore = [&](int nd, int nq) {
    //     vector<int> last(26);
    //     int old = t[nd];

    //     int oldscore = score;

    //     score -= s[nd][t[nd]];
    //     score += s[nd][nq];
    //     t[nd] = nq;
    //     int now = 0;
    //     rep(i, D) {
    //         last[t[i]] = i + 1;
    //         rep(j, 26) now += c[i] * last[j];
    //     }
    //     score += now;
    //     score -= lastsum;

    //     if (score > oldscore) {
    //         t[nd] = old;
    //         score = oldscore;
    //     } else {
    //         lastsum = now;
    //     }
    // };
    // srand((unsigned int)time(NULL));
    // rep(i, 300000) {
    //     int d = rand() % D;
    //     int q = rand() % 26;
    //     int old = t[d];
    //     chscore(d, q);
    // }

    // rep(i, D) { cout << t[i] + 1 << endl; }
    return 0;
}


Measured memory_bytes: 3956