//
// Created on 2020/07/23.
//
//
// Created on 2020/07/21.
//


#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;


int main() {


    int d;
    cin>>d;

    vector<int>c_sum(d+1);
    int r = 0;
    for (int i = 0; i <= d; ++i) {
        r += i;
        c_sum[i] = r;
    }

    vector<int>c(27);
    vector<int>c_log(d+1);
    vector<vector<int>>last(27,vector<int>(1,0));
    vector<vector<int>>s(d+1,vector<int>(27));
    for (int i = 1; i <= 26; ++i) {
        cin>>c[i];
    }
    for (int i = 1; i <= d; ++i) {
        for (int j = 1; j <= 26; ++j) {
            cin>>s[i][j];
        }
    }
    int final_score = 0;
    for (int k = 1; k <= d; ++k) {
        int today_c;
        cin>>today_c;
        c_log[k] = today_c;
        final_score += s[k][today_c];
        last[today_c].push_back(k);
    }
    for (int l = 1; l <= 26; ++l) {
        last[l].push_back(d + 1);
    }
    for (int i = 1; i <= 26; ++i) {
        for (int j = 0; j < last[i].size() - 1; ++j) {
            final_score -= c_sum[abs((last[i][j+1] - 1)  - last[i][j])] * c[i];
        }
    }
    int m;
    cin>>m;
    for (int i = 0; i < m; ++i) {
        int c_d,q;
        cin>>c_d>>q;
        int old_c = c_log[c_d];
        final_score -= s[c_d][old_c];
        final_score += s[c_d][q];
        int ok = last[old_c].size();
        int ng = 0;
        int mid = (ok + ng) / 2;
        while(!(abs(ok - ng) == 1)){
            if(last[old_c][mid] < c_d){
                ng = mid;
            }else{
                ok = mid;
            }
            mid = (ok + ng) / 2;
        }

        for (int j = 0; j < last[old_c].size() - 1; ++j) {
            final_score += c_sum[abs((last[old_c][j+1] - 1) - last[old_c][j])] * c[old_c];
        }
        for (int j = 0; j < last[q].size() - 1; ++j) {
            final_score += c_sum[abs((last[q][j+1] - 1) - last[q][j])] * c[q];

        }
        last[old_c].erase(last[old_c].begin() + ok);
        last[q].push_back(c_d);
        sort(all(last[q]));
        for (int j = 0; j < last[old_c].size() - 1; ++j) {
            final_score -= c_sum[abs((last[old_c][j+1] - 1) - last[old_c][j])] * c[old_c];

        }
        for (int j = 0; j < last[q].size() - 1; ++j) {
            final_score -= c_sum[abs((last[q][j+1] - 1) - last[q][j])] * c[q];
        }
        c_log[c_d] = q;
        cout<<final_score<<endl;
    }

    return 0;
}



Measured memory_bytes: 3664