local day = io.read("*n")
local c = {}
for i = 1, 26 do
  c[i] = io.read("*n")
end
local s = {}
for id = 1, day do
  s[id] = {}
  for i = 1, 26 do
    s[id][i] = io.read("*n")
  end
end

local function solve(t, debug)
  local cur = 0
  local lastday = {}
  for i = 1, 26 do lastday[i] = 0 end
  for id = 1, day do
    local pid = t[id]
    cur = cur + s[id][pid]
    lastday[pid] = id
    for i = 1, 26 do
      cur = cur - c[i] * (id - lastday[i])
    end
    if debug then print(cur) end
  end
  return cur
end
local t = {}
for id = 1, day do
  t[id] = io.read("*n")
  -- t[id] = 1 + (id % 26)
end
local m = io.read("*n")
for i = 1, m do
  local m, q = io.read("*n", "*n")
  t[m] = q
  print(solve(t))
end
-- local score = solve(t)
-- math.randomseed(1)
-- local iter = 1
-- local random = math.random
-- while os.clock() < 1.5 do
--   iter = iter + 1
--   local changeday = random(1, 365)
--   local changeto = random(1, 26)
--   local changefrom = t[changeday]
--   t[changeday] = changeto
--   local tmp = solve(t)
--   if score < tmp then
--     score = tmp
--   else
--     t[changeday] = changefrom
--   end
-- end
-- print(iter)
-- -- print(table.concat(t, "\n"))
