Lucky

Accepts: 34
Submissions: 267
Time Limit: 6000/3000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description
WLD is always very lucky.His secret is a lucky number $K$.$k$ is a fixed odd number. Now he meets a stranger with $N$ numbers:$a1,a2,...,aN$.The stranger asks him $M$ questions.Each question is like this:Given two ranges $[Li,Ri]$ and $[Ui,Vi]$,you can choose two numbers $X$ and $Y$ to make $aX+aY=K$.The $X$ you can choose is between $Li$ and $Ri$ and the $Y$ you can choose is between $Ui$ and $Vi$.How many pairs of numbers$(X,Y)$ you can choose? If WLD can answer all the questions correctly,he'll be the luckiest man in the world.Can you help him?
Input
There are multiple cases.(At MOST $5$) For each case: The first line contains an integer $N(1 \leq N \leq 30000)$. The following line contains an integer $K(2 \leq K \leq 2*N)$,WLD's lucky number.K is odd. The following line contains $N$ integers $a1,a2,...,aN(1 \leq ai \leq N)$. The following line contains an integer $M(1 \leq M \leq 30000)$,the sum of the questions WLD has to answer. The following $M$ lines,the i-th line contains $4$ numbers $Li,Ri,Ui,Vi(1 \leq Li \leq Ri < Ui \leq Vi \leq N)$,describing the i-th question the stranger asks.
Output
For each case: Print the total of pairs WLD can choose for each question.
Sample Input
5
3 
1 2 1 2 3
1 
1 2 3 5
Sample Output
2
Hint
a1+a4=a2+a3=3=K. So we have two pairs of numbers (1,4) and (2,3). Good luck!