output_description: For each query, print a line having a decimal integer indicating the time of
  usage in minutes. Output lines should not have any character other than this
  number.
input_description: The input is a sequence of a number of datasets. The end of the input is
  indicated by a line containing two zeros separated by a space. The number of
  datasets never exceeds 10.
  
  Each dataset is formatted as follows.
  
  > _N_  _M_  
  >  _r_  
  >  record1  
  >  ...  
  >  record _r_  
  >  _q_  
  >  query1  
  >  ...  
  >  query _q_  
  >
  
  The numbers _N_   and _M_   in the first line are the numbers of PCs and the
  students, respectively. _r_   is the number of records. _q_   is the number of
  queries. These four are integers satisfying the following.
  
  > 1 ≤ _N_   ≤ 1000, 1 ≤ _M_   ≤ 10000, 2 ≤ _r_   ≤ 1000, 1 ≤ _q_   ≤ 50
  
  Each record consists of four integers, delimited by a space, as follows.
  
  > _t_   _n_   _m_   _s_  
  
  _s_   is 0 or 1. If _s_   is 1, this line means that the student _m_   logged
  in to the PC _n_   at time _t_  . If _s_   is 0, it means that the student _m_
  logged out of the PC _n_   at time _t_  . The time is expressed as elapsed
  minutes from 0:00 of the day. _t_  , _n_   and _m_   satisfy the following.
  
  > 540 ≤ _t_   ≤ 1260, 1 ≤ _n_   ≤ _N_  , 1 ≤ _m_   ≤ _M_  
  
  You may assume the following about the records.
  
  * Records are stored in ascending order of time _t._  
  * No two records for the same PC has the same time _t._  
  * No PCs are being logged in before the time of the first record nor after that of the last record in the file. 
  * Login and logout records for one PC appear alternatingly, and each of the login-logout record pairs is for the same student. 
  
  Each query consists of three integers delimited by a space, as follows.
  
  > _t s_  _t e_  _m_  
  
  It represents "Usage of the student _m_   between _t s_  and _t e_ ". _t s_ ,
  _t e_  and _m_   satisfy the following.
  
  > 540 ≤ _t s_  < _t e_  ≤ 1260, 1 ≤ _m_   ≤ _M_
attempt: while True:
      N, M = [int(s) for s in input().strip().split()]
  
      if N == 0 and M == 0:
          break
  
      r = int(input().strip())
  
      logs = []
      for _ in range(r):
          t, n, m, s = [int(s) for s in input().strip().split()]
          logs.append((t, n, m, s))
  
      q = int(input().strip())
      for _ in range(q):
          ts, te, m = [int(s) for s in input().strip().split()]
  
          selected_logs = [log for log in logs if log[2] == m]
  
          used_time = 0
          login_time = 2000
          is_login_list = [False] * N
          for log in selected_logs:
              lt, ln, lm, ls = log
              ln -= 1
  
              # ログアウト
              if ls == 0:
                  # 観測開始前
                  if lt < ts:
                      continue
  
                  # 観測終了前
                  if lt < te:
                      # 他のPCにログインしていたらそのPCのログアウトまで待つ
                      other_pc_login = False
                      for i, is_login in enumerate(is_login_list):
                          if i == ln:
                              continue
                          if is_login:
                              other_pc_login = True
                              break
                      if other_pc_login:
                          is_login_list[ln] = False
                          continue
  
                      # ログイン時刻が観測開始前
                      if login_time < ts:
                          used_time += lt - ts
                      # ログイン時刻が観測開始後
                      else:
                          used_time += lt - login_time
  
                      # ログイン状態を解除
                      is_login_list[ln] = False
                      continue
                  # 観測終了後
                  else:
                      if login_time < ts:
                          used_time += te - ts
                      else:
                          used_time += te - login_time
                      break
  
              # ログイン
              else:
                  if lt > te:
                      break
  
                  is_login_list[ln] = True
  
                  other_pc_login = False
                  for i, is_login in enumerate(is_login_list):
                      if i == ln:
                          continue
                      if is_login:
                          other_pc_login = True
                          break
  
                  if not other_pc_login:
                      login_time = lt
  
          print(used_time)
  
problem_description: B: Analyzing Login/Logout Records
  
  You have a computer literacy course in your university. In the computer
  system, the login/logout records of all PCs in a day are stored in a file.
  Although students may use two or more PCs at a time, no one can log in to a PC
  which has been logged in by someone who has not logged out of that PC yet.
  
  You are asked to write a program that calculates the total time of a student
  that he/she used at least one PC in a given time period (probably in a
  laboratory class) based on the records in the file.
  
  The following are example login/logout records.
  
    * The student 1 logged in to the PC 1 at 12:55 
    * The student 2 logged in to the PC 4 at 13:00 
    * The student 1 logged in to the PC 2 at 13:10 
    * The student 1 logged out of the PC 2 at 13:20 
    * The student 1 logged in to the PC 3 at 13:30 
    * The student 1 logged out of the PC 1 at 13:40 
    * The student 1 logged out of the PC 3 at 13:45 
    * The student 1 logged in to the PC 1 at 14:20 
    * The student 2 logged out of the PC 4 at 14:30 
    * The student 1 logged out of the PC 1 at 14:40 
  
  For a query such as "Give usage of the student 1 between 13:00 and 14:30",
  your program should answer "55 minutes", that is, the sum of 45 minutes from
  13:00 to 13:45 and 10 minutes from 14:20 to 14:30, as depicted in the
  following figure.
  
  ![](https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_b)
samples: [{"input": "2\n    10\n    775 1 1 1\n    780 4 2 1\n    790 2 1 1\n    800 2 1 0\n    810 3 1 1\n    820 1 1 0\n    825 3 1 0\n    860 1 1 1\n    870 4 2 0\n    880 1 1 0\n    1\n    780 870 1\n    13 15\n    12\n    540 12 13 1\n    600 12 13 0\n    650 13 15 1\n    660 12 15 1\n    665 11 13 1\n    670 13 15 0\n    675 11 13 0\n    680 12 15 0\n    1000 11 14 1\n    1060 12 14 1\n    1060 11 14 0\n    1080 12 14 0\n    3\n    540 700 13\n    600 1000 15\n    1000 1200 11\n    1 1\n    2\n    600 1 1 1\n    700 1 1 0\n    5\n    540 600 1\n    550 650 1\n    610 620 1\n    650 750 1\n    700 800 1\n    0 0", "output": "70\n    30\n    0\n    0\n    50\n    10\n    50\n    0"}]