PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000144. Ước chung lớn nhất trên hình chữ nhật (Rectangle GCD)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ước chung lớn nhất trên hình chữ nhật (Rectangle GCD)

Rectangle GCD

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two sequences of positive integers A1,…,ANA_1,\ldots,A_N and B1,…,BNB_1,\ldots,B_N. Consider an N×NN\times N grid. Its cell at row ii, column jj contains Ai+BjA_i+B_j. Both rows and columns are numbered from 11.

Process QQ independent queries. Each specifies a rectangle with top-left cell (h1,w1)(h_1,w_1) and bottom-right cell (h2,w2)(h_2,w_2), including its boundary. For each rectangle, find the greatest common divisor of all the values in it. A rectangle may consist of a single cell, a single row, or a single column. No query modifies the grid.

Input

The first line contains N,QN,Q. The second line lists AiA_i and the third lists BiB_i. Each of the next QQ lines contains h1,h2,w1,w2h_1,h_2,w_1,w_2, where 1≤h1≤h2≤N1\le h_1\le h_2\le N and 1≤w1≤w2≤N1\le w_1\le w_2\le N.

Output

Print QQ lines, each containing the GCD of all entries in the respective queried rectangle.

Subtasks

  • Subtask 1 (20 points): 1≤N,Q≤301 \le N,Q \le 30; 1≤Ai,Bi≤10001 \le A_i,B_i \le 1000.
  • Subtask 2 (30 points): 1≤N,Q≤50001 \le N,Q \le 5000; 1≤Ai,Bi≤1061 \le A_i,B_i \le 10^6.
  • Subtask 3 (50 points): 1≤N,Q≤2⋅1051 \le N,Q \le 2\cdot 10^5; 1≤Ai,Bi≤1091 \le A_i,B_i \le 10^9.

Examples

Input

3 5
3 5 2
8 1 3
1 2 2 3
1 3 1 3
1 1 1 1
2 2 2 2
3 3 1 1

Output

2
1
11
6
10

Explanation

The first rectangle contains 4,6,6,84,6,6,8, whose GCD is 22. The second covers the whole grid and has GCD 11. The last three queries each contain one cell: A1+B1=11A_1+B_1=11, A2+B2=6A_2+B_2=6, and A3+B1=10A_3+B_1=10.