#SGM0000067. Truy vấn k trực tuyến (K-Query Online)

Truy vấn k trực tuyến (K-Query Online)

K-Query Online

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem

Queries are online. Let last be the previous answer, initially 00. Decode i=x⊕lasti=x\oplus last, j=y⊕lastj=y\oplus last, k=z⊕lastk=z\oplus last. Clamp ii to at least 11 and jj to at most nn. If i>ji>j, the answer is 00; otherwise count values greater than kk in ai..aja_i..a_j. Set last to that answer.

Input

The first line contains nn, the second line contains the array, the third line contains qq, and each following line contains encoded values x,y,zx,y,z.

Output

Print each decoded query answer in order.

Subtasks

Subtask 1 (20%)

  • n≤30n\le 30, number of queries ≤30\le 30.
  • All other conditions are the same as Subtask 3.

Subtask 2 (30%)

  • n≤3000n\le 3000, number of queries ≤3000\le 3000.
  • All other conditions are the same as Subtask 3.

Subtask 3 (50%)

  • 1≤n≤300001\le n\le30000
  • 1≤q≤2000001\le q\le200000
  • 1≤ai≤1091\le a_i\le10^9

Example

Input

6
8 9 3 5 1 9
5
2 3 5
3 3 7
0 0 11
0 0 2
3 7 4

Output

1
1
0
0
2

Explanation

Every query depends on the previous result, so queries cannot be reordered. The sample answers are 1,1,0,0,2.