from numba import njit

from numpy import int64,zeros

@njit('i8(i8[:],i8[:])',cache=True)

def func(s,x):

    last=zeros(26,int64)

    score=0

    for i,v in enumerate(x,1):

        last[v]=i

        c=0

        for j in range(26):

            c+=s[j]*(i-last[j])

        score+=s[i*26+v]-c

    return score

def main():

    d,*s=list(map(int,open(0).read().split()))

    s=int64(s)

    x=s[26*-~d:d*27+26]-1

    for d,q in s[27*-~d:].reshape(-1,2):

        x[d-1]=q-1

        print((func(s,x)))

main()
def P(s):

 R,P,*L=[0]*28

 for i in range(D):t=I[i+D][0]-1;P+=s-(i+1-L[t])*C[t];R+=I[i][t]-P;L[t]=i+1

 return R

(D,),C,*I=[[*list(map(int,t.split()))]for t in open(0)]

for d,q in I[D-~D:]:I[d-1+D][0]=q;print((P(sum(C))))
from numba import njit
from numpy import int64, zeros


@njit("i8(i8[:],i8[:])", cache=True)
def func(s, x):
    last = zeros(26, int64)
    score = 0
    for i, v in enumerate(x, 1):
        last[v] = i
        c = 0
        for j in range(26):
            c += s[j] * (i - last[j])
        score += s[i * 26 + v] - c
    return score


def main():
    d, *s = list(map(int, open(0).read().split()))
    s = int64(s)
    x = s[26 * -~d : d * 27 + 26] - 1
    for d, q in s[27 * -~d :].reshape(-1, 2):
        x[d - 1] = q - 1
        print((func(s, x)))


main()

def P(s):
    R, P, *L = [0] * 28
    for i in range(D):
        t = I[i + D][0] - 1
        P += s - (i + 1 - L[t]) * C[t]
        R += I[i][t] - P
        L[t] = i + 1
    return R


(D,), C, *I = [[*list(map(int, t.split()))] for t in open(0)]
for d, q in I[D - ~D :]:
    I[d - 1 + D][0] = q
    print((P(sum(C))))

-from numba import njit -from numpy import int64, zeros +def P(s): +    R, P, *L = [0] * 28 +    for i in range(D): +        t = I[i + D][0] - 1 +        P += s - (i + 1 - L[t]) * C[t] +        R += I[i][t] - P +        L[t] = i + 1 +    return R -@njit("i8(i8[:],i8[:])", cache=True) -def func(s, x): -    last = zeros(26, int64) -    score = 0 -    for i, v in enumerate(x, 1): -        last[v] = i -        c = 0 -        for j in range(26): -            c += s[j] * (i - last[j]) -        score += s[i * 26 + v] - c -    return score - - -def main(): -    d, *s = list(map(int, open(0).read().split())) -    s = int64(s) -    x = s[26 * -~d : d * 27 + 26] - 1 -    for d, q in s[27 * -~d :].reshape(-1, 2): -        x[d - 1] = q - 1 -        print((func(s, x))) - - -main() +(D,), C, *I = [[*list(map(int, t.split()))] for t in open(0)] +for d, q in I[D - ~D :]: +    I[d - 1 + D][0] = q +    print((P(sum(C))))