#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

//#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

    template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int mod = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};

int D;
LL C[26];
VVL S;
VI T;
set<int> days[26];
int M;
VI d,q;

void input(){
    cin >> D;
    REP(i,26) cin >> C[i];
    S.assign(D,VL(26));
    REP(i,D) REP(j,26) cin >> S[i][j];
    T.resize(D);
    REP(i,D){ cin >> T[i]; T[i]--; }

    cin >> M;
    d.resize(M);
    q.resize(M);
    REP(i,M){ cin >> d[i] >> q[i]; d[i]--; q[i]--; }
    
}

LL firstScore(){
    // last days
    LL L[26];
    REP(i,26) L[i] = -1;
    // 番兵
    REP(i,26) {
        days[i].insert(-1);
        days[i].insert(D);
    }

    LL score = 0;
    REP(day,D){
        score += S[day][T[day]];
        L[T[day]] = day;
        REP(i,26){
            score -= C[i] * (day - L[i]);
        }
        days[T[day]].insert(day);

    }
    return score;
}

LL query(int x, int y, LL cur){
    int pre = T[x];
    if(pre == y) return cur;

    dump(pre)
        dump(y)

    dump(S[x][pre])
        dump(S[x][y])

    cur -= S[x][pre];
    cur += S[x][y];

    auto preDay1 = prev(days[pre].lower_bound(x));
    auto preDay2 = days[pre].lower_bound(x+1);

    dump(C[pre] * (x - *preDay1) * (*preDay2 - x))
    cur -= C[pre] * (x - *preDay1) * (*preDay2 - x);

    auto nextDay1 = prev(days[y].lower_bound(x));
    auto nextDay2 = days[y].lower_bound(x+1);

    cur += C[y] * (x - *nextDay1) * (*nextDay2 - x);
    dump(C[y] * (x - *nextDay1) * (*nextDay2 - x))

    T[x] = y;
    days[pre].erase(x);
    days[y].insert(x);

    return cur;

}


signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);
    
    input();
    LL score = firstScore();
    dump(score)
    REP(i,M){
        score = query(d[i], q[i], score);
        cout << score << "\n";
    }

}

Measured memory_bytes: 4132