Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

d = int(input())
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
T = [int(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(input())
for k in range(m):
    p, q = map(int, input().split())
    T[p - 1] = q - 1
    print(score(T))


Fixed submission:
d = int(input())
dd = d * (d + 1) // 2
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
T = [int(input()) - 1 for i in range(d)]
m = int(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)

Buggy python submission (verdict: Time Limit Exceeded):
d = int(input())
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
T = [int(input()) - 1 for i in range(d)]


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(input())
for k in range(m):
    p, q = map(int, input().split())
    T[p - 1] = q - 1
    print(score(T))


Fixed submission:
d = int(input())
dd = d * (d + 1) // 2
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
T = [int(input()) - 1 for i in range(d)]
m = int(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)

Buggy python submission (verdict: Time Limit Exceeded):
d = int(input())
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
T = [int(input()) - 1 for i in range(d)]


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(input())
for k in range(m):
    p, q = map(int, input().split())
    T[p - 1] = q - 1
    print(score(T))


Fixed submission:
d = int(input())
dd = d * (d + 1) // 2
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
T = [int(input()) - 1 for i in range(d)]
m = int(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)