

‘X’ % ‘Z’ == 0
‘Y’ % ‘Z’ == 0
‘Z’ >= ‘THRESHOLDVALUE’
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.
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.
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.
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:-
We will iterate over all the possible pairs of nodes and check if they are directly connected. We will build the graph using the disjoint set union. If two nodes in the query are part of the same root insert ‘1’ otherwise ‘0’ in the vector/list ‘ans’.
We will apply the algorithm as follows:-