from bisect import bisect_left,insort_left

d=int(eval(input()))

c=list(map(int,input().split()))

s=[list(map(int,input().split()))for _ in range(d)]

t=[int(eval(input()))for _ in range(d)]

m=int(eval(input()))

dq=[list(map(int,input().split()))for _ in range(m)]

x=[[0]for _ in range(26)]

ans=0

for i in range(d):

  x[t[i]-1].append(i+1)

  ans+=s[i][t[i]-1]

  for j in range(26):ans-=((i+1)-x[j][-1])*c[j]

for i in range(26):

  x[i].append(d+1)

for dd,q in dq:

  p=t[dd-1]

  t[dd-1]=q

  ans-=s[dd-1][p-1]

  ans+=s[dd-1][q-1]

  pi=bisect_left(x[p-1],dd)

  qi=bisect_left(x[q-1],dd)

  pl=dd-x[p-1][pi-1]

  pr=x[p-1][pi+1]-dd

  ql=dd-x[q-1][qi-1]

  qr=x[q-1][qi]-dd

  del x[p-1][pi]

  insort_left(x[q-1],dd)

  ans-=c[p-1]*pl*pr

  ans+=c[q-1]*ql*qr

  print(ans)
import sys

input=sys.stdin.readline

from bisect import bisect_left,insort_left

d=int(eval(input()))

c=list(map(int,input().split()))

s=[list(map(int,input().split()))for _ in range(d)]

t=[int(eval(input()))for _ in range(d)]

m=int(eval(input()))

dq=[list(map(int,input().split()))for _ in range(m)]

x=[[0]for _ in range(26)]

ans=0

for i in range(d):

  v=t[i]-1

  ans+=s[i][v]-c[v]*(i-x[v][-1]+1)*(i-x[v][-1])//2

  x[v].append(i+1)

for i in range(26):

  ans-=c[i]*(d+1-x[i][-1])*(d-x[i][-1])//2

  x[i].append(d+1)

for dd,q in dq:

  p=t[dd-1]

  t[dd-1]=q

  ans-=s[dd-1][p-1]

  ans+=s[dd-1][q-1]

  pi=bisect_left(x[p-1],dd)

  qi=bisect_left(x[q-1],dd)

  pl=dd-x[p-1][pi-1]

  pr=x[p-1][pi+1]-dd

  ql=dd-x[q-1][qi-1]

  qr=x[q-1][qi]-dd

  del x[p-1][pi]

  x[q-1].insert(qi,dd)

  ans-=c[p-1]*pl*pr

  ans+=c[q-1]*ql*qr

  print(ans)
from bisect import bisect_left, insort_left

d = int(eval(input()))
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(d)]
t = [int(eval(input())) for _ in range(d)]
m = int(eval(input()))
dq = [list(map(int, input().split())) for _ in range(m)]
x = [[0] for _ in range(26)]
ans = 0
for i in range(d):
    x[t[i] - 1].append(i + 1)
    ans += s[i][t[i] - 1]
    for j in range(26):
        ans -= ((i + 1) - x[j][-1]) * c[j]
for i in range(26):
    x[i].append(d + 1)
for dd, q in dq:
    p = t[dd - 1]
    t[dd - 1] = q
    ans -= s[dd - 1][p - 1]
    ans += s[dd - 1][q - 1]
    pi = bisect_left(x[p - 1], dd)
    qi = bisect_left(x[q - 1], dd)
    pl = dd - x[p - 1][pi - 1]
    pr = x[p - 1][pi + 1] - dd
    ql = dd - x[q - 1][qi - 1]
    qr = x[q - 1][qi] - dd
    del x[p - 1][pi]
    insort_left(x[q - 1], dd)
    ans -= c[p - 1] * pl * pr
    ans += c[q - 1] * ql * qr
    print(ans)

import sys

input = sys.stdin.readline
from bisect import bisect_left, insort_left

d = int(eval(input()))
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(d)]
t = [int(eval(input())) for _ in range(d)]
m = int(eval(input()))
dq = [list(map(int, input().split())) for _ in range(m)]
x = [[0] for _ in range(26)]
ans = 0
for i in range(d):
    v = t[i] - 1
    ans += s[i][v] - c[v] * (i - x[v][-1] + 1) * (i - x[v][-1]) // 2
    x[v].append(i + 1)
for i in range(26):
    ans -= c[i] * (d + 1 - x[i][-1]) * (d - x[i][-1]) // 2
    x[i].append(d + 1)
for dd, q in dq:
    p = t[dd - 1]
    t[dd - 1] = q
    ans -= s[dd - 1][p - 1]
    ans += s[dd - 1][q - 1]
    pi = bisect_left(x[p - 1], dd)
    qi = bisect_left(x[q - 1], dd)
    pl = dd - x[p - 1][pi - 1]
    pr = x[p - 1][pi + 1] - dd
    ql = dd - x[q - 1][qi - 1]
    qr = x[q - 1][qi] - dd
    del x[p - 1][pi]
    x[q - 1].insert(qi, dd)
    ans -= c[p - 1] * pl * pr
    ans += c[q - 1] * ql * qr
    print(ans)

+import sys + +input = sys.stdin.readline -    x[t[i] - 1].append(i + 1) -    ans += s[i][t[i] - 1] -    for j in range(26): -        ans -= ((i + 1) - x[j][-1]) * c[j] +    v = t[i] - 1 +    ans += s[i][v] - c[v] * (i - x[v][-1] + 1) * (i - x[v][-1]) // 2 +    x[v].append(i + 1) +    ans -= c[i] * (d + 1 - x[i][-1]) * (d - x[i][-1]) // 2 -    insort_left(x[q - 1], dd) +    x[q - 1].insert(qi, dd)