defmodule Main do
  def main do
    _ = IO.gets("")
    t = 
      IO.gets("")
      |> String.trim()
      |> String.split()
      |> Enum.map(&String.to_integer/1)
      |> Enum.with_index(1)
    m = 
      IO.gets("")
      |> String.trim()
      |> String.to_integer()
    px =
      IO.stream(:stdio, :line)
      |> Enum.take(m)
      |> Enum.map(&String.trim/1)
      |> Enum.map(&String.split/1)
      |> Enum.map(&Enum.map(&1, fn elem -> String.to_integer(elem) end))
    
    process(px, t)
  end
  
  
  defp process([], _), do: nil
  defp process([head | tail], t) do
  	_process(head, t, 0)
    process(tail, t)
  end
  
  defp _process([p, x], [], acc), do: IO.puts(acc)
  defp _process([p, x], [{val, index} | tail], acc) when p == index, do: _process([p, x], tail, acc + x)
  defp _process(px, [{val, _} | tail], acc), do: _process(px, tail, acc + val)
end