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

int D;
int C[26];
int S[365][26];

int64_t evaluate(const vector<int> &T) {
  int64_t res = 0;
  vector<int> last(26);
  for (int i = 0; i < T.size(); ++i) {
    res += S[i][T[i] - 1];
    last[T[i] - 1] = i + 1;
    for (int j = 0; j < 26; ++j) {
      res -= C[j] * (i + 1 - last[j]);
    }
  }
  return res;
}

int64_t evaluate_modify(const vector<int> &T, int d, int q, int64_t v) {
  if (T[d - 1] == q) return v;
  int64_t res = v - S[d - 1][T[d - 1] - 1] + S[d - 1][q - 1];
  auto sum = [](int n) { return (n + 1) * n / 2; };
  for (int x : { T[d - 1], q }) {
    int l, r;
    for (l = d - 2; ~l; --l) if (T[l] == x) break;
    for (r = d; r < T.size(); ++r) if (T[r] == x) break;
    res += (x == q ? -1 : +1) * C[x - 1] * sum((d - 1) - l - 1);
    res += (x == q ? -1 : +1) * C[x - 1] * sum(r - (d - 1) - 1);
    res -= (x == q ? -1 : +1) * C[x - 1] * sum(r - l - 1);
  }
  return res;
}

int main() {
  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];

  vector<int> T(D);
  for (int i = 0; i < D; ++i) cin >> T[i];

  int v = evaluate(T);

  int M;
  cin >> M;
  for (int i = 0; i < M; ++i) {
    int d, q;
    cin >> d >> q;
    v = evaluate_modify(T, d, q, v);
    T[d - 1] = q;
    cout << v << endl;
  }


  return 0;
}

