#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;
}

int cost(int a, int b) {
    int d = b - a;
    return d * (d - 1) / 2;
}

struct State {
    vi out;
    int score;
    vvi ds;

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

        vvi dsn(365, vi(26));
        rep(d, D) dsn[out[d]].push_back(d + 1);
        ds = dsn;
    }

    void change(int d, int i) {
        int old_i = out[d];

        int p = find(ds[old_i].begin(), ds[old_i].end(), d + 1) - ds[old_i].begin();
        int prev, next;
        if (p - 1 >= 0)
            prev = ds[old_i][p - 1];
        else
            prev = 0;
        if (p + 1 < ds[old_i].size())
            next = ds[old_i][p + 1];
        else
            next = D + 1;
        ds[old_i].erase(ds[old_i].begin() + p);
        score += (cost(prev, d + 1) + cost(d + 1, next) - cost(prev, next)) * c[old_i];

        p = find_if(ds[i].begin(), ds[i].end(), [=](auto x) { return x > d + 1; }) - ds[i].begin();
        if (p - 1 >= 0)
            prev = ds[i][p - 1];
        else
            prev = 0;
        if (p < ds[i].size())
            next = ds[i][p];
        else
            next = D + 1;
        ds[i].insert(ds[i].begin() + p, d + 1);
        score -= (cost(prev, d + 1) + cost(d + 1, next) - cost(prev, next)) * c[i];

        score += s[d][i] - s[d][old_i];

        out[d] = i;
        // score = compute_score(out);
    }
};

int main() {
    scanf("%d", &D);
    rep(i, 26) scanf("%d", &c[i]);
    rep(i, D) rep(j, 26) scanf("%d", &s[i][j]);

    vi t(D);
    rep(i, D) {
        scanf("%d", &t[i]);
        t[i]--;
    }

    int M;
    scanf("%d", &M);
    vi d(M), q(M);
    rep(i, M) {
        scanf("%d %d", &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;
    }

    return 0;
}


Measured memory_bytes: 4160