program d613

implicit none
integer :: n, q, i, j, t
integer, allocatable :: v(:), w(:), a(:), l(:), aw(:), av(:)

read *, n
allocate(v(1:n))
allocate(w(1:n))

do i = 1, n
 read *, v(:), w(:)
end do

read *, q
allocate(a(1:q))
allocate(l(1:q))

do i = 1, q
 read *, a(:), l(:)
end do

av(:) = 0
aw(:) = 0

do i = 1, q
 t = a(i)
 do while(t >= 1)
  av(i) = av(i) + v(t)
  aw(i) = aw(i) + aw(i)
  t = t / 2
 end do
end do

do i = 1, q
print *, av(i)
end do

end program d613