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 scores = {}
local penalties = {}
for i = 1, 26 do
  scores[i] = 0
  penalties[i] = 0
end
local function solveAll(t)
  local lastday = {}
  for i = 1, 26 do lastday[i] = 0 end
  for id = 1, day do
    local pid = t[id]
    scores[pid] = scores[pid] + s[id][pid]
    lastday[pid] = id
    for i = 1, 26 do
      penalties[i] = penalties[i] + c[i] * (id - lastday[i])
    end
  end
end
local function solveDiff(t, iday, pto)
  local pfrom = t[iday]
  scores[pfrom] = scores[pfrom] - s[iday][pfrom]
  scores[pto] = scores[pto] + s[iday][pto]
  t[iday] = pto
  penalties[pfrom] = 0
  penalties[pto] = 0
  local ld = {}
  for i = 1, 26 do ld[i] = 0 end
  for id = 1, day do
    local p = t[id]
    ld[p] = id
    for ip = pfrom, pto, (pto - pfrom) do
      penalties[ip] = penalties[ip] + c[ip] * (id - ld[ip])
    end
  end
end

local function solveSum()
  local z = 0
  for i = 1, 26 do z = z + scores[i] - penalties[i] end
  return z
end

local t = {}
for id = 1, day do
  t[id] = io.read("*n")
  -- t[id] = 1 + (id % 26)
end
solveAll(t)
local score = solveSum()
local m = io.read("*n")
for i = 1, m do
  local d, q = io.read("*n", "*n")
  solveDiff(t, d, q)
  print(solveSum())
end
os.exit()

math.randomseed(1)
local iter = 1
local random = math.random
while os.clock() < 1.85 do
  iter = iter + 1
  local changeday = random(1, 365)
  local changeto = random(1, 25)
  local changefrom = t[changeday]
  if changefrom <= changeto then
    changeto = changeto + 1
  end
  solveDiff(t, changeday, changeto)
  local tmp = solveSum()
  if score < tmp then
    score = tmp
  else
    solveDiff(t, changeday, changefrom)
  end
end
-- print(iter)
print(table.concat(t, "\n"))
