#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T>
using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

struct timer {
    std::chrono::system_clock::time_point start, end;

    timer() {
        start = std::chrono::system_clock::now();
    }

    double get() {
        end = std::chrono::system_clock::now();
        return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    }
};

int D;
int c[26], s[365][26], t[365], ans[366];
vector<set<int>> last(26);

void calc() {
    for (int i = 0; i < D; ++i) {
        ans[i + 1] = ans[i] + s[i][t[i]];
        last[t[i]].emplace(i + 1);
        for (int j = 0; j < 26; ++j) {
            int tmp = *last[j].rbegin();
            if (i + 1 < tmp)
                ans[i + 1] -= c[j] * (i + 1);
            else
                ans[i + 1] -= c[j] * (i + 1 - tmp);
        }
    }
}

void change(int d, int q) {
    int old = t[d];
    if (old == q)
        return;

    t[d] = q;
    last[old].erase(d + 1);
    auto itr1 = last[q].lower_bound(d + 1);
    auto itr2 = last[old].lower_bound(d + 1);
    --itr1, --itr2;
    ans[D] += s[d][q] - s[d][old];
    ans[D] += c[q] * (d + 1 - *itr1) * (*next(itr1, 1) - d - 1);
    ans[D] -= c[old] * (d + 1 - *itr2) * (*next(itr2, 1) - d - 1);
    last[q].emplace(d + 1);
}

void greedy() {
    for (int i = 0; i < D - 1; ++i) {
        ans[i + 2] = INT_MIN;
        int maidx1 = 0, maidx2 = 0;
        for (int k1 = 0; k1 < 26; ++k1) {
            int old1 = *last[k1].rbegin();
            last[k1].emplace(i + 1);
            for (int k2 = 0; k2 < 26; ++k2) {
                int res = ans[i] + s[i][k1] + s[i + 1][k2], res1;
                int old2 = *last[k2].rbegin();
                for (int j = 0; j < 26; ++j)
                    res -= c[j] * (i + 1 - *last[j].rbegin());
                res1 = res;
                last[k2].emplace(i + 2);
                for (int j = 0; j < 26; ++j)
                    res -= c[j] * (i + 2 - *last[j].rbegin());

                if (ans[i + 2] < res) {
                    ans[i + 2] = res;
                    ans[i + 1] = res1 - s[i + 1][k2];
                    maidx1 = k1;
                    maidx2 = k2;
                }
                last[k2].erase(i + 2);
            }
            last[k1].erase(i + 1);
        }
        t[i] = maidx1;
        last[t[i]].emplace(i + 1);
        if (i + 1 == D - 1) {
            t[i + 1] = maidx2;
            last[t[i + 1]].emplace(i + 2);
        }
    }
}

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

    cin >> D;
    for (int i = 0; i < 26; ++i)
        cin >> c[i];
    for (int i = 0; i < D; ++i)
        for (int j = 0; j < 26; ++j)
            cin >> s[i][j];

    for (int i = 0; i < 26; ++i)
        last[i].emplace(0);

    for (int i = 0; i < D; ++i) {
        cin >> t[i];
        t[i]--;
    }
    calc();
    for (int i = 0; i < 26; ++i)
        last[i].emplace(D + 1);

    int M;
    cin >> M;
    for (int i = 0; i < M; ++i) {
        int d, q;
        cin >> d >> q;
        d--, q--;
        change(d, q);
        cout << ans[D] << '\n';
    }
    return 0;
}


Measured memory_bytes: 3676