import sys

from bisect import bisect_right, bisect_left





def checkScore(p):

    global D, last, S

    rst = 0

    _last = last[p]

    for d in range(1, D + 1):

        down = C[p] * (d - _last[d])

        up = S[d][p] if _last[d] == d else 0

        rst += up - down

    return rst





Z = 26

D = int(eval(input()))

C = list(map(int, input().split()))

S = [tuple([0] * Z)] + [tuple(map(int, input().split())) for _ in range(D)]

T = [None] + [int(eval(input())) - 1 for _ in range(D)]



M = int(eval(input()))

Q = [tuple([int(x) - 1 for x in s.split()]) for s in sys.stdin.readlines()]





last = [[0] * (D + 1) for _ in range(Z)]

V = [0] * Z

for d in range(1, D + 1):

    t = T[d]

    for p in range(Z):

        last[p][d] = last[p][d - 1]

    last[t][d] = d

for p in range(Z):

    V[p] = checkScore(p)



score = sum(V)



for d, q in Q:

    d += 1



    p = T[d]

    s = bisect_left(last[p], d)

    e = bisect_right(last[p], d)

    for j in range(s, e):

        last[p][j] = last[p][j - 1]



    old = last[q][d]

    e = bisect_right(last[q], old)

    for j in range(d, e):

        last[q][j] = d



    score -= V[p] + V[q]

    V[p] = checkScore(p)

    V[q] = checkScore(q)

    score += V[p] + V[q]

    print(score)



    T[d] = q

import sys

from bisect import bisect_right, bisect_left





def calculate_score(c):

    global D, last, S

    rst = 0

    _last = last[c]

    for d in range(1, D + 1):

        down = C[c] * (d - _last[d])

        up = S[d][c] if _last[d] == d else 0

        rst += up - down

    return rst





Z = 26

D = int(eval(input()))

C = list(map(int, input().split()))



S = [tuple([0] * Z)] + [tuple(map(int, input().split())) for _ in range(D)]

T = [None] + [int(eval(input())) - 1 for _ in range(D)] # 



M = int(eval(input()))

Q = [tuple([int(x) - 1 for x in s.split()]) for s in sys.stdin.readlines()]





last = [[0] * (D + 1) for _ in range(Z)]  # last[i][d]

V = [0] * Z  # 各コンテストiごとの得点を保管しておく



# 初期状態でのlastを求める

for d in range(1, D + 1):

    t = T[d]

    for p in range(Z):

        last[p][d] = last[p][d - 1]

    last[t][d] = d

# 各コンテストpごとに得点を求める

for p in range(Z):

    V[p] = calculate_score(p)



# 初期状態での得点

score = sum(V)



for d, q in Q:

    d += 1  # Qだけ0-indexなので補正

    p = T[d]



    # d日目の開催が p -> q に変わる

    # まず, last[p]を更新する. 更新箇所はd日目 ~ 次の開催日直前

    e = bisect_right(last[p], d)

    for j in range(d, e):

        last[p][j] = last[p][j - 1]



    # 次に, last[q]を更新する. 更新箇所はd日目 ~ 次の開催日直前

    old = last[q][d]

    e = bisect_right(last[q], old)

    for j in range(d, e):

        last[q][j] = d



    score -= V[p] + V[q]

    V[p] = calculate_score(p)

    V[q] = calculate_score(q)

    score += V[p] + V[q]

    print(score)



    T[d] = q
import sys
from bisect import bisect_right, bisect_left


def checkScore(p):
    global D, last, S
    rst = 0
    _last = last[p]
    for d in range(1, D + 1):
        down = C[p] * (d - _last[d])
        up = S[d][p] if _last[d] == d else 0
        rst += up - down
    return rst


Z = 26
D = int(eval(input()))
C = list(map(int, input().split()))
S = [tuple([0] * Z)] + [tuple(map(int, input().split())) for _ in range(D)]
T = [None] + [int(eval(input())) - 1 for _ in range(D)]
M = int(eval(input()))
Q = [tuple([int(x) - 1 for x in s.split()]) for s in sys.stdin.readlines()]
last = [[0] * (D + 1) for _ in range(Z)]
V = [0] * Z
for d in range(1, D + 1):
    t = T[d]
    for p in range(Z):
        last[p][d] = last[p][d - 1]
    last[t][d] = d
for p in range(Z):
    V[p] = checkScore(p)
score = sum(V)
for d, q in Q:
    d += 1
    p = T[d]
    s = bisect_left(last[p], d)
    e = bisect_right(last[p], d)
    for j in range(s, e):
        last[p][j] = last[p][j - 1]
    old = last[q][d]
    e = bisect_right(last[q], old)
    for j in range(d, e):
        last[q][j] = d
    score -= V[p] + V[q]
    V[p] = checkScore(p)
    V[q] = checkScore(q)
    score += V[p] + V[q]
    print(score)
    T[d] = q

import sys
from bisect import bisect_right, bisect_left


def calculate_score(c):
    global D, last, S
    rst = 0
    _last = last[c]
    for d in range(1, D + 1):
        down = C[c] * (d - _last[d])
        up = S[d][c] if _last[d] == d else 0
        rst += up - down
    return rst


Z = 26
D = int(eval(input()))
C = list(map(int, input().split()))
S = [tuple([0] * Z)] + [tuple(map(int, input().split())) for _ in range(D)]
T = [None] + [int(eval(input())) - 1 for _ in range(D)]  #
M = int(eval(input()))
Q = [tuple([int(x) - 1 for x in s.split()]) for s in sys.stdin.readlines()]
last = [[0] * (D + 1) for _ in range(Z)]  # last[i][d]
V = [0] * Z  # 各コンテストiごとの得点を保管しておく
# 初期状態でのlastを求める
for d in range(1, D + 1):
    t = T[d]
    for p in range(Z):
        last[p][d] = last[p][d - 1]
    last[t][d] = d
# 各コンテストpごとに得点を求める
for p in range(Z):
    V[p] = calculate_score(p)
# 初期状態での得点
score = sum(V)
for d, q in Q:
    d += 1  # Qだけ0-indexなので補正
    p = T[d]
    # d日目の開催が p -> q に変わる
    # まず, last[p]を更新する. 更新箇所はd日目 ~ 次の開催日直前
    e = bisect_right(last[p], d)
    for j in range(d, e):
        last[p][j] = last[p][j - 1]
    # 次に, last[q]を更新する. 更新箇所はd日目 ~ 次の開催日直前
    old = last[q][d]
    e = bisect_right(last[q], old)
    for j in range(d, e):
        last[q][j] = d
    score -= V[p] + V[q]
    V[p] = calculate_score(p)
    V[q] = calculate_score(q)
    score += V[p] + V[q]
    print(score)
    T[d] = q

-def checkScore(p): +def calculate_score(c): -    _last = last[p] +    _last = last[c] -        down = C[p] * (d - _last[d]) -        up = S[d][p] if _last[d] == d else 0 +        down = C[c] * (d - _last[d]) +        up = S[d][c] if _last[d] == d else 0 -T = [None] + [int(eval(input())) - 1 for _ in range(D)] +T = [None] + [int(eval(input())) - 1 for _ in range(D)]  # -last = [[0] * (D + 1) for _ in range(Z)] -V = [0] * Z +last = [[0] * (D + 1) for _ in range(Z)]  # last[i][d] +V = [0] * Z  # 各コンテストiごとの得点を保管しておく +# 初期状態でのlastを求める +# 各コンテストpごとに得点を求める -    V[p] = checkScore(p) +    V[p] = calculate_score(p) +# 初期状態での得点 -    d += 1 +    d += 1  # Qだけ0-indexなので補正 -    s = bisect_left(last[p], d) +    # d日目の開催が p -> q に変わる +    # まず, last[p]を更新する. 更新箇所はd日目 ~ 次の開催日直前 -    for j in range(s, e): +    for j in range(d, e): +    # 次に, last[q]を更新する. 更新箇所はd日目 ~ 次の開催日直前 -    V[p] = checkScore(p) -    V[q] = checkScore(q) +    V[p] = calculate_score(p) +    V[q] = calculate_score(q)