#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
const int MAXM = 4e2 + 5;
const int MAXN = 26 + 5;
const ll linf = 0x3f3f3f3f3f3f3f3f;

int n=26, m;
int c[MAXN];
int a[MAXM][MAXN];

int p[MAXM];
set<int> idx[MAXN];
ll ans;

ll solve(void)
{
	static int lst[MAXN];
	memset(lst,0,sizeof(lst));
	for(int i=1; i<=n; ++i)
	{
		idx[i].clear();
		idx[i].insert(0);
		idx[i].insert(m+1);
	}
	
	ll ans = 0;
	for(int k=1; k<=m; ++k)
	{
		int x = p[k];
		ans += a[k][x];
		for(int i=1; i<=n; ++i) if(i != x)
			ans -= c[i] * (k-lst[i]);
		lst[x] = k;
		
		idx[x].insert(k);
	}
	
	::ans = ans;
	return ans;
}

ll recalc(int pos, int x)
{
	int old = p[pos];
	if(old == x) return ans;
	
	ll res = ans;
	
	idx[old].erase(pos);
	
	set<int>::iterator it = idx[old].upper_bound(pos);
	int nxt = *it;
	int lst = *(--it);
	
	res -= (pos - lst) * c[old] * (nxt - pos);
	res -= a[pos][old];
	
	it = idx[x].upper_bound(pos);
	nxt = *it;
	lst = *(--it);
	
	res += (pos - lst) * c[x] * (nxt - pos);
	res += a[pos][x];
	
	idx[x].insert(pos);
	
	p[pos] = x;
	return res;
}

int main(void)
{
	scanf("%d",&m);
	for(int i=1; i<=n; ++i) scanf("%d",&c[i]);
	for(int i=1; i<=m; ++i)
		for(int j=1; j<=n; ++j)
			scanf("%d",&a[i][j]);
	for(int i=1; i<=m; ++i) scanf("%d",&p[i]);
	
	solve();
	
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int pos, x;
		scanf("%d%d",&pos,&x);
		
		ans = recalc(pos, x);
		printf("%lld\n",ans);
	}
	return 0;
}