CodeEff/ECCO huggingface dataset:
# Original solution
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict

def I(): return int(sys.stdin.readline())
def MI(): return list(map(int, sys.stdin.readline().split()))
def MI0(): return [int(s) - 1 for s in sys.stdin.readline().split()]
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list([int(s) - 1 for s in sys.stdin.readline().split()])
MOD = 10 ** 9 + 7
INF = float('inf')


D = I()
C = LMI()
S = [LMI() for _ in range(D)]
T = [I() - 1 for _ in range(D)]
M = I()


def eval(T):
    last = defaultdict(int)
    ans = 0
    for d in range(D):
        ans += S[d][T[d]]
        last[T[d]] = d + 1
        for i in range(26):
            ans -= C[i] * (d + 1 - last[i])
    return ans

for d, q in [tuple(MI0()) for _ in range(M)]:
    T[d] = q
    print((eval(T)))


# Optimized solution
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict

def I(): return int(sys.stdin.readline())
def MI(): return list(map(int, sys.stdin.readline().split()))
def MI0(): return [int(s) - 1 for s in sys.stdin.readline().split()]
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list([int(s) - 1 for s in sys.stdin.readline().split()])
MOD = 10 ** 9 + 7
INF = float('inf')


D = I()
C = LMI()
S = [LMI() for _ in range(D)]
T = [I() - 1 for _ in range(D)]
M = I()

last = [[i + 1] * 26 for i in range(D)]
for d in range(D):
    i = T[d]
    j = 0
    for dd in range(d, D):
        last[dd][i] = j
        j += 1

def eval(d, q):
    i = T[d]
    val = S[d][q] - S[d][i]

    contrib = 0
    if d == 0:
        j = 1
    else:
        j = last[d - 1][i] + 1
    for dd in range(d, D):
        if dd > d and last[dd][i] == 0:
            break
        contrib += j - last[dd][i]
        last[dd][i] = j
        j += 1
    val -= contrib * C[i]

    contrib = 0
    j = 0
    for dd in range(d, D):
        if last[dd][q] == 0:
            break
        contrib += last[dd][q] - j
        last[dd][q] = j
        j += 1
    val += contrib * C[q]

    T[d] = q

    return val


def score0(T):
    last = defaultdict(int)
    ans = 0
    for d in range(D):
        ans += S[d][T[d]]
        last[T[d]] = d + 1
        for i in range(26):
            ans -= C[i] * (d + 1 - last[i])
    return ans


score = score0(T)
for d, q in [tuple(MI0()) for _ in range(M)]:
    val = eval(d, q)
    score += val
    print(score)
# Original solution
def scoring():
    score = 0
    last = [0] * 26
    for d in range(len(out)):
        last[out[d]] = d + 1
        score += S[d][out[d]]
        score -= sum((d + 1 - l) * c for l, c in zip(last, C))
    return score

def local_search(d, q, score):
    # old = out[d]
    out[d] = q
    tmp = scoring()
    # if tmp > score:
        # score = tmp
    # else:
        # out[d] = old
    return tmp

D = int(eval(input()))
C = list(map(int, input().split()))
S = []
for _ in range(D):
    s = list(map(int, input().split()))
    S.append(s)
out = [int(eval(input())) - 1 for _ in range(D)]
res = scoring()
M = int(eval(input()))
for _ in range(M):
    d, q = list(map(int, input().split()))
    res = local_search(d-1, q-1, res)
    print(res)


# Optimized solution
import sys
input = sys.stdin.readline

def scoring(sc):
    last = [0] * 26
    res = 0
    penalty = 0
    for i in range(D):
        t = out[i]
        res += S[i][t]
        penalty += sc - (i + 1 - last[t]) * C[t]
        res -= penalty
        last[t] = i + 1
    return res

D = int(eval(input()))
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for _ in range(D)]
out = [int(eval(input())) - 1 for _ in range(D)]
sc = sum(C)
M = int(eval(input()))
for _ in range(M):
    d, q = list(map(int, input().split()))
    out[d-1] = q - 1
    print((scoring(sc)))
# Original solution
from _collections import deque
def main():
    D = int(eval(input()))
    C = list(map(int,input().split()))
    S = deque([])
    for _ in range(D):
        S.append(list(map(int,input().split())))

    SELECT = deque([])
    for i in range(D):
        SELECT.append(int(eval(input()))-1)

    def score(selects):
        score = 0
        L = [0 for _ in range(26)]
        def calc(ans, select, n):
            for i, l in enumerate(L):
                if i != select:
                    ans -= C[i] * (n - L[i])
                else:
                    L[i] = n
            return ans

        for i,select in enumerate(selects):
            score += S[i][select]

            for j, l in enumerate(L):
                if j != select:
                    score -= C[j] * (i+1 - L[j])
                else:
                    L[j] = i+1
        return score

    M = int(eval(input()))
    for _ in range(M):
        i, s = list(map(int,input().split()))
        SELECT[i-1]=s-1
        print((score(SELECT)))

if __name__=="__main__":
    main()

# Optimized solution
D = int(eval(input()))
C = list(map(int,input().split()))
S = [list(map(int,input().split()))for _ in range(D)]

SELECT = []
for i in range(D):
    SELECT.append(int(eval(input()))-1)

M = int(eval(input()))
d = []
q = []
for _ in range(M):
    td, tq = list(map(int,input().split()))
    d.append(td-1)
    q.append(tq-1)

L_ALL = [[0]*26 for _ in range(D)]
L = [0]*26
ans = 0
for i in range(D):
    ans += S[i][SELECT[i]]
    L[SELECT[i]]=i+1
    if i != 0:
        L_ALL[i] = L_ALL[i-1][:]
    L_ALL[i][SELECT[i]] = i+1
    for j in range(26):
        ans -= C[j]*(i+1-L[j])

for i in range(M):
    day = d[i]
    before = SELECT[day]
    after = q[i]
    ans -= S[day][before]
    ans += S[day][after]
    SELECT[day] = q[i]

    if day == 0:
        tmp = 0
    else:
        tmp = L_ALL[day-1][before]
    j = day
    while 0<=j<D and L_ALL[j][before]==day+1:
        L_ALL[j][before] = tmp
        ans -= C[before]*(day+1-tmp)
        j += 1

    if day == 0:
        tmp = 0
    else:
        tmp = L_ALL[day-1][after]
    j = day
    while 0<=j<D and L_ALL[j][after]==tmp:
        L_ALL[j][after]=day+1
        ans += C[after] * (day+1-tmp)
        j += 1

    print(ans)
# Original solution
D = int(eval(input()))
C = list(map(int,input().split()))
S = [list(map(int,input().split()))for _ in range(D)]

SELECT = []
for i in range(D):
    SELECT.append(int(eval(input()))-1)

M = int(eval(input()))
d = []
q = []
for _ in range(M):
    td, tq = list(map(int,input().split()))
    d.append(td-1)
    q.append(tq-1)

L_ALL = [[0]*26 for _ in range(D)]
L = [0]*26
ans = 0
for i in range(D):
    ans += S[i][SELECT[i]]
    L[SELECT[i]]=i+1
    if i != 0:
        L_ALL[i] = L_ALL[i-1][:]
    L_ALL[i][SELECT[i]] = i+1
    for j in range(26):
        ans -= C[j]*(i+1-L[j])

for i in range(M):
    day = d[i]
    before = SELECT[day]
    after = q[i]
    ans -= S[day][before]
    ans += S[day][after]
    SELECT[day] = q[i]

    if day == 0:
        tmp = 0
    else:
        tmp = L_ALL[day-1][before]
    j = day
    while 0<=j<D and L_ALL[j][before]==day+1:
        L_ALL[j][before] = tmp
        ans -= C[before]*(day+1-tmp)
        j += 1

    if day == 0:
        tmp = 0
    else:
        tmp = L_ALL[day-1][after]
    j = day
    while 0<=j<D and L_ALL[j][after]==tmp:
        L_ALL[j][after]=day+1
        ans += C[after] * (day+1-tmp)
        j += 1

    print(ans)

# Optimized solution
def main():
    D = int(eval(input()))
    C = list(map(int,input().split()))
    S = [list(map(int,input().split()))for _ in range(D)]

    SELECT = []
    for i in range(D):
        SELECT.append(int(eval(input()))-1)

    M = int(eval(input()))
    d = []
    q = []
    for _ in range(M):
        td, tq = list(map(int,input().split()))
        d.append(td-1)
        q.append(tq-1)

    L_ALL = [[0]*26 for _ in range(D)]
    L = [0]*26
    ans = 0
    for i in range(D):
        ans += S[i][SELECT[i]]
        L[SELECT[i]]=i+1
        if i != 0:
            L_ALL[i] = L_ALL[i-1][:]
        L_ALL[i][SELECT[i]] = i+1
        for j in range(26):
            ans -= C[j]*(i+1-L[j])

    for i in range(M):
        day = d[i]
        before = SELECT[day]
        after = q[i]
        ans -= S[day][before]
        ans += S[day][after]
        SELECT[day] = q[i]

        if day == 0:
            tmp = 0
        else:
            tmp = L_ALL[day-1][before]
        j = day
        while 0<=j<D and L_ALL[j][before]==day+1:
            L_ALL[j][before] = tmp
            ans -= C[before]*(day+1-tmp)
            j += 1

        if day == 0:
            tmp = 0
        else:
            tmp = L_ALL[day-1][after]
        j = day
        while 0<=j<D and L_ALL[j][after]==tmp:
            L_ALL[j][after]=day+1
            ans += C[after] * (day+1-tmp)
            j += 1

        print(ans)

if __name__=="__main__":
    main()
# Original solution
D=int(eval(input()))
c = list(map(int,input().split()))

s=[None]*D
for i in range(D):
    s[i] = list(map(int,input().split()))
t=[0]*D
for i in range(D):
    t[i]=int(eval(input()))
    
m=int(eval(input()))
d=[None]*m
q=[None]*m
for i in range(m):
    d[i],q[i] = list(map(int,input().split()))

def calc_score(t):
    last=[0]*26
    satis=0
    pena=0
    for j in range(D):
        ss=s[j]
        tt=t[j]
        satis += ss[tt-1]
        last[tt-1]=j+1
        
        pena += sum(c[i]*(j+1-last[i]) for i in range(26))
    return satis-pena

# calc_score(t)-10**6
for i in range(m):
    t[d[i]-1] = q[i]
    print((calc_score(t)))

# Optimized solution
D=int(eval(input()))
c = list(map(int,input().split()))

s=[None]*D
for i in range(D):
    s[i] = list(map(int,input().split()))
t=[0]*D
for i in range(D):
    t[i]=int(eval(input()))
    
m=int(eval(input()))
d=[None]*m
q=[None]*m
for i in range(m):
    d[i],q[i] = list(map(int,input().split()))
sumc=sum(c)



def calc_score(t):
    last=[0]*26
    satis=0
    pena=0
    delta_pena=0
    for j in range(D):
        ss=s[j]
        tt=t[j]
        satis += ss[tt-1]
        delta_pena += sumc - c[tt-1]*(j+1-last[tt-1])

        last[tt-1]=j+1   
        pena += delta_pena
    return satis-pena

for i in range(m):
    t[d[i]-1] = q[i]
    print((calc_score(t)))
# Original solution
D = int(eval(input()))
c = [int(i) for i in input().split()]
s = [[int(i) for i in input().split()] for _ in range(D)]
t = [int(eval(input()))-1 for _ in range(D)]
llst = [0]*26

def scoreing():
    res = 0
    for i in range(26): llst[i] = 0
    for day,i in enumerate(list(range(D)),1):
        llst[t[i]] = day
        res += s[i][t[i]]
        for j in range(26): res -= c[j]*(day-llst[j])
    return res
        
M = int(eval(input()))
ans = [0]*M
for i in range(M):
    d,q = list(map(int,input().split()))
    d -= 1
    q -= 1
    t[d] = q
    ans[i] = scoreing()
for i in range(M): print((ans[i]))

# Optimized solution
D = int(eval(input()))
c = [int(i) for i in input().split()]
s = [[int(i) for i in input().split()] for _ in range(D)]
t = [int(eval(input()))-1 for _ in range(D)]

def scoreing(x):
    res = 0
    last = 0
    for day,i in enumerate(list(range(D)),1):
        if t[i] == x:
            last = day
            res += s[i][x]
        res -= c[x]*(day-last)
    return res

score = [0]*26
for i in range(26): score[i] = scoreing(i)
M = int(eval(input()))
ans = [0]*M
for i in range(M):
    d,q = list(map(int,input().split()))
    d -= 1
    q -= 1
    old = t[d]
    t[d] = q
    score[old] = scoreing(old)
    score[q] = scoreing(q)
    ans[i] = sum(score)
for i in range(M): print((ans[i]))
# Original solution
import sys
from bisect import bisect_right, bisect_left


def checkScore(p):
    global D, last, S
    rst = 0
    _last = last[p]
    for d in range(1, D + 1):
        down = C[p] * (d - _last[d])
        up = S[d][p] if _last[d] == d else 0
        rst += up - down
    return rst


Z = 26
D = int(eval(input()))
C = list(map(int, input().split()))
S = [tuple([0] * Z)] + [tuple(map(int, input().split())) for _ in range(D)]
T = [None] + [int(eval(input())) - 1 for _ in range(D)]

M = int(eval(input()))
Q = [tuple([int(x) - 1 for x in s.split()]) for s in sys.stdin.readlines()]


last = [[0] * (D + 1) for _ in range(Z)]
V = [0] * Z
for d in range(1, D + 1):
    t = T[d]
    for p in range(Z):
        last[p][d] = last[p][d - 1]
    last[t][d] = d
for p in range(Z):
    V[p] = checkScore(p)

score = sum(V)

for d, q in Q:
    d += 1

    p = T[d]
    s = bisect_left(last[p], d)
    e = bisect_right(last[p], d)
    for j in range(s, e):
        last[p][j] = last[p][j - 1]

    old = last[q][d]
    e = bisect_right(last[q], old)
    for j in range(d, e):
        last[q][j] = d

    score -= V[p] + V[q]
    V[p] = checkScore(p)
    V[q] = checkScore(q)
    score += V[p] + V[q]
    print(score)

    T[d] = q


# Optimized solution
import sys
from bisect import bisect_right, bisect_left


def calculate_score(c):
    global D, last, S
    rst = 0
    _last = last[c]
    for d in range(1, D + 1):
        down = C[c] * (d - _last[d])
        up = S[d][c] if _last[d] == d else 0
        rst += up - down
    return rst


Z = 26
D = int(eval(input()))
C = list(map(int, input().split()))

S = [tuple([0] * Z)] + [tuple(map(int, input().split())) for _ in range(D)]
T = [None] + [int(eval(input())) - 1 for _ in range(D)] # 

M = int(eval(input()))
Q = [tuple([int(x) - 1 for x in s.split()]) for s in sys.stdin.readlines()]


last = [[0] * (D + 1) for _ in range(Z)]  # last[i][d]
V = [0] * Z  # 各コンテストiごとの得点を保管しておく

# 初期状態でのlastを求める
for d in range(1, D + 1):
    t = T[d]
    for p in range(Z):
        last[p][d] = last[p][d - 1]
    last[t][d] = d
# 各コンテストpごとに得点を求める
for p in range(Z):
    V[p] = calculate_score(p)

# 初期状態での得点
score = sum(V)

for d, q in Q:
    d += 1  # Qだけ0-indexなので補正
    p = T[d]

    # d日目の開催が p -> q に変わる
    # まず, last[p]を更新する. 更新箇所はd日目 ~ 次の開催日直前
    e = bisect_right(last[p], d)
    for j in range(d, e):
        last[p][j] = last[p][j - 1]

    # 次に, last[q]を更新する. 更新箇所はd日目 ~ 次の開催日直前
    old = last[q][d]
    e = bisect_right(last[q], old)
    for j in range(d, e):
        last[q][j] = d

    score -= V[p] + V[q]
    V[p] = calculate_score(p)
    V[q] = calculate_score(q)
    score += V[p] + V[q]
    print(score)

    T[d] = q
# Original solution
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))))

# Optimized solution
def P(s):
 i,R,P,*L=[0]*29
 while i<D:t=I[i+D][0]-1;P+=s-(i+1-L[t])*C[t];R+=I[i][t]-P;i+=1;L[t]=i
 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))))
# Original solution
def P(s):
 i,R,P,*L=[0]*29
 while i<D:t=I[i+D][0]-1;P+=s-(i+1-L[t])*C[t];R+=I[i][t]-P;i+=1;L[t]=i
 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))))

# Optimized solution
def P(s):
 i,R,P,*L=[0]*29
 while i<D:t=I[i+D][0]-1;P+=s+(~i+L[t])*C[t];R+=I[i][t]-P;i+=1;L[t]=i
 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))))
# Original solution
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

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


# Optimized solution
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
# Original solution
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


# Optimized solution
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)
# Original solution
D=int(eval(input()))
C=list(map(int,input().split()))
S=[list(map(int,input().split())) for i in range(D)]
T=[int(eval(input()))-1 for i in range(D)]
X=[0]*26
Y=[0]*26
M=int(eval(input()))
d,q=0,0
P=0
for i in range(D):
  P+=S[i][T[i]]
  for j in range(26):
    X[j]+=C[j]
  X[T[i]]=0
  for j in range(26):
    Y[j]+=X[j]
for i in range(M):
  d,q=list(map(int,input().split()))
  d,q=d-1,q-1
  P-=S[d][T[d]]
  P+=S[d][q]
  for j in range(26):
    X[j]=0
  Y[T[d]],Y[q]=0,0
  for j in range(D):
    if j==d:
      Z=q
    else:
      Z=T[j]
    for j in range(26):
      X[j]+=C[j]
    X[Z]=0
    Y[T[d]]+=X[T[d]]
    Y[q]+=X[q]
  T[d]=q
  print((P-sum(Y)))

# Optimized solution
D=int(eval(input()))
C=list(map(int,input().split()))
S=[list(map(int,input().split())) for i in range(D)]
T=[int(eval(input()))-1 for i in range(D)]
X=[0]*26
Y=[0]*26
M=int(eval(input()))
d,q=0,0
P=0
for i in range(D):
  P+=S[i][T[i]]
  for j in range(26):
    X[j]+=C[j]
  X[T[i]]=0
  for j in range(26):
    Y[j]+=X[j]
for i in range(M):
  d,q=list(map(int,input().split()))
  d,q=d-1,q-1
  P-=S[d][T[d]]
  P+=S[d][q]
  for j in range(26):
    X[j]=0
  Y[T[d]],Y[q]=0,0
  for j in range(D):
    if j==d:
      Z=q
    else:
      Z=T[j]
    X[T[d]]+=C[T[d]]
    X[q]+=C[q]
    X[Z]=0
    Y[T[d]]+=X[T[d]]
    Y[q]+=X[q]
  T[d]=q
  print((P-sum(Y)))
# Original solution
import sys
input = sys.stdin.readline

d = int(eval(input()))
*C, = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(d)]
T = [int(eval(input())) - 1 for i in range(d)]
L = [[-1]]


def score(T):
    L = [-1 for j in range(26)]
    tmp = 0
    for i in range(d):
        tmp += S[i][T[i]]
        L[T[i]] = i
        tmp -= sum([C[j] * (i - L[j]) for j in range(26)])
    return tmp


m = int(eval(input()))
for k in range(m):
    p, q = list(map(int, input().split()))
    T[p - 1] = q - 1
    print((score(T)))


# Optimized solution
d = int(eval(input()))
dd = d * (d + 1) // 2
*C, = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(d)]
T = [int(eval(input())) - 1 for i in range(d)]
m = int(eval(input()))
PQ = [tuple(map(int, input().split())) for k in range(m)]
PQ = [(p - 1, q - 1) for p, q in PQ]

for p, q in PQ:
    T[p] = q
    L = [-1 for j in range(26)]
    X = [0 for j in range(26)]
    score = 0
    for i in range(d):
        score += S[i][T[i]]
        X[T[i]] += (d - i) * (i - L[T[i]])
        L[T[i]] = i
    for j in range(26):
        score -= C[j] * (dd - X[j])
    print(score)
# Original solution
d = int(eval(input()))
dd = d * (d + 1) // 2
*C, = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(d)]
T = [int(eval(input())) - 1 for i in range(d)]
m = int(eval(input()))
PQ = [tuple(map(int, input().split())) for k in range(m)]
PQ = [(p - 1, q - 1) for p, q in PQ]

for p, q in PQ:
    T[p] = q
    L = [-1 for j in range(26)]
    X = [0 for j in range(26)]
    score = 0
    for i in range(d):
        score += S[i][T[i]]
        X[T[i]] += (d - i) * (i - L[T[i]])
        L[T[i]] = i
    for j in range(26):
        score -= C[j] * (dd - X[j])
    print(score)


# Optimized solution
d = int(eval(input()))
dd = d * (d + 1) // 2
*C, = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(d)]


def calc_score(T):
    L = [-1 for j in range(26)]
    X = [0 for j in range(26)]
    score = 0
    for i in range(d):
        score += S[i][T[i]]
        X[T[i]] += (d - i) * (i - L[T[i]])
        L[T[i]] = i
    for j in range(26):
        score -= C[j] * (dd - X[j])
    return score


T = [int(eval(input())) - 1 for i in range(d)]
m = int(eval(input()))
PQ = [tuple(map(int, input().split())) for k in range(m)]
PQ = [(p - 1, q - 1) for p, q in PQ]

for p, q in PQ:
    T[p] = q
    print((calc_score(T)))