/* 
   AOJ 1148
   Analyzing Login/Logout Records
   2015/9/7
*/

#include<stdio.h>
#define REC_MAX 1000

struct record
{
  int time, pc_id, stu_id, log_flg;
};

struct record records[REC_MAX];

int ans_f(int start_t, int end_t, int questioned_stu_id, int records_n)
{
  int i, login_pc_count = 0, start = 0, timer = 0;

  for(i = 0; i < records_n; i++)
    {
      if(records[i].stu_id == questioned_stu_id)
	{
	  if(records[i].log_flg)
	    {
	      /* login */
	      login_pc_count++;
	      if(login_pc_count == 1)
		{
		  /* first login */
		  if(records[i].time < start_t)
		    {
		      /* login before start_t */
		    }
		  else if(records[i].time >= start_t && records[i].time <= end_t)
		    {
		      /* login after start_t && before end_t*/
		      start = records[i].time;
		    }
		  else
		    {
		      /* login after end_t */
		      return timer;
		    }
		}
	    }
	  else
	    {
	      /* logout */
	      login_pc_count--;
	      if(login_pc_count == 0)
		{
		  /* last logout */
		  if(records[i].time < start_t)
		    {
		      /* logout before start_t */
		    }
		  else if(records[i].time >= start_t && records[i].time <= end_t)
		    {
		      /* logout after start_t && before end_t */
		      if(start == 0)
			{
			  /* first login before start_t*/
			  timer += records[i].time - start_t;
			}
		      else
			{
			  /* first login after start_t */
			  timer += records[i].time - start;
			}
		    }
		  else
		    {
		      /* logout after end_t */
		      if(start == 0)
			{
			  /* first login before start_t */
			  timer += end_t - start_t;
			}
		      else
			{
			  /* first login after start_t */
			  timer += end_t - start;
			}
		      return timer;
		    }
		}
	    }
	}
    }
  return timer;
}

int main(void)
{
  int i, pcs_n, stus_n, records_n, questions_n, start_t, end_t, questioned_stu_id, ans = 0;

  while(1)
    {
      scanf("%d %d", &pcs_n, &stus_n);

      if(pcs_n == 0 && stus_n == 0)
	{
	  break;
	}

      scanf("%d", &records_n);
      for(i = 0; i < records_n; i++)
	{
	  scanf("%d %d %d %d", &records[i].time, &records[i].pc_id, &records[i].stu_id, &records[i].log_flg);
	}
      scanf("%d", &questions_n);
      for(i = 0; i < questions_n; i++)
	{
	  scanf("%d %d %d", &start_t, &end_t, &questioned_stu_id);
	  ans = ans_f(start_t, end_t, questioned_stu_id, records_n);
	  printf("%d\n", ans);
	}
    }
  return 0;
}