Buggy python submission (verdict: Time Limit Exceeded):
import sys


def read():
    return sys.stdin.readline().rstrip()


def main():
    d = int(read())
    c = [int(i) for i in read().split()]
    s = [[int(i) for i in read().split()] for _ in range(d)]
    t = [int(read()) - 1 for _ in range(d)]
    m = int(read())
    query = [[int(i) - 1 for i in read().split()] for _ in range(m)]
    score = [0] * d
    last = [[0] * 26 for _ in range(d)]
    for i in range(d):
        score[i] = s[i][t[i]]
        if i > 0:
            last[i] = last[i - 1].copy()
        last[i][t[i]] = i + 1
        for j in range(26):
            score[i] -= c[j] * (i + 1 - last[i][j])
    for di, qi in query:
        if di == 0:
            last[di][t[di]] = 0
        else:
            last[di][t[di]] = last[di - 1][t[di]]
        last[di][qi] = di + 1
        t[di] = qi
        for i in range(di, d):
            score[i] = s[i][t[i]]
            if i > 0:
                last[i] = last[i - 1].copy()
            last[i][t[i]] = i + 1
            for j in range(26):
                score[i] -= c[j] * (i + 1 - last[i][j])
        print(sum(score))


if __name__ == '__main__':
    main()


Fixed submission:
import sys


def read():
    return sys.stdin.readline().rstrip()


def main():
    d = int(read())
    c = [int(i) for i in read().split()]
    s = [[int(i) for i in read().split()] for _ in range(d)]
    t = [int(read()) - 1 for _ in range(d)]
    m = int(read())
    query = [[int(i) - 1 for i in read().split()] for _ in range(m)]
    score = 0
    last = [[0] * 26 for _ in range(d)]
    for i in range(d):
        score += s[i][t[i]]
        if i > 0:
            last[i] = last[i - 1].copy()
        last[i][t[i]] = i + 1
        for j in range(26):
            score -= c[j] * (i + 1 - last[i][j])
    for di, qi in query:
        hi = t[di]
        t[di] = qi
        score -= s[di][hi]
        score += s[di][qi]
        if di == 0:
            last[di][hi] = 0
        else:
            last[di][hi] = last[di - 1][hi]
        score -= c[hi] * (di + 1 - last[di][hi])
        score += c[qi] * (di + 1 - last[di][qi])
        last[di][qi] = di + 1
        for i in range(di + 1, d):
            score += c[hi] * (i + 1 - last[i][hi])
            if t[i] != hi:
                last[i][hi] = last[i - 1][hi]
                score -= c[hi] * (i + 1 - last[i][hi])
            else:
                break
        for i in range(di + 1, d):
            score += c[qi] * (i + 1 - last[i][qi])
            if t[i] != qi:
                last[i][qi] = last[i - 1][qi]
                score -= c[qi] * (i + 1 - last[i][qi])
            else:
                break
        print(score)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import sys


def read():
    return sys.stdin.readline().rstrip()


def main():
    d = int(read())
    c = [int(i) for i in read().split()]
    s = [[int(i) for i in read().split()] for _ in range(d)]
    t = [int(read()) - 1 for _ in range(d)]
    m = int(read())
    query = [[int(i) - 1 for i in read().split()] for _ in range(m)]
    score = [0] * 26
    last = [[0] * 26 for _ in range(d)]
    for i in range(d):
        score[i] = s[i][t[i]]
        if i > 0:
            last[i] = last[i - 1].copy()
        last[i][t[i]] = i + 1
        for j in range(26):
            score[i] -= c[j] * (i + 1 - last[i][j])
    for di, qi in query:
        if di == 0:
            last[di][t[di]] = 0
        else:
            last[di][t[di]] = last[di - 1][t[di]]
        last[di][qi] = di + 1
        t[di] = qi
        for i in range(di, d):
            score[i] = s[i][t[i]]
            if i > 0:
                last[i] = last[i - 1].copy()
            last[i][t[i]] = i + 1
            for j in range(26):
                score[i] -= c[j] * (i + 1 - last[i][j])
        print(sum(score))


if __name__ == '__main__':
    main()


Fixed submission:
import sys


def read():
    return sys.stdin.readline().rstrip()


def main():
    d = int(read())
    c = [int(i) for i in read().split()]
    s = [[int(i) for i in read().split()] for _ in range(d)]
    t = [int(read()) - 1 for _ in range(d)]
    m = int(read())
    query = [[int(i) - 1 for i in read().split()] for _ in range(m)]
    score = 0
    last = [[0] * 26 for _ in range(d)]
    for i in range(d):
        score += s[i][t[i]]
        if i > 0:
            last[i] = last[i - 1].copy()
        last[i][t[i]] = i + 1
        for j in range(26):
            score -= c[j] * (i + 1 - last[i][j])
    for di, qi in query:
        hi = t[di]
        t[di] = qi
        score -= s[di][hi]
        score += s[di][qi]
        if di == 0:
            last[di][hi] = 0
        else:
            last[di][hi] = last[di - 1][hi]
        score -= c[hi] * (di + 1 - last[di][hi])
        score += c[qi] * (di + 1 - last[di][qi])
        last[di][qi] = di + 1
        for i in range(di + 1, d):
            score += c[hi] * (i + 1 - last[i][hi])
            if t[i] != hi:
                last[i][hi] = last[i - 1][hi]
                score -= c[hi] * (i + 1 - last[i][hi])
            else:
                break
        for i in range(di + 1, d):
            score += c[qi] * (i + 1 - last[i][qi])
            if t[i] != qi:
                last[i][qi] = last[i - 1][qi]
                score -= c[qi] * (i + 1 - last[i][qi])
            else:
                break
        print(score)


if __name__ == '__main__':
    main()