#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

struct Timer {
  Timer() { reset(); }
  void reset() { beg = chrono::high_resolution_clock::now(); }
  ll elapsed() const {
    chrono::high_resolution_clock::time_point en = chrono::high_resolution_clock::now();
    return chrono::duration_cast<chrono::milliseconds>(en - beg).count();
  }
private:
  chrono::high_resolution_clock::time_point beg;
} timer;

struct Xor128 {
  int rand() {
    unsigned t = x ^ (x << 11);
    x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return static_cast<int>(w);
  }
  int rand(int ub) {
    int res = rand() % ub;
    return res < 0 ? res + ub : res;
  }
  int rand(int lb, int ub) { return lb + rand(ub - lb); }
  ll randll() {
    unsigned long long res = static_cast<unsigned long long>(rand()) << 32;
    return static_cast<ll>(res | rand());
  }
  ll randll(ll ub) {
    ll res = randll() % ub;
    return res < 0 ? res + ub : res;
  }
  ll randll(ll lb, ll ub) { return lb + randll(ub - lb); }
private:
  unsigned x = 123456789, y = 362436069, z = 521288629, w = static_cast<unsigned>(time(nullptr));
} xor128;

const int D = 365, N = 26;
int c[N], s[D][N];

void input() {
  int d; cin >> d;
  REP(i, N) cin >> c[i];
  REP(i, D) REP(j, N) cin >> s[i][j];
}

void output(const vector<int> &t) {
  REP(i, D) cout << t[i] << '\n';
}

int calc_score(const vector<int> &t) {
  vector<int> last(N, -1);
  int score = 0;
  REP(d, D) {
    score += s[d][t[d]];
    last[t[d]] = d;
    REP(i, N) score -= c[i] * (d - last[i]);
  }
  return score;
  // return max(score + 1000000, 0);
}

vector<int> greedy() {
  vector<int> t(D), last(N, -1);
  REP(d, D) {
    int minus = 0;
    REP(i, N) minus += c[i] * (d - last[i]);
    int mx = -INF, idx;
    REP(i, N) {
      int tmp = s[d][i] + c[i] * (d - last[i]);
      if (tmp > mx) {
        mx = tmp;
        idx = i;
      }
    }
    t[d] = idx;
    last[idx] = d;
  }
  return t;
}

int sub(int idx, int len) {
  return c[idx] * len * (len + 1) / 2;
}

int main() {
  input();
  vector<int> t(D); REP(i, D) cin >> t[i], --t[i];
  vector last(N, set<int>{-1, D});
  int score = 0;
  REP(i, D) {
    score += s[i][t[i]];
    last[t[i]].emplace(i);
  }
  REP(i, N) {
    for (auto it = next(last[i].begin()); it != last[i].end(); ++it) {
      score -= sub(i, *it - *prev(it) - 1);
    }
  }
  assert(calc_score(t) == score);
  int m; cin >> m;
  while (m--) {
    int d, q; cin >> d >> q; --d; --q;
    int p = t[d];
    score -= s[d][p];
    auto it = last[p].lower_bound(d);
    score += sub(p, *it - *prev(it) - 1);
    score += sub(p, *next(it) - *it - 1);
    score -= sub(p, *next(it) - *prev(it) - 1);
    last[p].erase(it);
    score += s[d][q];
    it = last[q].emplace(d).first;
    score -= sub(q, *it - *prev(it) - 1);
    score -= sub(q, *next(it) - *it - 1);
    score += sub(q, *next(it) - *prev(it) - 1);
    t[d] = q;
    cout << score << '\n';
  }
  // output(t);
  return 0;
}


Measured memory_bytes: 3656