program incremental_scoring
  implicit none
  integer :: d, c(26) = 0, s(26, 365) = 0, t(365) = 0, m, p, q, i
  read(*,*) d
  read(*,*) c
  read(*,*) (s(:, i), i = 1, d)
  read(*,*) (t(i), i = 1, d)
  read(*,*) m
  do i = 1, m
    read(*,*) p, q
    t(p) = q
    write(*,'(i0)') score()
  end do
contains
  integer function score() result(res)
    integer :: last(26), i
    last = 0
    res = 0
    do i = 1, d
      last(t(i)) = i
      res = res + s(t(i), i) - sum(c * (i - last))
    end do
  end
end program incremental_scoring