#include <bits/stdc++.h>
using namespace std;

int main()
{
	int d;
	cin >> d;

	vector<int> c(26+1);
	for (int i=1;i<=26;++i)
	{
		cin >> c[i];
	}

	vector<vector<int>> s(d+1,vector<int>(26+1));
	for (int i=1;i<=d;++i)
	{
		for (int j=1;j<=26;++j)
		{
			cin >> s[i][j];
		}
	}
	vector<int> t(d+1);
	for (int i=1;i<=d;++i)
	{
		cin >> t[i];
	}

	int m;
	cin >> m;
	vector<pair<int,int>> q(m);

	for (int i=0;i<m;++i)
	{
		int dd,qq;
		cin >> dd >> qq;
		q[i]=make_pair(dd,qq);
	}

	vector<int> last(26+1);
	vector<set<int>> hh(26+1);
	for (int i=1;i<=26;++i)
	{
		last[i]=0;
		hh[i].insert(0);
		hh[i].insert(d+1);
	}


	int64_t score=0;
	for (int i=1;i<=d;++i)
	{
		score+=s[i][t[i]];
		last[t[i]]=i;
		hh[t[i]].insert(i);
		for (int j=1;j<=26;++j)
		{
			score-=c[j]*(i-last[j]);
		}
		//cout << score << endl;
	}

	for (int i=0;i<m;++i)
	{
		int dd=q[i].first;
		int qq=q[i].second;

		score-=s[dd][t[dd]];
		score+=s[dd][qq];
		auto it=hh[t[dd]].find(dd);
		auto prev=it;
		--prev;
		auto next=it;
		++next;
		score-=(dd-*prev)*(*next-dd)*c[t[dd]];

		hh[t[dd]].erase(dd);
		t[dd]=qq;

		hh[qq].insert(dd);
		it=hh[qq].find(dd);
		prev=it;
		--prev;
		next=it;
		++next;
		score+=(dd-*prev)*(*next-dd)*c[qq];

		cout << score << endl;
	}


}




