output_description: For each query, print the answer in its own line.
  
  * * *
input_description: The input is given from Standard Input in the following format:
  
      
      
      X
      K
      r_1 r_2 .. r_K
      Q
      t_1 a_1
      t_2 a_2
      :
      t_Q a_Q
attempt: def read_data_f(filename="arc082-e.dat"):
      try:
          LOCAL_FLAG
          import codecs
          import os
  
          lines = []
          file_path = os.path.join(os.path.dirname(__file__), filename)
          with codecs.open(file_path, "r", "utf-8") as f:
              for each in f:
                  lines.append(each.rstrip("\r\n"))
  
      except NameError:
          lines = []
          lines.append(input())
          lines.append(input())
          lines.append(input())
          lines.append(input())
          Q = int(lines[3])
          for i in range(Q):
              lines.append(input())
  
      return lines
  
  
  def F_Sandglass():
      #
      # 180
      # 3
      # 60 120 180
      # 3
      # 30 90
      # 61 1
      # 180 180
      raw_data = read_data_f("arc082-f.dat")
      X = int(raw_data[0])
      K = int(raw_data[1])
      r = list(map(int, raw_data[2].split()))
      Q = int(raw_data[3])
      a = []
      t = []
      for i in range(Q):
          tt, aa = list(map(int, raw_data[i + 4].split()))
          a.append(aa)
          t.append(tt)
  
      # print(r)
      # print(t)
      # print(a)
  
      time = 0
      i = j = 0
      U = X
      L = 0
      c = 0
      isUp = True
      time_diff = 0
      r.append(2000000000)
      t.append(1)
  
      while (i < K + 1) and (time < t[Q - 1]):
          while time <= t[j] and t[j] < r[i]:
              time_diff_temp = t[j] - time
              if isUp:
                  c_temp = max(c - time_diff_temp, -X)
                  U_temp = U
                  L_temp = L
                  if (L + c_temp) < 0:
                      L_temp = min(L + time_diff_temp, X)
              else:
                  c_temp = min(c + time_diff_temp, X)
                  U_temp = U
                  if (U + c_temp) > X:
                      U_temp = max(U - time_diff_temp, 0)
                  L_temp = L
  
              if a[j] > U_temp:
                  print(min(U_temp + c_temp, X))
              elif a[j] < L_temp:
                  print(max(L_temp + c_temp, 0))
              else:
                  print(a[j] + c_temp)
  
              j += 1
  
          time_diff = r[i] - time
          if isUp:
              c = max(c - time_diff, -X)
              if (L + c) < 0:
                  L = -c
                  if L > U:
                      U = L
  
          else:
              c = min(c + time_diff, X)
              if (U + c) > X:
                  U = X - c
                  if L > U:
                      L = U
  
          time = r[i]
          isUp = not isUp
          i += 1
  
      # result = remain(X, A[start_time], t[j] - start_time, start_status)
      # print(result)
  
  
  F_Sandglass()
  
problem_description: Statement
  
  We have a sandglass consisting of two bulbs, bulb A and bulb B. These bulbs
  contain some amount of sand. When we put the sandglass, either bulb A or B
  lies on top of the other and becomes the _upper bulb_. The other bulb becomes
  the _lower bulb_.
  
  The sand drops from the upper bulb to the lower bulb at a rate of 1 gram per
  second. When the upper bulb no longer contains any sand, nothing happens.
  
  Initially at time 0, bulb A is the upper bulb and contains a grams of sand;
  bulb B contains X-a grams of sand (for a total of X grams).
  
  We will turn over the sandglass at time r_1,r_2,..,r_K. Assume that this is an
  instantaneous action and takes no time. Here, time t refer to the time t
  seconds after time 0.
  
  You are given Q queries. Each query is in the form of (t_i,a_i). For each
  query, assume that a=a_i and find the amount of sand that would be contained
  in bulb A at time t_i.
samples: [{"input": "180\n    3\n    60 120 180\n    3\n    30 90\n    61 1\n    180 180", "output": "60\n    1\n    120\n    \n\nIn the first query, 30 out of the initial 90 grams of sand will drop from bulb\nA, resulting in 60 grams. In the second query, the initial 1 gram of sand will\ndrop from bulb A, and nothing will happen for the next 59 seconds. Then, we\nwill turn over the sandglass, and 1 second after this, bulb A contains 1 gram\nof sand at the time in question.\n\n* * *"}, {"input": "100\n    1\n    100000\n    4\n    0 100\n    90 100\n    100 100\n    101 100", "output": "100\n    10\n    0\n    0\n    \n\nIn every query, the upper bulb initially contains 100 grams, and the question\nin time comes before we turn over the sandglass.\n\n* * *"}, {"input": "100\n    5\n    48 141 231 314 425\n    7\n    0 19\n    50 98\n    143 30\n    231 55\n    342 0\n    365 100\n    600 10", "output": "19\n    52\n    91\n    10\n    58\n    42\n    100"}]