#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

int get_random(int l, int r) {
  int k = r - l + 1;
  return l + rng() % k;
}

const int N = 365 + 7;
const int K = 26 + 7;
int n;
int c[K], s[N][K], t[N], last[K], sol[N], best = -(int) 2e9, univ;
set<int> pos[K];

void put_univ() {
  univ = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 26; j++) {
      univ -= c[j] * i;
    }
  }
  for (int j = 1; j <= 26; j++) {
    pos[j].clear();
    pos[j].insert(0);
    pos[j].insert(n + 1);
    last[j] = 0;
  }
  for (int i = 1; i <= n; i++) {
    pos[t[i]].insert(i);
  }
  int grow = 0;
  for (int i = 1; i <= n; i++) {
    univ += s[i][t[i]];
    grow += c[t[i]] * (i - last[t[i]]);
    last[t[i]] = i;
    univ += grow;
  }
}

void upd_univ(int i, int x) {
  univ -= s[i][t[i]];
  univ += s[i][x];
  auto it = pos[t[i]].lower_bound(i + 1);
  auto ant = it;
  ant--;
  ant--;
  int init = univ;
  univ -= c[t[i]] * i * (*it - i);
  univ += c[t[i]] * (*ant) * (*it - i);

  it = pos[x].lower_bound(i + 1);
  ant = it;
  ant--;
  univ -= c[x] * (*ant) * (*it - i);
  univ += c[x] * i * (*it - i);

  pos[t[i]].erase(i);
  pos[x].insert(i);
  t[i] = x;
}

int get() {
  for (int j = 1; j <= 26; j++) {
    last[j] = 0;
  }
  int sol = 0, grow = 0;
  for (int i = 1; i <= n; i++) {
    sol += s[i][t[i]];
    grow += c[t[i]] * (i - last[t[i]]);
    last[t[i]] = i;
    sol += grow;
  }
  return sol;
}

void upd() {
  int cur = get();
  if (cur > best) {
    best = cur;
    for (int i = 1; i <= n; i++) {
      sol[i] = t[i];
    }
  }
}

void make_better(double seconds) {
  time_t bg = clock();
  int c = 0;
  int cur = get();
  while (1) {
    c++;
    if (c % 10 == 0) {
      time_t nd = clock();
      if ((double) (nd - bg) / CLOCKS_PER_SEC > seconds) {
        break;
      }
    }
    int i = get_random(1, n), x = get_random(1, 26);
    while (x == t[i]) {
      x = get_random(1, 26);
    }
    int init = t[i];
    t[i] = x;
    int after = get();
    if (after > cur) {
      cur = after;
    } else {
      t[i] = init;
    }
  }
  upd();
}

void generate_max() {
  for (int i = 1; i <= n; i++) {
    t[i] = 1;
    for (int j = 1; j <= 26; j++) {
      if (s[i][j] > s[i][t[i]]) {
        t[i] = j;
      }
    }
  }
}

void generate_random(int cnt) {
  generate_max();
  int modif = get_random(1, cnt);
  while (modif--) {
    int i = get_random(1, n), x = get_random(1, 26);
    while (t[i] == x) {
      x = get_random(1, 26);
    }
    t[i] = x;
  }
}

void generate_random2(int cnt) {
  t[1] = 1;
  for (int j = 1; j <= 26; j++) {
    if (c[j] > c[t[1]]) {
      t[1] = j;
    }
  }
  for (int i = 2; i <= n; i++) {
    t[i] = t[1];
  }
  int modif = get_random(1, cnt);
  while (modif--) {
    int i = get_random(1, n), x = get_random(1, 26);
    while (t[i] == x) {
      x = get_random(1, 26);
    }
    t[i] = x;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= 26; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }

  for (int i = 1; i <= n; i++) {
    cin >> t[i];
  }
  put_univ();
  int m;
  cin >> m;
  while (m--) {
    int i, x;
    cin >> i >> x;
    upd_univ(i, x);
    cout << univ << "\n";
  }
  return 0;
  for (int i = 1; i <= 150; i++) {
    generate_random(i);
    make_better(0.01);
    upd();
  }
  for (int i = 1; i <= 15; i++) {
    generate_random2(i);
    make_better(0.01);
    upd();
  }
  generate_max();
  make_better(0.2);
  upd();
  for (int i = 1; i <= n; i++) {
    cout << sol[i] << "\n";
  }
  return 0;
}


Measured memory_bytes: 3740