Buggy python submission (verdict: Time Limit Exceeded):
D = int(input())
c = [int(i) for i in input().split()]
s = [[int(i) for i in input().split()] for _ in range(D)]
t = [int(input())-1 for _ in range(D)]

def calc(x):
    res = 0
    last = 0
    for day,i in enumerate(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] = calc(i)
M = int(input())
ans = [0]*M
for i in range(M):
    d,q = map(int,input().split())
    d -= 1
    q -= 1
    old = t[d]
    t[d] = q
    score[old] = calc(old)
    score[q] = calc(q)
    ans[i] = sum(score)
for i in range(M): print(ans[i])


Fixed submission:
D = int(input())
c = [int(i) for i in input().split()]
s = [[int(i) for i in input().split()] for _ in range(D)]
t = [int(input())-1 for _ in range(D)]

def scoreing(x):
    res = 0
    last = 0
    for day,i in enumerate(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(input())
ans = [0]*M
for i in range(M):
    d,q = 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])

Buggy python submission (verdict: Time Limit Exceeded):
D = int(input())
c = [int(i) for i in input().split()]
s = [[int(i) for i in input().split()] for _ in range(D)]
t = [int(input())-1 for _ in range(D)]

def calc(x):
    res = 0
    last = 0
    for day,i in enumerate(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] = calc(i)
M = int(input())
ans = [0]*M
for i in range(M):
    d,q = map(int,input().split())
    d -= 1
    q -= 1
    old = t[d]
    t[d] = q
    score[old] = calc(old)
    score[q] = calc(q)
    ans[i] = sum(score)
for i in range(M): print(ans[i])

Fixed submission:
D = int(input())
c = [int(i) for i in input().split()]
s = [[int(i) for i in input().split()] for _ in range(D)]
t = [int(input())-1 for _ in range(D)]

def scoreing(x):
    res = 0
    last = 0
    for day,i in enumerate(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(input())
ans = [0]*M
for i in range(M):
    d,q = 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])

Buggy python submission (verdict: Time Limit Exceeded):
D = int(input())
c = [int(i) for i in input().split()]
s = [[int(i) for i in input().split()] for _ in range(D)]
t = [int(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(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(input())
ans = [0]*M
for i in range(M):
    d,q = map(int,input().split())
    d -= 1
    q -= 1
    t[d] = q
    ans[i] = scoreing()
for i in range(M): print(ans[i])

Fixed submission:
D = int(input())
c = [int(i) for i in input().split()]
s = [[int(i) for i in input().split()] for _ in range(D)]
t = [int(input())-1 for _ in range(D)]

def scoreing(x):
    res = 0
    last = 0
    for day,i in enumerate(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(input())
ans = [0]*M
for i in range(M):
    d,q = 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])