#P9854. Pair Sum and Perfect Square

Pair Sum and Perfect Square

Pair Sum and Perfect Square

Problem Description

A permutation of nn elements is an array of nn numbers from 11 to nn such that each number occurs exactly one times in it. Given a permutation pp of nn elements, there are QQ queries to be made. Each query provides two integers LL and R1LRnR (1 ≤ L ≤ R ≤ n), asking how many pairs i,j(i, j) satisfy Li<jRL \leq i < j \leq R and pi+pjp_i + p_j is a square number. A square number is the product of some integer with itself. For example, 99 is a square number, since it can be written as 323^2.

Input

The first line contains an integer TT5T (T\leq 5), representing the number of test cases. For each test case, the input consists of Q+3Q+3 lines: The first line contains an integer n(1n105n (1 \leq n \leq 10^5), representing the length of permutation pp. The second line contains nn integers p1,p2,...,pn1pinp_1, p_2, ..., p_n (1 \leq p_i \leq n), representing the elements of permutation pp. The third line contains an integer Q1Q105Q (1 \leq Q \leq 10^5), indicating the number of queries. The next QQ lines each contain two integers LL and R1LRnR (1 \leq L \leq R \leq n), representing the range of each query.

Output

For each query in each test case, output one line containing an integer, representing the answer.

Sample Input

1
8
5 7 4 1 8 6 2 3
10
4 5
2 6
1 8
2 7
4 8
3 8
4 7
1 5
2 5
3 7

Sample Output

1
1
5
2
3
3
1
2
1
1

Source

2023“钉耙编程”中国大学生算法设计超级联赛(6)