

You have been given a graph consisting of ‘N’ nodes and a threshold value ‘THRESHOLDVALUE’. Two different nodes ‘X’ and ‘Y’ are directly connected to each other if and only if there exists a ‘Z’ such that all of the following are true:-
‘X’ % ‘Z’ == 0
‘Y’ % ‘Z’ == 0
‘Z’ >= ‘THRESHOLDVALUE’
You are given ‘Q’ queries where each query consists of two distinct nodes ‘U’ and ‘V’, you must determine if ‘U’ and ‘V’ are connected directly or indirectly. Return a vector/list of size ‘Q’ where ‘i-th’ element is ‘1’ if nodes in ith query are connected directly or indirectly otherwise it is ‘0’.
Example:Let’s say ‘N’ is 6 and 'M' is ‘2’. Then our graph will look as follows:-

There is an edge between ‘2’ and ‘4’, ‘4’ and ‘6’ and ‘2’ and ‘6’ because their gcd is 2 which is equal to ‘m’. There is an edge between ‘3’ and ‘6’ because their gcd is 3 which is greater than ‘m’. If the query consists of vertices ‘2’ and ‘3’ answer will be ‘1’ because they are indirectly connected.
The first line contains a single integer ‘t’ representing the number of test cases.
The first line of each test case contains two space-separated integers ‘n’ and ‘THRESHOLDVALUE’ representing the number of nodes and the threshold value.
The second line contains a single integer ‘q’ representing the number of queries.
Each of the next ‘q’ lines contains two space-separated integers representing the vertices for which you need to find if they are connected directly or indirectly.
Output Format:
For each test case, print a single line containing space-separated integers denoting answers to all the queries.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 100
1 <= THRESHOLDVALUE <= 100
1 <= Q <= 10000
1 <= U[i] <= N
1 <= V[i] <= N
Where ‘T’ is the number of test cases.‘N’ is the number of nodes in the graph. ‘THRESHOLDVALUE’ is the threshold value. ‘Q’ is the number of queries. ‘U[i]’ and ‘V[i]’ are vertices of the i-th query.
Time Limit: 1 sec.
2
6 2
3
1 2
2 3
3 4
6 3
2
2 3
3 6
0 1 1
0 1
Test case 1:
The graph looks as follows:-

Nodes 1 and 2 are not connected directly or indirectly. Therefore answer to this query is 0. Nodes 2 and 3 are connected indirectly. Therefore answer to this query is 1. Nodes 3 and 4 are connected indirectly. Therefore answer to this query is 1.
Therefore the answer is [0,1,1].
Test case 2:
The graph looks as follows:-

Nodes 2 and 3 are not connected directly or indirectly. Therefore answer to this query is 0. Nodes 3 and 6 are connected directly. Therefore answer to this query is 1.
Therefore the answer is [0,1].
2
4 1
3
1 2
2 3
3 4
4 4
2
1 2
2 3
1 1 1
0 0
Iterate over all possible pairs of vertices.
We will iterate over all the possible pairs of nodes and check if they are directly connected. We will build the graph and using a breadth-first search we can find all the connected components. If two nodes in the query are part of the same component insert ‘1’ otherwise ‘0’ in the vector/list ‘ans’.
We will apply the algorithm as follows:-
O(N * N * log(max(X, Y)’), where ‘N’ denotes the number of nodes in the graph and X and Y denotes the value of nodes.
We are calculating the greatest common divisor for each pair of nodes and checking if they are directly connected by an edge.
O(N * N), where ‘N’ denotes the number of nodes in the graph.
We build an adjacency list where ‘i-th’ vector/list can be of size ‘N’.