// Template
#include "bits/stdc++.h"
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T> 
inline bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
struct IOSET {
    IOSET() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
    }
} ioset;

// Main
int main() {
    int d;
    cin >> d;
    vector<int> c(26);
    rep(i, 26) cin >> c[i];
    vector<vector<int>> s(d, vector<int>(26));
    rep(i, d) rep(j, 26) cin >> s[i][j];

    vector<int> t(d);
    vector<vector<int>> ds(26);
    int score = 0;
    auto rescore = [&](int x, int y) {
        if (y == t[x]) return;
        int mn = -inf, mx = inf;
        for (int i : ds[t[x]]) {
            if (i < x) chmax(mn, i);
            if (i > x) chmin(mx, i);
        }
        score -= s[x][t[x]];
        score += (x - mn) * (x - mn - 1) / 2 * c[t[x]];
        score += (mx - x) * (mx - x - 1) / 2 * c[t[x]];
        score -= (mx - mn) * (mx - mn - 1) / 2 * c[t[x]];
        mn = -inf, mx = inf;
        for (int i : ds[y]) {
            if (i < x) chmax(mn, i);
            if (i > x) chmin(mx, i);
        }
        score += s[x][y];
        score += (mx - mn) * (mx - mn - 1) / 2 * c[y];
        score -= (x - mn) * (x - mn - 1) / 2 * c[y];
        score -= (mx - x) * (mx - x - 1) / 2 * c[y];
        vector<int> new_;
        for (int i : ds[t[x]]) if (i != x) new_.push_back(i);
        ds[t[x]] = new_;
        new_.clear();
        rep(i, ds[y].size()) {
            if (ds[y][i] > x && ds[y][i - 1] < x) new_.push_back(x);
            new_.push_back(ds[y][i]);
        }
        ds[y] = new_;
        t[x] = y;
    };

    /*
    vector<int> last(26, -1);
    rep(i, d) {
        int mx = -inf, idx = -1;
        rep(j, 26) {
            if (chmax(mx, (i - last[j]) * c[j] + s[i][j])) idx = j;
        }
        t[i] = idx;
    }
    last = vector<int>(26, -1);
    rep(i, d) {
        score += s[i][t[i]];
        last[t[i]] = i;
        rep(j, 26) score -= (i - last[j]) * c[j];
    }
    */
    rep(i, 26) ds[i].push_back(-1);
    rep(i, d) {
        cin >> t[i];
        --t[i];
        ds[t[i]].push_back(i);
    }
    rep(i, 26) ds[i].push_back(d);
    vector<int> last(26, -1);
    rep(i, d) {
        score += s[i][t[i]];
        last[t[i]] = i;
        rep(j, 26) score -= (i - last[j]) * c[j];
    }
    int m;
    cin >> m;
    rep(i, m) {
        int d_, q;
        cin >> d_ >> q;
        --d_; --q;
        rescore(d_, q);
        cout << score << "\n";
        /*
        rep(i, 26) {
            if (ds[i].size()) cout << i << ": ";
            rep(j, ds[i].size()) cout << ds[i][j] << " \n"[j == ds[i].size() - 1];
        }
        */
    }
}

Measured memory_bytes: 3692