import sys





def B(T):

    global S, Z, D

    _last = [0] * Z

    cnt = 0

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

        t = T[d]

        _last[t] = d

        up = S[d][t]

        down = sum(C[i] * (d - _last[i]) for i in range(Z))

        cnt += up - down

    return cnt





def checkScore(r):

    global D, last

    L = last[r]

    rst = 0

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

        down = C[r] * (d - L[d])

        up = S[d][r] if L[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)]

S += [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]

    T[d] = q



    for r, L in ((p, last[p]), (q, last[q])):

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

            L[e] = L[e - 1] if r != T[e] else e



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

    V[p] = checkScore(p)

    V[q] = checkScore(q)

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

    # # if M > 10 ** 4:

    #     print(score)

    # else:

    #     print(B(T))

    print(score)

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


def B(T):
    global S, Z, D
    _last = [0] * Z
    cnt = 0
    for d in range(1, D + 1):
        t = T[d]
        _last[t] = d
        up = S[d][t]
        down = sum(C[i] * (d - _last[i]) for i in range(Z))
        cnt += up - down
    return cnt


def checkScore(r):
    global D, last
    L = last[r]
    rst = 0
    for d in range(1, D + 1):
        down = C[r] * (d - L[d])
        up = S[d][r] if L[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)]
S += [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]
    T[d] = q
    for r, L in ((p, last[p]), (q, last[q])):
        for e in range(1, D + 1):
            L[e] = L[e - 1] if r != T[e] else e
    score -= V[p] + V[q]
    V[p] = checkScore(p)
    V[q] = checkScore(q)
    score += V[p] + V[q]
    # # if M > 10 ** 4:
    #     print(score)
    # else:
    #     print(B(T))
    print(score)

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

+from bisect import bisect_right, bisect_left -def B(T): -    global S, Z, D -    _last = [0] * Z -    cnt = 0 +def checkScore(p): +    global D, last, S +    rst = 0 +    _last = last[p] -        t = T[d] -        _last[t] = d -        up = S[d][t] -        down = sum(C[i] * (d - _last[i]) for i in range(Z)) -        cnt += up - down -    return cnt - - -def checkScore(r): -    global D, last -    L = last[r] -    rst = 0 -    for d in range(1, D + 1): -        down = C[r] * (d - L[d]) -        up = S[d][r] if L[d] == d else 0 +        down = C[p] * (d - _last[d]) +        up = S[d][p] if _last[d] == d else 0 -S = [tuple([0] * Z)] -S += [tuple(map(int, input().split())) for _ in range(D)] +S = [tuple([0] * Z)] + [tuple(map(int, input().split())) for _ in range(D)] -    T[d] = q -    for r, L in ((p, last[p]), (q, last[q])): -        for e in range(1, D + 1): -            L[e] = L[e - 1] if r != T[e] else e +    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 -    # # if M > 10 ** 4: -    #     print(score) -    # else: -    #     print(B(T)) +    T[d] = q