#include <queue>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <set>
#include <tuple>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <chrono>
#include <random>

using namespace std;

class Pts {
public:
    Pts(int n, int d) : mem(n) {
        for (auto &a : mem) {
            a.reserve(d + 1);
            a.push_back(-1);
            a.push_back(d);
        }
    }

    inline pair<int, int> get(int t, int k) {
        auto &a = mem[k];
        auto i = lower_bound(a.begin(), a.end(), t);
        if (*i == t) return {*(i - 1), *(i + 1)};
        else return {*(i - 1), *i};
    }

    inline void set(int t, int i) {
        auto &a = mem[i];
        auto j = lower_bound(a.begin(), a.end(), t);
        a.insert(j, t);
    }

    inline void unset(int t, int i) {
        auto &a = mem[i];
        auto j = find(a.begin(), a.end(), t);
        a.erase(j);
    }

private:
    vector<vector<int>> mem;
};

class Solution {
public:
    Solution(int n, int d) :
            n(n), d(d), cost(0),
            c(n), s(d, vector<int>(n)),
            res(d), pts(n, d) {

        for (auto &x : c) cin >> x;

        for (int t = 0; t < d; t++) {
            for (int i = 0; i < n; i++) {
                cin >> s[t][i];
            }
        }

        for (int t = 0; t < d; t++) {
            int i;
            cin >> i;
            i--;
            res[t] = i;
            cost += s[t][i];
            int q = pts.get(t, i).first;
            cost -= c[i] * (t - q - 1) * (t - q) / 2;
            pts.set(t, i);
        }

        for (int i = 0; i < n; i++) {
            int q = pts.get(d, i).first;
            cost -= c[i] * (d - q - 1) * (d - q) / 2;
        }

        cerr << cost << '\n';
    }

    int64_t estimate(int t, int j) {
        int i = res[t];
        int64_t delta = s[t][j] - s[t][i];
        {
            auto p = pts.get(t, i);
            int l = t - p.first - 1;
            int r = p.second - t - 1;
            delta += c[i] * (l * (l + 1) + r * (r + 1) - (l + r + 1) * (l + r + 2)) / 2;
        }
        {
            auto p = pts.get(t, j);
            int l = t - p.first - 1;
            int r = p.second - t - 1;
            delta -= c[j] * (l * (l + 1) + r * (r + 1) - (l + r + 1) * (l + r + 2)) / 2;
        }
        return delta;
    }

    void change(int t, int j, int64_t delta) {
        int i = res[t];
        res[t] = j;
        pts.unset(t, i);
        pts.set(t, j);
        cost += delta;
    }

    vector<int> &result() {
        return res;
    }

    int64_t get_cost() {
        return cost;
    }

private:
    int n;
    int d;
    int64_t cost;
    vector<int> c;
    vector<vector<int>> s;
    vector<int> res;
    Pts pts;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    const int n = 26;

    int d;
    cin >> d;

    Solution solution(n, d);

    int m;
    cin >> m;
    while (m-- > 0) {
        int t, j;
        cin >> t >> j;
        t--;
        j--;

        int64_t delta = solution.estimate(t, j);
        solution.change(t, j, delta);

        cout << solution.get_cost() << '\n';
    }

    return 0;
}


Measured memory_bytes: 3688