#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef long double ld; 
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

struct Edge{
    int u, v;
    long long cap, cost;
 
    Edge(int _u, int _v, long long _cap, long long _cost){
        u = _u; v = _v; cap = _cap; cost = _cost;
    }
};
 
struct MinCostFlow{
    int n, s, t;
    long long flow, cost;
    vector<vector<int> > graph;
    vector<Edge> e;
    vector<long long> dist, potential;
    vector<int> parent;
    bool negativeCost;
 
    MinCostFlow(int _n){
        // 0-based indexing
        n = _n;
        graph.assign(n, vector<int> ());
        negativeCost = false;
    }
 
    void addEdge(int u, int v, long long cap, long long cost, bool directed = true){
        if(cost < 0)
            negativeCost = true;
 
        graph[u].push_back(e.size());
        e.push_back(Edge(u, v, cap, cost));
 
        graph[v].push_back(e.size());
        e.push_back(Edge(v, u, 0, -cost));
 
        if(!directed)
            addEdge(v, u, cap, cost, true);
    }
 
    pair<long long, long long> getMinCostFlow(int _s, int _t){
        s = _s; t = _t;
        flow = 0, cost = 0;
 
        potential.assign(n, 0);
        if(negativeCost){
            // run Bellman-Ford to find starting potential
            dist.assign(n, 1LL<<62);
            for(int i = 0, relax = false; i < n && relax; i++, relax = false){
                for(int u = 0; u < n; u++){
                    for(int k = 0; k < graph[u].size(); k++){
                        int eIdx = graph[u][i];
                        int v = e[eIdx].v; ll cap = e[eIdx].cap, w = e[eIdx].cost;
 
                        if(dist[v] > dist[u] + w && cap > 0){
                            dist[v] = dist[u] + w;
                            relax = true;
            }   }   }   }
 
            for(int i = 0; i < n; i++){
                if(dist[i] < (1LL<<62)){
                    potential[i] = dist[i];
        }   }   }
 
        while(dijkstra()){
            flow += sendFlow(t, 1LL<<62);
        }
 
        return make_pair(flow, cost);
    }
 
    bool dijkstra(){
        parent.assign(n, -1);
        dist.assign(n, 1LL<<62);
        priority_queue<ii, vector<ii>, greater<ii> > pq;
 
        dist[s] = 0;
        pq.push(ii(0, s));
 
 
        while(!pq.empty()){
            int u = pq.top().second;
            long long d = pq.top().first;
            pq.pop();
 
            if(d != dist[u]) continue;
 
            for(int i = 0; i < graph[u].size(); i++){
                int eIdx = graph[u][i];
                int v = e[eIdx].v; ll cap = e[eIdx].cap;
                ll w = e[eIdx].cost + potential[u] - potential[v];
 
                if(dist[u] + w < dist[v] && cap > 0){
                    dist[v] = dist[u] + w;
                    parent[v] = eIdx;
 
                    pq.push(ii(dist[v], v));
        }   }   }
 
        // update potential
        for(int i = 0; i < n; i++){
            if(dist[i] < (1LL<<62))
                potential[i] += dist[i];
        }
 
        return dist[t] != (1LL<<62);
    }
 
    long long sendFlow(int v, long long curFlow){
        if(parent[v] == -1)
            return curFlow;
        int eIdx = parent[v];
        int u = e[eIdx].u; ll w = e[eIdx].cost;
 
        long long f = sendFlow(u, min(curFlow, e[eIdx].cap));
 
        cost += f*w;
        e[eIdx].cap -= f;
        e[eIdx^1].cap += f;
 
        return f;
    }
};

const int C = 26;
const int N = 365;
ll a[N+10][C+10];
ll c[C+10];

ll getScore(vi &ans)
{
	ll satisfaction = 0;
	vi las(C+1,-1);
	for(int i=0;i<ans.size();i++)
	{
		satisfaction += a[i][ans[i]];
		las[ans[i]]=i;
		for(int j=0;j<C;j++)
		{
			satisfaction-=c[j]*(i-las[j]);
		}
	}
	return satisfaction;//max(0LL, ll(1e6)+satisfaction);
}

ll s2(ll r)
{
	return (r*(r+1))/2;
}

ll s2(ll l, ll r) //sum of l to r
{
	if(l>r) return 0;
	if(l==0) return s2(r);
	return s2(r)-s2(l-1);
}

ll solve(vi &ans, int l, int r)
{
	MinCostFlow mcmf(2*C+10);
	vi las(C,-1);
	int lascnt=0;
	for(int i=l-1;i>=0;i--)
	{
		if(las[ans[i]]<0)
		{
			lascnt++;
			las[ans[i]]=i;
		}
		if(lascnt>=C) break;
	}
	int s = 2*C+9; int e = 2*C+8;
	for(int i=l;i<=r;i++)
	{
		for(int j=0;j<26;j++)
		{
			mcmf.addEdge(j,i-l+26,1,-(a[i][j]-c[j]*(s2(l-las[j],i-1-las[j])+s2(r-i))));
		}
	}
	int trash = 2*C+7;
	for(int j=0;j<26;j++)
	{
		mcmf.addEdge(s,j,1,0);
		mcmf.addEdge(j,trash,1,c[j]*(s2(l-las[j],r-las[j])));
	}
	mcmf.addEdge(trash,e,C-(r-l+1),0);
	for(int i=l;i<=r;i++)
	{
		mcmf.addEdge(i-l+26,e,1,0);
	}
	auto tmp = mcmf.getMinCostFlow(s,e);
	//cerr<<"FLOW SIZE = "<<tmp.fi<<'\n';
	//cerr<<"FLOW COST = "<<-tmp.se<<'\n';
	for(int i=0;i<C;i++)
	{
		for(auto id:mcmf.graph[i])
		{
			auto e = mcmf.e[id];
			if(e.v>=26&&e.v<=r-l+26&&e.cap==0)
			{
				assert(ans[e.v-26+l]<0);
				ans[e.v-26+l]=i;
			}
		}
	}
	return -tmp.se;
}

void print(const vi &ans)
{
	for(int i=0;i<ans.size();i++)
	{
		assert(ans[i]>=0);
		cout<<ans[i]+1<<'\n';
	}
}

const bool DEBUG = 0;
int n; 

void read()
{
	cin>>n;
	for(int i=0;i<C;i++) cin>>c[i];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<C;j++)
		{
			cin>>a[i][j];
		}
	}
}

ll solve_run()
{
	vi bestans(n,-1); ll bestscore = -ll(1e18);
	for(int D=6;D<=16;D++)
	{
		vi ans(n,-1);
		int l = 0;
		while(l<n)
		{
			ld bestavg = -ld(1e18);
			int bestr = l;
			if(l+D<n)
			{
				for(int r=l+D;r<min(n,l+C);r++)
				{
					ll tmp = solve(ans,l,r);
					ld avg = ld(tmp)/ld(r-l+1);
					if(avg>bestavg)
					{
						bestavg=avg; bestr=r;
					}
					for(int j=l;j<=r;j++) ans[j]=-1;
				}
			}
			else
			{
				for(int r=l;r<min(n,l+C);r++)
				{
					ll tmp = solve(ans,l,r);
					ld avg = ld(tmp)/ld(r-l+1);
					if(avg>bestavg)
					{
						bestavg=avg; bestr=r;
					}
					for(int j=l;j<=r;j++) ans[j]=-1;
				}
			}
			solve(ans,l,bestr);
			l=bestr+1;
		}
		ll newscore = getScore(ans);
		if(newscore>bestscore)
		{
			bestscore = newscore; bestans = ans;
		}
	}
	vi ans = bestans;
	if(!DEBUG)
	{
		print(ans);
	}
	return getScore(ans);
}

set<int> days[C+10];

void getSets(vi &ans)
{
	for(int i=0;i<C;i++) 
	{
		days[i].clear();
		days[i].insert(-1);
		days[i].insert(int(ans.size()));
	}
	for(int i=0;i<n;i++)
	{
		days[ans[i]].insert(i);
	}
}

ll getDiff(vi &ans, int d, int q, bool upd = 0) //what happens when you update day d to q
{
	ll scorediff = 0;
	int oldq = ans[d];
	if(q==oldq) return 0;
	{
		auto it = days[oldq].lower_bound(d); //should exist in q
		auto itr = it; auto itl = it;
		itr++; itl--;
		//cerr<<oldq<<' '<<(*itl)<<' '<<(*it)<<' '<<(*itr)<<'\n';
		ll tmp = c[oldq]*(s2((*itr)-(*itl)-1)-s2((*it)-(*itl)-1)-s2((*itr)-(*it)-1));
		scorediff -= tmp;
		//cerr<<c[oldq]*(s2((*itr)-(*itl)-1)-s2((*it)-(*itl)-1)-s2((*itr)-(*it)-1))<<'\n';
		scorediff -= a[d][oldq];
	}
	//cerr<<"SCORE DIFF = "<<scorediff<<'\n';
	{
		auto it = days[q].lower_bound(d); //should not exist in q
		auto itr = it; auto itl = it;
		itl--;
		//cerr<<q<<' '<<(*itl)<<' '<<(*itr)<<'\n';
		ll tmp = c[q]*(s2((*itr)-(*itl)-1)-s2(d-(*itl)-1)-s2((*itr)-d-1));
		//cerr<<c[q]*(s2((*itr)-(*itl)-1)-s2(d-(*itl)-1)-s2((*itr)-d-1))<<'\n';
		scorediff += tmp;
		scorediff += a[d][q];
	}
	if(upd)
	{
		days[oldq].erase(d);
		days[q].insert(d);
		ans[d]=q;
	}
	//cerr<<"SCORE DIFF = "<<scorediff<<'\n';
	return scorediff;
}

void genCase()
{
	n=365;
	for(int i=0;i<26;i++) c[i]=rand()%101;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<26;j++)
		{
			a[i][j]=rand()%20001;
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	srand(time(NULL));
	if(!DEBUG)
	{
		read();
		vi ans(n,-1);
		for(int i=0;i<n;i++)
		{
			cin>>ans[i]; ans[i]--;
		}
		getSets(ans);
		ll curscore = getScore(ans);
		int z; cin>>z;
		//cerr<<"INITAL SCORE = "<<curscore<<'\n';
		while(z--)
		{
			int d,q; cin>>d>>q;
			d--; q--;
			curscore+=getDiff(ans,d,q,1);
			cout<<curscore<<'\n';
		}
		//solve_run();
		return 0;
	}
	const int C = 50;
	ll sum = 0;
	for(int cc=1;cc<=C;cc++)
	{
		genCase();
		sum+=solve_run();
		cerr<<"Case "<<cc<<" complete. Total score = "<<sum<<", Average score = "<<fixed<<setprecision(5)<<ld(sum)/ld(cc)<<'\n';
	}
}



Measured memory_bytes: 3752