Buggy python submission (verdict: Time Limit Exceeded):
D = int(input())
C = list(map(int,input().split())) #C[i]AiCを実施しないと下がる満足度
S = [] #S[i][j] i日目にAjcというコンテストを実施した場合に上がる満足度
for i in range(D): #コンテストは0index
    temp = list(map(int,input().split()))
    S.append(temp)
T = []
for i in range(D):
    temp = int(input())
    T.append(temp)
M = int(input())
DQ = []
for i in range(M):
    d,q = map(int,input().split())
    DQ.append((d,q))
#since = [0 for _ in range(26)] #最後に開催されてから何日経ったか。
#manzoku = 0
def cycle(T):
    manzoku = 0
    since = [0 for _ in range(26)]
    for i in range(26):
        since[i] += C[i]
    for i in range(D):
        contest = T[i]-1 #0index
        manzoku += S[i][contest]
        for j in range(26):
            if j != contest:
                manzoku -= since[j]
        #print(manzoku)
        for j in range(26):
            if j != contest:
                since[j] += C[j]
            else:
                since[j] = C[j]
    return manzoku
score = cycle(T)
for i in range(M):
    d,q = DQ[i]
    d-=1 #0indexへ
    old = T[d]
    T[d] = q
    nscore = cycle(T)
    print(nscore)
    #if nscore >= score:
    #    print(nscore)
    #else:
    #    print(score)
    #    T[d] = old

Fixed submission:
from collections import defaultdict
D = int(input())
C = list(map(int,input().split())) #C[i]AiCを実施しないと下がる満足度
S = [] #S[i][j] i日目にAjcというコンテストを実施した場合に上がる満足度
for i in range(D): #コンテストは0index
    temp = list(map(int,input().split()))
    S.append(temp)
T = []
dic = defaultdict(list)
for i in range(D):
    temp = int(input())
    temp-=1 #0index
    T.append(temp)
    dic[temp].append(i)
for i in range(len(dic)):
    dic[i].sort()
M = int(input())
DQ = []
for i in range(M):
    d,q = map(int,input().split())
    DQ.append((d,q))
#since = [0 for _ in range(26)] #最後に開催されてから何日経ったか。
#manzoku = 0
def cycle(T):
    manzoku = 0
    since = [0 for _ in range(26)]
    for i in range(26):
        since[i] += C[i]
    for i in range(D):
        contest = T[i] #0index
        manzoku += S[i][contest]
        for j in range(26):
            if j != contest:
                manzoku -= since[j]
        #print(manzoku)
        for j in range(26):
            if j != contest:
                since[j] += C[j]
            else:
                since[j] = C[j]
    return manzoku
score = cycle(T)
#print(score)
for i in range(M):
    #print(dic)
    d,q = DQ[i] #d日目をAqCに変える。
    d-=1;q-=1 #0indexへ
    old = T[d]
    try:
        oscore = nscore*1
    except:
        oscore= score*1
    dic[old].remove(d)
    T[d] = q
    if old == q:
        nscore = oscore
    else:
        impact = S[d][q]-S[d][old] #コンテスト実施による満足度の変化の差
        #print(impact,C[q],C[old])
        if not dic[q]:
            impact += C[q]*(D-d)*(d+1)
        else:
            for j in range(len(dic[q])):
                if d < dic[q][0]:
                    impact += C[q]*(d+1)*(dic[q][0]-d)
                    break
                elif d > dic[q][-1]:
                    impact += C[q]*(D-d)*(d-dic[q][-1])
                    break
                else:
                    if dic[q][j] < d and d < dic[q][j+1]:
                        impact += C[q]*(d-dic[q][j])*(dic[q][j+1]-d)
                        break
        dic[q].append(d)
        dic[q].sort()
        if not dic[old]:
            impact -= C[old]*(D-d)*(d+1)
        else:
            for j in range(len(dic[old])):
                if d < dic[old][0]:
                    impact -= C[old]*(d+1)*(dic[old][0]-d)
                    break
                elif d > dic[old][-1]:
                    impact -= C[old]*(D-d)*(d-dic[old][-1])
                    break
                else:
                    if dic[old][j] < d and d < dic[old][j+1]:
                        impact -= C[old]*(d-dic[old][j])*(dic[old][j+1]-d)
                        break
        nscore = oscore+impact
    #nscore = cycle(T)
    print(nscore)
    #if nscore >= score:
    #    print(nscore)
    #else:
    #    print(score)
    #    T[d] = old