#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <shared_mutex>
#include <boost/format.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/optional.hpp>

#ifdef DEBUG
#include "libs/debug.cpp"
#define DEBUG 1
#else
#define DEBUG 0
#endif

#define rep(i, n) repr((i), 0, (n))
#define repr(i, a, t) reps((i), (a), (t), 1)
#define reps(i, a, t, s) for (long long (i) = (a); (i) < (long long)(t); (i) += (s))
#define rrep(i, n) rrepr((i), (n), 0)
#define rrepr(i, a, t) rreps((i), (a), (t), 1)
#define rreps(i, a, t, s) for (long long (i) = (a) - 1; (i) >= (long long)(t); (i) -= (s))
#define each(v, c) for (auto &&(v) : (c))
#define all(c) (c).begin(), (c).end()

using namespace std;

// コンテナのメンバ関数を使わないと O(N)
template<typename T>
typename set<T>::iterator lower_bound(typename set<T>::iterator l, typename set<T>::iterator r, const T &v);
template<typename T>
typename set<T>::iterator upper_bound(typename set<T>::iterator l, typename set<T>::iterator r, const T &v);
template<typename S, typename T>
typename map<S, T>::iterator lower_bound(typename map<S, T>::iterator l, typename map<S, T>::iterator r, const T &v);
template<typename S, typename T>
typename map<S, T>::iterator upper_bound(typename map<S, T>::iterator l, typename map<S, T>::iterator r, const T &v);

namespace mp = boost::multiprecision;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using ml = mp::cpp_int;
template<typename T>
using optional = boost::optional<T>;
const auto none = boost::none;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;

unsigned int randxor() {
  static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
  unsigned int t;
  t = (x ^ (x << 11));
  x = y;
  y = z;
  z = w;
  return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

/**
 * l 以上 r 以下の整数を返す
 * @param l
 * @param r
 */
int randint(int l, int r) {
  ld rnd = (ld) randxor() / UINT_MAX;
  rnd *= (r - l + 1);
  return int(rnd) + l;
}

/**
 * pair を map のキーにできるようにハッシュ化の方法を定義
 * https://www.techiedelight.com/use-std-pair-key-std-unordered_map-cpp/
 */
struct pair_hash {
  template<class T1, class T2>
  std::size_t operator()(const std::pair<T1, T2> &pair) const {
    return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
  }
};

/**
 * 時間を計る
 * timer("<procedure name>", [&]{
 *   ...
 * });
 * @param name
 * @param fn
 */
void timer(string name, const function<void(void)> &fn) {
  auto st = chrono::system_clock::now();
  fn();
  auto now = chrono::system_clock::now();
  double t = chrono::duration_cast<chrono::milliseconds>(now - st).count();
  cerr << boost::format("[%s] %4d ms") % name % t << "\n";
}

auto calc(ll t, const set<ll> &days, const vector<vector<ll>> &S, const vector<ll> &C) -> ll {
  ll last = -1;
  ll ret = 0;
  each (d, days) {
    ll diff = d - last;
    ret += S[d][t];
    ret -= C[t] * (diff - 1) * diff / 2;
    last = d;
  }
  return ret;

}

auto main() -> int {
  cout << fixed << setprecision(10);
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ifstream ifs;
  if (DEBUG) {
    ifs.open("src/_in.txt");
    cin.rdbuf(ifs.rdbuf());
  }

  ll D;
  cin >> D;
  vector<ll> C;
  rep (i, 26) {
    ll v0;
    cin >> v0;
    C.push_back(v0);
  }
  vector<vector<ll>> S(D, vector<ll>(26));
  rep (i, D) {
    rep (j, 26) {
      cin >> S[i][j];
    }
  }
  vector<ll> T;
  rep (i, D) {
    ll v0;
    cin >> v0;
    T.push_back(v0 - 1);
  }
  ll M;
  cin >> M;
  vector<tuple<ll, ll>> DQ(M);
  rep (i, M) {
    cin >> get<0>(DQ[i]) >> get<1>(DQ[i]);
    get<1>(DQ[i]) -= 1;
    get<0>(DQ[i]) -= 1;
  }

  S.push_back(vector<ll>(26, 0));

  vector<set<ll>> days(26);
  rep (d, D) {
    ll t = T[d];
    days[t].insert(d);
  }
  rep (t, 26) {
    days[t].insert(D);
  }
  vector<ll> scores(26, 0);
  ll total_score = 0;
  rep (t, 26) {
    scores[t] = calc(t, days[t], S, C);
    total_score += scores[t];
  }

  each (dq, DQ) {
    auto[d, next_t] = dq;
    ll prev_t = T[d];
    T[d] = next_t;
    days[prev_t].erase(d);
    days[next_t].insert(d);

    total_score -= scores[prev_t];
    total_score -= scores[next_t];
    scores[prev_t] = calc(prev_t, days[prev_t], S, C);
    scores[next_t] = calc(next_t, days[next_t], S, C);
    total_score += scores[prev_t];
    total_score += scores[next_t];

    cout << total_score << "\n";
  }

  timer("main", [&] {

  });
}

Measured memory_bytes: 4284