#include<stdio.h>
static inline int IN(void)
{
  int x=0,f=0,c=getchar();while(c<48||c>57){f^=c==45,c=getchar();}
  while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x;
}
int main()
{
  int c[26],D=IN(),i=0,j=0,M=0,s[365][26]={{0}},t[365],sum=0,temp=0,update[26]={0};
  while(i<26){c[i++]=IN();}i=0;
  while(i<D){while(j<26){s[i][j++]=IN();}i++,j=0;}i=0;
  while(i<D)
  {
    j=0;temp=IN()-1;t[i]=temp;sum+=s[i][temp];update[temp]=++i;
    while(j<26){sum-=c[j]*(i-update[j]);j++;}
  }M=IN();
  while(M--)
  {
    int d=IN()-1,q=IN()-1,l0=-1,l1=-1,r0=D,r1=D,esc0=0,esc1=0;
    sum+=s[d][q]-s[d][t[d]];i=d-1;
    while(i>=0)
    {
      if(t[i]==t[d]&&!esc0){l0=i;esc0=1;}
      if(t[i]==q&&!esc1){l1=i;esc1=1;}
      if(esc0&esc1){break;}i--;
    }
    esc0=esc1=0;i=d+1;
    while(i<D)
    {
      if(t[i]==t[d]&&!esc0){r0=i;esc0=1;}
      if(t[i]==q&&!esc1){r1=i;esc1=1;}
      if(esc0&esc1){break;}i++;
    }
    sum-=c[t[d]]*(d-l0)*(r0-d);sum+=c[q]*(d-l1)*(r1-d);t[d]=q;
    printf("%d\n",sum);
  }
}