import sys, heapq

def calcScore_change(D, complaints, satisfications, HoldedContestDays, N_queues, queues):
    TotalSatisfication = 0
    ContestDays_List = [ [i+1] * 26 for i in range(D) ] # [D][j]

    complaint_all = 0
    for i in range(26):
        complaint_all += complaints[i]

    complaint_sum = 0
    for i in range(D):
        complaint_sum += complaint_all
        Contest = HoldedContestDays[i]
        complaint_sum -= ContestDays_List[i][Contest] * complaints[Contest]
        ContestDays_List[i][Contest] = 0

        TotalSatisfication += satisfications[i][Contest] - complaint_sum

        for j in range(i+1, D):
            ContestDays_List[j][Contest] = ContestDays_List[j-1][Contest] + 1

    scores_afterchanging = [TotalSatisfication]
    for m in range(N_queues):
        queue = queues[m]

        day = queue[0]
        contest_change = queue[1]
        contest_old = HoldedContestDays[day]

        # 1. 元のコンテスト変化
        nextcontest = D
        for i in range(day+1, D):
            if(HoldedContestDays[i]==contest_old):
                nextcontest = i
                break
        for i in range(day, nextcontest):
            if(i==0):
                ContestDays_List[i][contest_old] = 1
            else:
                ContestDays_List[i][contest_old] = ContestDays_List[i-1][contest_old]+1
        if(day==0):
            changed_s = 1
        else:
            changed_s = ContestDays_List[day-1][contest_old]+1

        score_change = -changed_s * (nextcontest-day) * complaints[contest_old]

        # 2. その代わりに開催
        nextcontest_new = D
        for i in range(day+1, D):
            if(HoldedContestDays[i]==contest_change):
                nextcontest_new = i
                break
        k = 0
        for i in range(day, nextcontest_new):
            ContestDays_List[i][contest_change] = k
            k+= 1
        if(day==0):
            changed_s2 = 1
        else:
            changed_s2 = ContestDays_List[day-1][contest_change]+1
        change = changed_s2 * (nextcontest_new-day) * complaints[contest_change]
        score_change += change

        #3. score
        score_change += satisfications[day][contest_change] - satisfications[day][contest_old]

        score = scores_afterchanging[-1] + score_change
        scores_afterchanging.append(score)
        HoldedContestDays[day] = contest_change
    return scores_afterchanging
    """
    TotalSatisfication: 変更前のスコア
    di Contest ->qiに変更すると:
        もとのHoldedContestDays, ContestDays_Listの変更を忘れないように

        元のコンテストContestからqiへ変更
        1) ContestDays_List[d][Contest] = ContestDays_List[d-1][Contest] +1になる(次にコンテストが行われ0に初期化される日をd_nextとするとd_next-1まで)
        結果、満足度は (ContestDays_List[di-1][Contest]+1) * (d_next-di) * complaints[Contest]現象

        2)その代わり実行されたコンテストは(qiの次の実行びをd_next_2として)
        (ContestDays_List[di-1][qi]+1) * (d_next_2-di) * complaints[qi]増加

        3)さらに、s[di][qi]-s[di][Contest]上昇
          5 0 1 2 3 0
        ->5 6 7 8 9 0
    """










"""
13: 19922, 89
20: 19830, 80

20: 19830 - 80 * 5 = 19430
13: 19922 - 89 * 2 = 19744
毎日1つのコンテスト
満足度をあげるようにコンテスト
ci * (最後にコンテストを行ってからの経過日数)満足度低下
上昇値はs_d_i

"""
if(__name__ == "__main__"):
    D = int(input().strip())
    complaints = list(map(int, input().split() ) )

    satisfications = []
    for i in range(D):
        satisfication = list(map(int, input().split() ) )
        satisfications.append(satisfication)
    HoldedContestDays =[0] * D
    for i in range(D):
        HoldedContestDays[i] = int(input().strip())
        HoldedContestDays[i] -= 1
    N_queues = int(input().strip())
    queues = []
    for m in range(N_queues):
        queue = list(map(int, input().split() ) )
        queue[0] -= 1
        queue[1] -= 1
        queues.append(queue)
    Score_change = calcScore_change(D, complaints, satisfications, HoldedContestDays, N_queues, queues)

    for m in range(N_queues):
        print(Score_change[m+1])


Measured memory_bytes: 26232