def f(a,d,T):

    try:

        return T[d:].index(a)+d

    except ValueError:

        return len(T)



from random import randint

A=26



D=int(eval(input()))



C=[0]+list(map(int,input().split())) #減る満足度のベース

S=[0]*(D+1) #S[d][i] :(d+1)日目にコンテストiを開催した時の満足度



for i in range(1,D+1):

    S[i]=[0]+list(map(int,input().split()))



T=[0]

for i in range(D):

    T.append(int(eval(input())))



L=[[0]*(A+1) for _ in range(D+1)]



for x in range(1,D+1):

    t=T[x]

    for j in range(1,A+1):

        if t!=j:

            L[x][j]=L[x-1][j]+1

        else:

            L[x][j]=0

            

X=0

for d in range(1,D+1):

    X+=S[d][T[d]]    

    for j in range(1,A+1):

        X-=C[j]*L[d][j]



M=int(eval(input()))

for _ in range(M):

    d,q=list(map(int,input().split()))

    p=T[d]



    a=f(p,d+1,T)

    b=f(q,d+1,T)

    

    Y=0

    h=L[d-1][p]

    for i in range(d,a):

        h+=1

        Y+=h-L[i][p]

        L[i][p]=h

        

    m=0

    Z=0

    for j in range(d,b):

        Z-=m-L[j][q]

        L[j][q]=m

        m+=1



    X+=(S[d][q]-S[d][p])+Z*C[q]-Y*C[p]

    print(X)



    T[d]=q

def f(a,d,T):

    try:

        return T[d+1:].index(a)+(d+1)

    except ValueError:

        return len(T)



def g(a,d,T):

    try:

        return T[:d].index(a)

    except ValueError:

        return 0



A=26



D=int(eval(input()))



C=[0]+list(map(int,input().split())) #減る満足度のベース

S=[0]*(D+1) #S[d][i] :(d+1)日目にコンテストiを開催した時の満足度



for i in range(1,D+1):

    S[i]=[0]+list(map(int,input().split()))



T=[0]

for i in range(D):

    T.append(int(eval(input())))

            

X=0

L=[0]*(A+1)

R=[[0]*(A+1) for _ in range(D+1)]

for d in range(1,D+1):

    X+=S[d][T[d]]

    for j in range(1,A+1):

        if j!=T[d]:

            L[j]+=1

            R[d][j]=R[d-1][j]+1

            X-=C[j]*L[j]

        else:

            L[j]=0



M=int(eval(input()))

for _ in range(M):

    d,q=list(map(int,input().split()))

    p=T[d]



    Y=0

    h=R[d-1][p]

    for i in range(d,f(p,d,T)):

        h+=1

        Y+=h-R[i][p]

        R[i][p]=h



    Z=0

    m=0

    for j in range(d,f(q,d,T)):

        Z+=R[j][q]-m

        R[j][q]=m

        m+=1

    

    T[d]=q

    X+=(S[d][q]-S[d][p])+Z*C[q]-Y*C[p]

    print(X)

def f(a, d, T):
    try:
        return T[d:].index(a) + d
    except ValueError:
        return len(T)


from random import randint

A = 26
D = int(eval(input()))
C = [0] + list(map(int, input().split()))  # 減る満足度のベース
S = [0] * (D + 1)  # S[d][i] :(d+1)日目にコンテストiを開催した時の満足度
for i in range(1, D + 1):
    S[i] = [0] + list(map(int, input().split()))
T = [0]
for i in range(D):
    T.append(int(eval(input())))
L = [[0] * (A + 1) for _ in range(D + 1)]
for x in range(1, D + 1):
    t = T[x]
    for j in range(1, A + 1):
        if t != j:
            L[x][j] = L[x - 1][j] + 1
        else:
            L[x][j] = 0
X = 0
for d in range(1, D + 1):
    X += S[d][T[d]]
    for j in range(1, A + 1):
        X -= C[j] * L[d][j]
M = int(eval(input()))
for _ in range(M):
    d, q = list(map(int, input().split()))
    p = T[d]
    a = f(p, d + 1, T)
    b = f(q, d + 1, T)
    Y = 0
    h = L[d - 1][p]
    for i in range(d, a):
        h += 1
        Y += h - L[i][p]
        L[i][p] = h
    m = 0
    Z = 0
    for j in range(d, b):
        Z -= m - L[j][q]
        L[j][q] = m
        m += 1
    X += (S[d][q] - S[d][p]) + Z * C[q] - Y * C[p]
    print(X)
    T[d] = q

def f(a, d, T):
    try:
        return T[d + 1 :].index(a) + (d + 1)
    except ValueError:
        return len(T)


def g(a, d, T):
    try:
        return T[:d].index(a)
    except ValueError:
        return 0


A = 26
D = int(eval(input()))
C = [0] + list(map(int, input().split()))  # 減る満足度のベース
S = [0] * (D + 1)  # S[d][i] :(d+1)日目にコンテストiを開催した時の満足度
for i in range(1, D + 1):
    S[i] = [0] + list(map(int, input().split()))
T = [0]
for i in range(D):
    T.append(int(eval(input())))
X = 0
L = [0] * (A + 1)
R = [[0] * (A + 1) for _ in range(D + 1)]
for d in range(1, D + 1):
    X += S[d][T[d]]
    for j in range(1, A + 1):
        if j != T[d]:
            L[j] += 1
            R[d][j] = R[d - 1][j] + 1
            X -= C[j] * L[j]
        else:
            L[j] = 0
M = int(eval(input()))
for _ in range(M):
    d, q = list(map(int, input().split()))
    p = T[d]
    Y = 0
    h = R[d - 1][p]
    for i in range(d, f(p, d, T)):
        h += 1
        Y += h - R[i][p]
        R[i][p] = h
    Z = 0
    m = 0
    for j in range(d, f(q, d, T)):
        Z += R[j][q] - m
        R[j][q] = m
        m += 1
    T[d] = q
    X += (S[d][q] - S[d][p]) + Z * C[q] - Y * C[p]
    print(X)

-        return T[d:].index(a) + d +        return T[d + 1 :].index(a) + (d + 1) -from random import randint +def g(a, d, T): +    try: +        return T[:d].index(a) +    except ValueError: +        return 0 + -L = [[0] * (A + 1) for _ in range(D + 1)] -for x in range(1, D + 1): -    t = T[x] -    for j in range(1, A + 1): -        if t != j: -            L[x][j] = L[x - 1][j] + 1 -        else: -            L[x][j] = 0 +L = [0] * (A + 1) +R = [[0] * (A + 1) for _ in range(D + 1)] -        X -= C[j] * L[d][j] +        if j != T[d]: +            L[j] += 1 +            R[d][j] = R[d - 1][j] + 1 +            X -= C[j] * L[j] +        else: +            L[j] = 0 -    a = f(p, d + 1, T) -    b = f(q, d + 1, T) -    h = L[d - 1][p] -    for i in range(d, a): +    h = R[d - 1][p] +    for i in range(d, f(p, d, T)): -        Y += h - L[i][p] -        L[i][p] = h +        Y += h - R[i][p] +        R[i][p] = h +    Z = 0 -    Z = 0 -    for j in range(d, b): -        Z -= m - L[j][q] -        L[j][q] = m +    for j in range(d, f(q, d, T)): +        Z += R[j][q] - m +        R[j][q] = m +    T[d] = q -    T[d] = q