program r20628c
 
implicit none
integer :: c(1:26)
integer, allocatable :: interval(:, :)
integer, allocatable :: s(:, :)
integer, allocatable :: t(:), v(:)
integer :: i, j, d, m, h
integer, allocatable :: smalld(:)
integer, allocatable :: q(:)


read *, d

allocate(s(1:26, 1:d))
allocate(interval(1:26, 0:d))
allocate(t(1:d))
allocate(v(0:d))

interval(:, :) = 0 !interval(i,d) = d - last(d,i)
v(0) = 0

read *, c(1:26)

do i = 1, d
 read *, s(:, i)
end do

do i = 1, d
 read *, t(i)
 
 v(i) = v(i-1) + s(t(i), i)
 
 do j = 1, 26
  if(t(i) == j) then
   interval(j, i) = 0
  else
   interval(j, i) = interval(j, i-1) + 1
  end if
  v(i) = v(i) - (c(j) * interval(j, i))
 end do
 
! print *, v(i)
end do

read *, m

allocate(smalld(1:m))
allocate(q(1:m))

do h = 1, m
 read *, smalld(h), q(h)
 t(smalld(h)) = q(h)
 
  v(smalld(h)) = v(smalld(h)-1) + s(q(h), smalld(h))
  
  do j = 1, 26
  
   if(q(h) == j) then
    interval(j, smalld(h)) = 0
   else
    interval(j, smalld(h)) = interval(j, smalld(h)-1) + 1
   end if
   
   v(smalld(h)) = v(smalld(h)) - (c(j) * interval(j, smalld(h)))
  end do   
 
 do i = smalld(h)+1, d
 
 v(i) = v(i-1) + s(t(i), i)
 
  do j = 1, 26
   if(t(i) == j) then
    interval(j, i) = 0
   else
    interval(j, i) = interval(j, i-1) + 1
   end if
   v(i) = v(i) - (c(j) * interval(j, i))
  end do   
 end do
 
 print *, v(d)
 
end do


end program r20628c