We need to interpret problem: Given array a, operation: choose i != j such that gcd(a_i, a_j) equals the minimum element of the whole array (call m = min(a)). Then we can swap a_i and a_j.
Goal: can we reorder the array via any sequence of swaps (using only allowed swaps) to make it non-decreasing (i.e., sorted ascending)?
Since we can apply swaps any number of times, we need to see which elements can be moved to which positions. The operation allows swapping two elements if their gcd equals the overall min m. This is a swapping condition that defines connectivity in a graph on indices? We can think of allowed swaps as edges between indices i and j if gcd(a_i, a_j) = m. But note that condition may depend on the values at those indices, which can change after swaps. Because swapping changes the values at indices. So the graph is dynamic? However, operations are swaps; we can think of the set of values we have and how they can be permuted. It is a permutation group generated by allowed swaps depending on current values. But the condition gcd(a_i, a_j) = m refers to the current values at i and j. After swapping, values move. So the allowed swaps change over time. This is more complex.
But maybe there is a property: Only elements that are multiples of m can be moved? Let's think.
Let m = min(a). Note that m is fixed as the minimum element of the original array? Actually the operation definition: "If gcd(a_i, a_j) is equal to the minimum element of the whole array a". The phrase "the whole array a" presumably means the current array? Or the original array? Usually such problems mean the current minimum at the moment of operation. Let's read carefully: "If gcd(a_i, a_j) is equal to the minimum element of the whole array a". This could be ambiguous. Usually in Codeforces problems, they define m as the minimum of the original array? But many problems like "Make It Increasing" use the current minimum? Let's check sample: For second sample: a = [4,3,6,6,2,9]. Minimum initially is 2. They swap a_1 (4) and a_3 (6) because gcd(4,6)=2? gcd(4,6)=2, equals min=2, so allowed. Then swap a_1 (now 6 after first swap?) Actually first swap: swap a_1 (4) and a_3 (6) -> array becomes [6,3,4,6,2,9]. Then swap a_1 and a_5: a_1=6, a_5=2, gcd(6,2)=2 which equals min? Current min is now 2? Yes min is still 2. So allowed. After swapping a_1 and a_5 => [2,3,4,6,6,9] which is sorted. So it works.
Now consider fourth sample: a = [7,5,2,2,4]. Minimum is 2. Can we sort? They say NO. Let's reason. Sorted order: [2,2,4,5,7]. We need to bring the two 2's to front. Currently positions: a3=2, a4=2. Actually array: index1=7,2=5,3=2,4=2,5=4. So we have two 2's. Can we swap? We can swap indices if gcd equals 2. Which pairs have gcd=2? Pairs where both are even? But note gcd(7,2)=1 !=2; gcd(5,2)=1; gcd(2,2)=2; gcd(2,4)=2; gcd(7,4)=1; gcd(5,4)=1; etc. So allowed swaps initially: between the two 2's (3 and 4) because gcd(2,2)=2; between 2 and 4 (indices 3 and 5, 4 and5) because gcd(2,4)=2; also maybe between 4 and? gcd(4,?= maybe 4? but need two different indices? Also maybe if we had 6? Not. So we can swap 2's with 4. But 7 and 5 are odd, they have gcd with any other number not 2 unless the other number is multiple of 2? gcd(7,4)=1, gcd(5,4)=1. So there is no direct swap between 7/5 and the 2's? But maybe we can use intermediate swaps: e.g., swap 4 with 2, then swap 4 (now at a3) with 7? But to swap 7 and 4 we need gcd(7,4)=1 which is not 2. So not allowed. Could we rearrange so that 7 ends up in later position? We need final array: [2,2,4,5,7]. So the positions: 1 and 2 should be 2's; 3 should be 4; 4 should be 5; 5 should be 7. The current elements: 7,5,2,2,4. So we need to move 7 and 5 to positions 4 and 5, while moving 2's and 4 to front. But 7 and 5 are odd and not multiples of 2. They can only be swapped with each other if gcd(7,5)=1 not 2; they cannot be swapped directly with any even? But maybe we can bring an even to their position and then swap? For a swap to be allowed, gcd must equal min (which is 2). Since min is 2, condition gcd = 2 means both numbers must be multiples of 2? Actually gcd(x,y)=2 implies both are divisible by 2. More precisely, if gcd(x,y)=2, then both numbers are even and after dividing by 2, they are coprime. So indeed both numbers must be even. So only even numbers can participate in a swap (with another even) that yields gcd exactly 2? But wait, could one number be 2 and another be 6? gcd=2, yes both even. Could one be 2 and another be 2? both even. Could one be 2 and another be 0? no. So the condition gcd=2 forces both numbers to be even (since 2 divides both). Moreover, they must not have a common divisor larger than 2; so they are both multiples of 2 but not both multiples of 4 (or if both multiples of 4, gcd would be at least 4 unless one is 2? Actually if both multiples of 4, gcd would be at least 4 if both are multiples of 4; but if one is 2 and the other 4, gcd(2,4)=2; 2 is not multiple of 4. So it's allowed if at least one is exactly 2 or has only factor 2 not more? Let's formalize: gcd(x,y)=2 means that 2 divides both, but after dividing by 2, the gcd is 1. So essentially x=2a, y=2b with gcd(a,b)=1. So both must be even, and after removing the factor 2, they become coprime.
Thus only even numbers can be involved in allowed swaps (since gcd with an odd number cannot be 2 because 2 does not divide odd numbers). But there is a nuance: What about numbers that are equal to m? If m is odd? Then gcd could be odd. So more generally, condition is gcd = m, where m = minimum. So if m is odd, then odd numbers could be swapped if their gcd equals that odd number. For example, if m=3, then swapping two numbers both multiples of 3, and after dividing by 3, they are coprime. So the condition forces both numbers to be multiples of m (since m divides both). So the elements that are not multiples of m cannot be involved in any swap because for any pair containing a non-multiple of m, gcd cannot be m (since m does not divide that number). Indeed, if x is not divisible by m, then any divisor common to x and y must divide x; but m does not divide x, so gcd(x,y) cannot be m. So only elements that are multiples of m are "swappable" in the sense they can be swapped with some other element that is also a multiple of m under the condition gcd = m. But even among multiples of m, not all pairs are allowed; they need to have exactly gcd = m, not larger. However, perhaps using multiple swaps, any two multiples of m can be swapped indirectly, forming a connected component. Let's explore.
We have a set of elements. The operation is a swap that can be applied to any two positions if the gcd of the current values at those positions equals the current minimum of the whole array. Note that the minimum may change after swaps? If we swap elements, the minimum value of the array could change if we move the minimum element away? But note that m is defined as the minimum element of the whole array a at the time of operation. So it's dynamic; it could change if we move the smallest element? But is it possible to have the minimum increase? If we swap elements, the multiset stays same, so the minimum of the array is always the smallest value present. Since the multiset is invariant, the minimum value among all elements is fixed. Actually the multiset of values is constant (we only swap). So the minimum element's value is constant; the smallest value among all elements never changes because we don't change values. So m is fixed as the minimum value in the initial array (and stays that throughout). So we can treat m as constant: the smallest value in the whole multiset. So condition: gcd(a_i, a_j) = m.
Thus we have a fixed condition: allowed to swap two elements if their values (at that moment) have gcd exactly equal to m. Since values move, the condition on positions changes.
We need to see if we can permute the array to sorted order using such swaps. This is akin to sorting by allowed swaps but where the allowed swaps depend on values.
We can think in terms of the values that are multiples of m. As argued, if a value is not a multiple of m, then for any other value y, gcd(value, y) cannot be m because m does not divide that value. So such an element can never be involved in any swap. Because to swap it with something else, we need gcd = m. Since m does not divide it, impossible. So non-multiples of m are fixed in place? Actually they could be swapped if they are moved indirectly? But to move an element, it must be part of a swap. So if an element cannot be part of any allowed swap, it cannot move from its position at all. Because any move would require it being swapped with another element. So those indices are immobile. Therefore, for sorting to be possible, every element that is not a multiple of m must already be in its correct sorted position (relative to the final sorted array). Because we cannot move them.
Now consider elements that are multiples of m. Among these, can they be arbitrarily permuted among themselves? Possibly yes, if they form a connected graph where swaps can reorder them. However, note that the condition for swapping two multiples of m is that their gcd is exactly m, i.e., after dividing by m, they are coprime. So not every pair of multiples of m is directly swappable. But we might be able to sequence swaps to achieve any permutation among them? Possibly if the set includes a special element: the minimum element itself (which is m) or some other element that can act as a "bridge". Let's think.
We have a multiset of values that are multiples of m. Write each as m * x_i, where x_i is a positive integer. Then condition for swapping two such elements ma and mb is gcd(ma, mb) = m * gcd(a,b) = m => gcd(a,b)=1. So the condition becomes that a and b are coprime.
We want to see if we can rearrange these multiples among themselves arbitrarily via swaps. This is reminiscent of the problem of swapping if numbers are coprime. But note we can also use intermediate positions and the fact that the values are moving. Usually such problems (like Codeforces Round #... ) have a known solution: we can sort the array if and only if the positions of elements that are not multiples of m are already in sorted order (i.e., the array would be sorted after only moving the multiples of m). Additionally, we might need to consider that we can sort the multiples of m arbitrarily because we can use the minimum element (which is m) as a pivot; since gcd(m, any multiple of m) = m? Wait, gcd(m, ma) = m * gcd(1, a) = m. So indeed gcd(m, ma) = m for any a. Because gcd(m, ma) = m * gcd(1, a) = m. So the minimum element (which is exactly m) can be swapped with any other multiple of m directly. Because gcd(m, ma) = m. So the element equal to m (there might be multiple copies) can swap with any other multiple of m. So using m as a "bridge", we can swap any two multiples of m? Let's see: Suppose we have elements X and Y, both multiples of m, but not necessarily directly swappable because gcd(X,Y) might be > m (i.e., they share a factor >1 after dividing by m). But we could use m as an intermediary: swap X with m (requires swapping indices i and j where m is at some position). If we have an m somewhere, we can swap X with that m, moving m to X's original position and X to m's position. Then swap m (now at X's old position) with Y? But careful: after first swap, the m has moved. The positions and values change. We can then swap the m (now at position originally of X) with Y, if gcd(m, Y) = m (which holds). So effectively we can exchange X and Y by a sequence: swap X with m (if m is at some index), then swap m (now at X's old spot) with Y. This results in X moving to m's original spot? Actually we need to see the net effect. Let's say we have positions: pos1 holds X, pos2 holds m, pos3 holds Y. We want to swap X and Y. Steps:
- swap pos1 (X) and pos2 (m): after swap, pos1 has m, pos2 has X.
- swap pos1 (m) and pos3 (Y): after swap, pos1 gets Y, pos3 gets m.
Now positions: pos1 Y, pos2 X, pos3 m. So net effect: X and Y have been swapped, and m ends at pos3 (originally Y). So yes, using m as a temporary, we can swap any two multiples of m. But we need an m element present. Are there always m present? Yes, because m is the minimum value, so at least one element equals m. So there is at least one copy of the minimum element. So we have at least one "pivot" that can directly swap with any multiple of m. Therefore, any permutation among the multiples of m (including the m's themselves) can be achieved by using the m element(s) as swapping hub. However, we must ensure that we can perform these swaps without violating the gcd condition each time. As argued, swapping m with any multiple of m is allowed because gcd = m. So we can move m around. Once we have m at a location, we can use it to swap with other multiples. So indeed, the set of positions that contain multiples of m are fully connected (i.e., any two such positions can have their values swapped via a sequence of allowed swaps). This suggests that we can arbitrarily permute the values that are multiples of m among those positions. However, we need to be careful: when we swap two multiples using m as intermediary, we require that at the time of swapping, the values we are swapping have gcd = m with the other. That holds for m with any multiple. But after the first swap, m moves. Then we swap the m (now at a new position) with Y. That's allowed. So it's fine.
But is there any restriction when we have multiple m's? Even easier. So the multiples of m (call them "good" elements) can be arbitrarily rearranged among the positions that hold good elements. Meanwhile, the elements that are not multiples of m (call them "bad") are stuck in their original positions because they cannot be involved in any swap (since they aren't divisible by m). Let's confirm: Could a bad element ever be moved indirectly? For it to move, it would need to be swapped with some other element. The condition for a swap involving a bad element would require gcd(bad, other) = m. Since m does not divide bad, gcd(bad, other) cannot be m because any common divisor must divide bad, but m does not divide bad. So impossible. So bad elements are fixed.
Thus the only way to sort the array is if, after sorting the whole array, the bad elements are already in their correct positions (i.e., relative ordering of bad elements matches sorted order and they are in the same positions as they would be in the sorted array when we only consider those positions). But more precisely: Since bad elements are immobile, their positions are fixed. So the final sorted array must have those same values at those same positions. So we can check: If we take the array and remove (or ignore) the good elements (multiples of m), and look at the remaining positions, the sequence of values at those positions must be non-decreasing (since in the final sorted array, those positions will contain those values in sorted order). However, we can also reorder the good elements arbitrarily among the good positions (the positions that originally held good elements). So we can fill the good positions with the sorted list of good elements. Then the whole array will be sorted if and only if after placing the sorted good elements into the good positions (in increasing order), the entire array becomes non-decreasing. But since we can permute good elements arbitrarily, we can choose to place them in the good positions in sorted order. So the condition reduces to: if we take the original array, and look at the subsequence of bad elements (non-multiples of m) in the order they appear, they must already be sorted (non-decreasing). Because we cannot move them, so they stay at same indices. In the final sorted array, the values in those indices must be the same as the values that were originally there? Not exactly: The final sorted array will have all values sorted. If we can reorder good elements arbitrarily among good positions, we can achieve a sorted overall array if and only if when we insert the sorted list of good elements into the good positions (which are fixed), the resulting array is sorted. That is equivalent to: if we take the original array and sort only the good elements among themselves (keeping bad elements fixed), the resulting array is sorted. Another way: Sort all elements (including both good and bad) to get target sorted array. Then check: for each index i, if a_i (original) is bad (not divisible by m), then the value at that index in the sorted target must be exactly a_i. Because we cannot change that value. If a_i is good, then the value at that index in the sorted target must be some good value (since we can only place good values in good positions, but we also must place all values; there are exactly the same number of good values as good positions. So the multiset of good values will fill the good positions. So the condition for feasibility is: after sorting the entire array, for every index i where the original a_i is not divisible by m, the sorted target's i-th element must equal a_i. Because we cannot change that element. If original a_i is divisible by m, we can replace it with any good element, so no restriction.
Let's test with samples.
Sample1: n=1, a=[8]. m=8? Actually min is 8. All elements are multiples of m (since 8%8=0). So all are good. So we can reorder arbitrarily (only one element). So answer YES. Output YES.
Sample2: [4,3,6,6,2,9]. m=2. Multiples of 2: 4,6,6,2 (since 4,6,6,2 are even; 3 and 9 are odd so not divisible by 2). So bad: 3 and 9. Their positions: index2=3, index6=9. In sorted array: [2,3,4,6,6,9]. Bad values: 3 at index2? Sorted has index2=3, index6=9. So matches. So condition holds -> YES.
Sample3: [4,5,6,7]. m=4? Actually min is 4? The array is [4,5,6,7] min=4. Multiples of 4: 4 is multiple (4%4=0); 5%4=1 not multiple; 6%4=2 not; 7%4=3 not. So only good: index1=4. Bad: 5,6,7. Sorted target: [4,5,6,7]. Bad positions: index2=5 (original 5), index3=6 (original 6), index4=7 (original7). All match. So YES.
Sample4: [7,5,2,2,4]. m=2. Multiples of 2: 2,2,4 (indices 3,4,5). Bad: 7 and 5 (indices1,2). Sorted target: [2,2,4,5,7]. Bad positions: index1: original 7 vs target 2 -> mismatch (7 != 2). Also index2: original 5 vs target 2? Actually target index2=2, but original 5. So mismatches. So condition fails -> NO. That matches output.
Thus solution appears correct.
But need to be careful: Are there any edge cases where some good elements cannot be moved because they might not be able to swap with m? However we argued m can swap with any multiple of m. But what if the minimum element m is not itself divisible by m? trivial. So yes, m is a multiple of m (since m*1). So it can swap with any other multiple of m. But what about the scenario where there is no m? But there is always at least one m because m is the minimum. So fine.
But wait: Is it always true that if a value is divisible by m, then gcd(m, value) = m? Yes, because gcd(m, m*a) = m * gcd(1, a) = m. So that's allowed.
Thus using m as a pivot, we can swap any two multiples of m. But is it possible that the pivot m is not available at some step because we might have moved it? But we can always bring it back; we just need at least one copy of m somewhere. Since we have at least one m, we can use it repeatedly to facilitate swaps. So the good positions are connected.
Hence the condition is as described.
But let's think about potential pitfalls: What if m is 0? Not possible, a_i positive. Good.
What about if there are multiples of m that are also equal to m? That's fine.
What about if there are multiples of m but we need to move a bad element? Already fixed.
Now, the condition "bad elements must be in correct sorted positions" is equivalent to: if we take the array and sort it, then the set of bad positions (where original a_i not divisible by m) must contain exactly the bad values in sorted order, i.e., the original value at each such position must equal the sorted value at that position. But is it sufficient? Let's test with other possible tricky cases.
Consider array: [3, 2, 4] min=2. Bad: 3 (not divisible by2). Good: 2,4. Sorted: [2,3,4]. Bad position: index1 originally 3 vs sorted index1=2 -> mismatch -> NO. Is it impossible? Let's test manually: array [3,2,4]. We need to get [2,3,4]. Bad element 3 is at index1 and cannot move? But is 3 divisible by 2? No. So 3 cannot be swapped at all. So index1 is stuck with 3. But target index1 should be 2. So impossible -> NO. Correct.
Consider [2, 6, 4] min=2. All are multiples of 2? 2,6,4 all even -> all good. So we can reorder arbitrarily -> YES. Indeed we can sort to [2,4,6]. Using m=2 as pivot: swap 2 (index1) with 6 (index2) allowed? gcd(2,6)=2 -> yes. Then array becomes [6,2,4]. Then swap 6(index1) with 4(index3)? gcd(6,4)=2? Actually gcd(6,4)=2 -> yes. So we can reorder.
Consider [6,10,15] min=6? Actually min is 6? The array [6,10,15] min=6. Multiples of 6: 6 is multiple; 10 not multiple (10%6=4), 15 not. So bad: 10,15. Sorted: [6,10,15]. Bad positions: index2=10 matches, index3=15 matches. So YES. But can we actually achieve? Need to check if 6 can move? 6 is good and can be moved. But we need final order [6,10,15]. Bad elements 10 and 15 cannot move. They are at indices 2 and 3 originally? Actually original indices: 1:6,2:10,3:15. That's already sorted. So it's fine. But suppose original [10,6,15] with min=6. Bad: 10 and 15. Sorted: [6,10,15]. Bad elements at indices 1 and 3: index1=10 (bad) but target index1=6 (good) -> mismatch -> NO. So impossible. Indeed, 10 at index1 cannot move; so can't place 6 there.
Now consider scenario where there are multiple copies of m. That's fine.
What about scenario where some multiples of m are not directly swappable with each other due to gcd > m? But using m we can swap them as argued. So all good.
Now what about scenario where m is not the minimum? It is by definition the minimum value. So fine.
Thus solution: For each test case, compute m = min(a). Then create a sorted version of a, call b = sorted(a). Then iterate over original array a: for each i, if a[i] % m != 0 (i.e., not divisible by m), then we require a[i] == b[i]. If any such mismatch, answer NO; else YES.
But careful: What about numbers that are multiples of m but not divisible by m? Actually multiples of m are numbers where a[i] % m == 0. But what if m=0? Not possible.
Edge: if m=0? Not possible.
Check: Example: a = [2, 3, 6] min=2. Bad: 3. Sorted: [2,3,6]. Bad at index2: a[2]=3 vs b[2]=3 OK -> YES. But can we achieve? Original [2,3,6] is already sorted -> YES.
Another: a = [6,2,3] min=2. Bad: 3? Actually 6%2=0 good, 2%2=0 good, 3%2=1 bad. Sorted: [2,3,6]. Bad position: index3 originally 3 vs b[3]=6? Wait original indices: i1=6, i2=2, i3=3. Sorted b: [2,3,6]. For i=1: a1=6 (good) no check; i=2: a2=2 (good) no check; i=3: a3=3 (bad) must equal b3=6? 3!=6 -> NO. So answer should be NO. Let's test manually: array [6,2,3]. Can we sort to [2,3,6]? 3 is bad cannot move; it's stuck at index3. But target index3 is 6, so impossible. So NO correct.
Now what about elements that are multiples of m but we might need to move a bad element indirectly by swapping with a good? Since bad cannot be swapped at all, it's fixed.
Now consider scenario where some element is not divisible by m but its gcd with some other element might equal m? Could that happen? Suppose m=6, and we have numbers 10 and 15? gcd(10,15)=5 not 6. For gcd to be 6, both numbers must be divisible by 6. Because if d = gcd(x,y) = 6, then 6 divides both x and y. So indeed any element not divisible by m cannot have gcd=m with any other element. So correct.
Therefore algorithm is straightforward.
Complexities: O(n log n) per test case due to sorting. Sum n <= 1e5, so fine.
But wait, check constraints: t up to 1e4, but sum n <= 1e5, so sorting each test case O(n log n) total O(N log N) overall, okay.
Now ensure that the condition "a_i % m != 0" is correct for bad. However, what about the case where m = 0? Not possible.
Edge case: if m = 1. Since 1 divides everything, all numbers are multiples of 1. Then all elements are good. So condition yields always YES? Because we can arbitrarily permute all elements? Let's check: if m=1, condition for swapping two numbers is gcd(a_i, a_j) = 1. That is not always true. Only pairs with gcd 1 can be swapped directly. But we argued that using m=1 as pivot, we can swap any two numbers because gcd(1, any) = 1. So we have at least one 1 in the array? Wait, m=1 means the minimum element is 1. So there is at least one element equal to 1. So we have a 1 element. That 1 can swap with any other element because gcd(1, x)=1. So using 1 as pivot, we can swap any two elements. So indeed all positions are swappable. So we can sort arbitrarily. So YES always when m=1. Our condition: for m=1, all a_i % 1 == 0, so no bad elements, so condition vacuously true -> YES. So matches.
Now consider scenario where m is not 1 but some elements are divisible by m. But is it always true that using m we can swap any two multiples? Suppose we have multiples: ma and mb. To swap them, we need to involve m itself. But if the only copies of m are used as pivot, we need to ensure that we can perform swaps without "losing" the pivot. But it's fine: we can always use one m as a temporary. For example, to swap X and Y (both multiples), we can do: swap X with m (if m is somewhere else), then swap m (now at X's old spot) with Y. This works as long as the m remains present. After the sequence, m ends at Y's old spot. So we still have m somewhere. So it's fine.
What if there is only one copy of m and it is needed for many swaps? Still works because we can reuse it.
Thus algorithm is correct.
But we must also consider if there is any scenario where an element that is divisible by m cannot be swapped because it might not be directly swappable with m? We already argued gcd(m, multiple) = m, so it is directly swappable. So good.
Hence solution: For each test case, compute min. Create sorted copy. Check for each i: if a[i] is not divisible by min, then a[i] must equal sorted[i]. If any violation, output NO, else YES.
But note: The condition a[i] % m != 0 implies not divisible. But careful: what if m=0? Not.
Potential nuance: What if a[i] is divisible by m but is actually less than m? That can't happen because m is the minimum, so any other element >= m. So a[i] >= m. So if divisible, it's at least m and multiple.
Now, we must confirm with more complex examples, maybe where the sorted array could be achieved by moving good elements but the bad elements are not in the correct order but still overall non-decreasing is possible? Let's test: Suppose array: [4,2,3] min=2. Bad: 3 (index3). Good: 4,2. Sorted target: [2,3,4]. Check: index2? original a=[4,2,3]. Sorted b=[2,3,4]. Bad at index3: a[3]=3 vs b[3]=4 -> mismatch -> NO. But is it possible to get a non-decreasing array? Could we produce [2,4,3]? That's not non-decreasing. Maybe [2,3,4]? To get that, need 3 at index2 and 4 at index3. But 3 is stuck at index3. Could we swap 4 and 2? Yes, gcd(4,2)=2, so we can swap indices 1 and 2 to get [2,4,3]. Then can we swap 4 and 3? gcd(4,3)=1 not 2, so cannot. Could we involve 2? Now array [2,4,3], 2 at index1, 4 at index2, 3 at index3. We want to move 3 to index2. But 3 cannot be swapped with 4 because gcd=1. Could we swap 3 with 2? gcd(2,3)=1 not 2. So 3 cannot move. So impossible. So NO correct.
Consider scenario: [6,9,4,10] min=4? Actually min=4? Array: [6,9,4,10] min=4. Bad: numbers not divisible by 4: 6%4=2, 9%4=1 -> both bad. Good: 4,10 (10%4=2? Actually 10%4=2, not divisible by 4? 10/4=2.5, 10 is not multiple of 4. So 10 is not divisible by 4? Since m=4, multiples of 4: numbers divisible by 4: 4 only (and maybe 8,12,...). 6 and 9 and 10 are not divisible by 4. So only good is 4. So only the minimum itself is good. Then all others are bad and cannot move. So the array must already be sorted. Sorted: [4,6,9,10]. Compare original: [6,9,4,10]. Bad positions: index1=6 vs sorted1=4 mismatch -> NO. So cannot sort. Check manually: Since only 4 can move (by swapping with others? But to swap 4 with something, need gcd(4, other)=4. But gcd(4,6)=2, not 4; gcd(4,9)=1; gcd(4,10)=2. So 4 cannot swap with any other element because gcd !=4. So 4 is also immobile? Wait, earlier we said elements divisible by m are good and can be swapped with m. But here m=4, and there is exactly one element equal to m (4). For it to be movable, it needs to swap with another element that is a multiple of m and with gcd = m. But the other elements are not multiples of 4. So gcd(4, other) is either 2 or 1, not 4. So 4 cannot swap with any other element either! Because condition requires gcd = m (4). So even though 4 is divisible by m, it's not necessarily swappable with arbitrary numbers; it's only swappable with numbers that also are multiples of m and have gcd exactly m. So our earlier claim that any multiple of m can swap with m itself is true because gcd(m, ma) = m. But here 4 is m, and the other numbers are not multiples of m (i.e., not ma). So they are not multiples of 4. So gcd(4,6)=2 not 4. So indeed 4 cannot swap with them. So in this case, even the minimum element might be immobile if there are no other multiples of m? But we argued that m can swap with any multiple of m. If there are no other multiples of m, then m cannot swap with anything. But that's okay because if there are no other multiples, then all elements are not multiples, so they are all immobile. So the array is immutable. So our condition must also consider that the minimum element itself might be immobile if it's the only multiple. But that doesn't affect the condition we derived, because in that case we cannot move any element; then the array must already be sorted. In our condition, we treat all elements not divisible by m as bad (immobile). For the minimum element (which is divisible by m), is it always mobile? It depends: if there exists at least one other multiple of m (other than itself), then it can swap. But if it's the only multiple, then it cannot swap either. However, note that if it's the only multiple, then it's still considered "good" by our divisibility condition. But we assumed that good elements can be arbitrarily permuted among good positions, but if there is only one good element, it's trivially in its correct position if the bad ones are in correct positions. Actually, even if it cannot move, we need to ensure it's already in the correct position. Our condition (checking bad positions only) might not be sufficient if the good elements cannot be moved but they are not in the correct sorted order. However, we claimed that good elements can be arbitrarily rearranged because we can use m as pivot. But that relies on having at least two multiples, one of which is m, and the ability to swap m with other multiples. If there is only one multiple (which is m itself), then there are no other multiples, so the set of good elements is just {m}. So there's only one good element, so no need to rearrange. It will be at some index. In the sorted array, that index should contain m. But if there are multiple copies of m? That's multiple good elements? Actually multiple copies of m are multiples of m (since m * 1). So they are also good. So if there are multiple m's, they are all multiples of m. Then we have at least two good elements (if count of m >1). Even if all good elements are exactly m, they are all equal, so swapping among them is trivial. But can we swap two copies of m? gcd(m,m)=m, so yes they can be swapped directly. So even with only m's, they can be permuted among themselves. So the good set connectivity: All good elements are multiples of m. They may not all be directly swappable with each other, but with m as pivot, any two can be swapped provided there is at least one m. But if the only good elements are m themselves, then they can swap directly (since gcd(m,m)=m). So they are all connected.
But what about a case where there is at least one element that is a multiple of m but not equal to m, and there is at least one m (the minimum). Then we have m and another multiple X (X > m). gcd(m, X) = m, so they can swap. So m can move. Then using m we can swap X with any other multiple Y: swap X with m, then m with Y. So all multiples are connected. So good.
But what if there are multiples of m, but there is no element exactly equal to m? That's impossible because m is the minimum value, so at least one element equals m. So there is always at least one m.
Thus the good set is always connected (i.e., any two good elements can be swapped via a sequence) as long as there is at least one good element? Actually if there is at least one m, and we have at least one other multiple (could be another m or a larger multiple), then connectivity holds. If the only good element is a single m (i.e., count of multiples = 1), then it's just a singleton, trivially connected. So good set is connected and we can permute arbitrarily among the good positions? We need to ensure that we can achieve any permutation of good values among good positions. In group theory, if the set of positions containing good values is a set G, and the allowed swaps generate the full symmetric group on G if the graph of allowed swaps is connected (with the ability to perform swaps that exchange any two vertices using a series of allowed swaps). However, we need to check: In our allowed swaps, we can only swap two indices if the values at those indices have gcd = m. But if we want to permute arbitrarily, we need to be able to perform any transposition on the indices of G using allowed swaps that depend on current values. This is more subtle because the allowed swaps depend on the values, which change as we permute. However, we typically can simulate any transposition using a series of swaps with a "placeholder" value (the m). Since m can swap with any good element, we can use m as a temporary to swap any two good elements. This works regardless of the other values: e.g., to swap X and Y (both multiples of m, not necessarily equal to m), we do: swap X (at index i) with m (at index j), then swap m (now at i) with Y (at index k), then optionally swap m (now at k) with something else to bring it back, but that's not needed for the transposition. This sequence is valid because each step involves m and another good element, and gcd(m, any good) = m. So it's allowed. Thus we can realize any transposition between good positions. Therefore the group generated on good positions is the full symmetric group. So we can arbitrarily permute good values among good positions.
Thus condition holds.
Now consider the case where there is only one good element (the min itself). Then we cannot move it because to move it, we would need to swap it with some other element, but that other element is not good (i.e., not divisible by m). Swapping with a bad element would require gcd = m, but gcd(m, bad) cannot be m because bad not divisible by m. So the min is also immobile if it's the only good element. So in that scenario, the entire array is immobile. Our condition currently only checks bad elements for mismatch, but if the min is not at its correct sorted position, we might also have a problem. For example, array: [3,1,2]? Let's test. a=[3,1,2], m=1. Since m=1, all are good (since divisible by 1). So that's not the case. Need m>1 where only one element is divisible by m. For example: a=[3,2,5]? min=2. multiples of 2: only 2. So good set = {2} only one. Bad: 3,5. Sorted: [2,3,5]. Original: [3,2,5]. Check bad positions: index1: 3 (bad) vs sorted[1]=2 -> mismatch -> NO. So our condition would output NO because bad position mismatch. That's correct. Another example: a=[2,3,5] sorted already -> index1 is good? Actually index1=2 (good) so no check; index2=3 (bad) vs sorted[2]=3 ok; index3=5 (bad) vs sorted[3]=5 ok. So YES. Could we actually sort? Since the only good is 2 and it's at index1 already, and bad immobile, array is already sorted. So YES. Good.
What about a scenario where the only good element is the min, and it's not in its correct sorted position, but the bad elements are in correct relative order? Example: a=[3,5,2]? min=2, multiples: only 2. Sorted: [2,3,5]. Check: index1=3 (bad) vs sorted[1]=2 mismatch -> NO. So condition says NO. Indeed, can we sort? Array [3,5,2]. Bad elements 3 and 5 cannot move. 2 is good but can it move? To move 2, need to swap with 3 or 5, but gcd(2,3)=1 not 2, gcd(2,5)=1 not 2, so 2 cannot move either. So array stuck -> cannot become [2,3,5]. So NO. Good.
Now consider a scenario where there are multiple good elements but the min is not necessarily the pivot? Actually we need at least one copy of m to act as pivot. There always is at least one. So connectivity holds.
But consider case where all good elements are multiples of m but none equal to m? That's impossible because m is the minimum, so at least one equals m.
Thus condition is correct.
But wait: There's a nuance: The condition for swapping requires gcd exactly equals m. So for two multiples of m, gcd could be greater than m if they share additional common factor. For example, m=2, X=4, Y=6. gcd(4,6)=2 which equals m (since 4=22, 6=23, gcd=2). That's okay. For X=4, Y=8, gcd=4 > m. So they are not directly swappable. But we can still swap them via m: swap X with m, then m with Y. That's allowed because each step involves m and a multiple, gcd = m. So indirect swap works. So no problem.
Now what about a scenario where we need to swap two good elements but there is only one copy of m and we need to use it as a bridge. That is fine as shown.
Thus algorithm is sound.
Now, note: In the condition we only check positions where a[i] % m != 0. But what about positions where a[i] % m == 0 but maybe they cannot be moved because they are not swappable with anything? For instance, consider m=6, and array [6,12,18] all multiples of 6. There is at least m=6. We can swap 6 with 12, and 6 with 18, etc. So all movable.
But what about m=4, array [8,12,4]? All multiples of 4. m=4 present. gcd(8,12)=4? gcd(8,12)=4, which equals m. So 8 and 12 are directly swappable. Also gcd(4,8)=4, gcd(4,12)=4. So all connected.
So no issue.
Now, let's test a more complex case: m=6, array [6, 12, 18, 10] where 10 is bad. Sorted: [6,10,12,18]. Bad: 10 at index4? Actually original: indices: 1:6,2:12,3:18,4:10. Sorted: [6,10,12,18]. Bad at index4 original 10 vs sorted[4]=18 mismatch -> NO. Could we achieve [6,10,12,18]? Need 10 at index2. But 10 is bad immobile at index4, so impossible. So NO.
What about [10,6,12,18]? min=6. Bad: 10 only? Actually 10%6=4, not divisible. Good: 6,12,18. Sorted: [6,10,12,18]. Bad at index1: 10 vs sorted[1]=6 mismatch -> NO. Indeed 10 stuck at index1, so impossible.
Now what about [12,6,10,18]? Sorted: [6,10,12,18]. Bad at index3? Original: 1:12,2:6,3:10,4:18. Bad: 10 at index3, sorted[3]=12 mismatch -> NO.
Now what about [6,10,18,12]? Bad:10 at index2, sorted[2]=10? Sorted: [6,10,12,18] actually index2=10 ok; index3=18? original index3=18, sorted index3=12 mismatch? Check: original: [6,10,18,12], sorted: [6,10,12,18]. At index4 original 12 vs sorted 18 mismatch? But index4 is good (12 divisible by6). However, we only check bad positions. index4 is good, so we don't enforce equality. But we need to see if we can sort it by moving good elements. Since good elements are 6,18,12. They can be permuted among indices {1,3,4} (since index2 is bad). We need final array [6,10,12,18]. So at index1 should be 6, index3 should be 12, index4 should be 18. The good values we have: 6,18,12. We can assign 6 to index1, 12 to index3, 18 to index4. This is possible because good indices can be permuted arbitrarily. So answer should be YES. Our condition: check bad positions: only index2 (value 10) must equal sorted[2]=10, which holds. So YES. Good.
Now test if it's actually possible: We have array [6,10,18,12]. Need to get 12 at index3 and 18 at index4. We have m=6 at index1. Steps: swap index1 (6) with index4 (12): allowed because gcd(6,12)=6. Array becomes [12,10,18,6]. Then swap index1 (12) with index3 (18): gcd(12,18)=6? gcd=6, yes allowed. Array becomes [18,10,12,6]. Then swap index1 (18) with index4 (6): gcd(18,6)=6 allowed. Array becomes [6,10,12,18]. Sorted. So works.
Thus condition seems sufficient.
Now consider edge case where m is the minimum but there are multiple copies of m, but some multiples are not directly swappable with m? Actually they all are directly swappable because gcd(m, multiple)=m always. So fine.
Thus solution is implemented as described.
Now we must ensure we handle large inputs and sum n <= 100k, so sorting per test case is okay.
Now let's think about potential pitfalls: The condition a[i] % m != 0: In languages, be careful with negative? Not relevant.
But what if m = 0? Not.
Now, I recall there is a known Codeforces problem: "GCD Arrays" maybe? Actually this is problem 1629B? Not sure. The described logic matches.
But is there any scenario where an element is divisible by m but cannot be moved because it might be the only element that is a multiple of m and also not equal to m? Actually if it's the only multiple besides m? Wait if there is only one multiple besides m, then we have at least two good elements: m and X. They can swap directly (since gcd(m,X)=m). So both can move. So yes they are movable.
But consider case where there is no m? Not possible.
Thus solution is correct.
Now I will produce solution explanation.
Implementation steps for each test case:
- Read n.
- Read array a.
- Compute m = min(a).
- Create sorted copy b = sorted(a).
- For i in 0..n-1:
if a[i] % m != 0 and a[i] != b[i]: print NO and go to next test case.
- If loop finishes, print YES.
But caution: The condition a[i] % m != 0 means not divisible. But what about elements that are divisible by m but could be immobile because they are the only good? As argued, if there is at least one other good (including m itself) they can move; if only one good, it's just one element, so no need to move. But is it possible that a good element (multiple of m) is immobile even if there are other good elements? Possibly if the graph of allowed swaps among good elements is not connected? But we argued connectivity using m as hub: Since m can swap with any other good, and any two goods can be swapped via m. This relies on the ability to swap m with any good. But what if m itself is not directly swappable with some good because gcd(m, that good) might not be m? But as argued, gcd(m, m*a) = m * gcd(1, a) = m. So yes, it's always m. So m can swap with any multiple of m. So connectivity is assured. However, there is a nuance: The swapping operation requires that we choose two different indices i and j. It does not require that we have a dedicated "placeholder" that stays as m after swapping. But as we perform swaps, the m moves. That's fine, we can still use it. So the good set is connected and we can generate any permutation.
But is there any restriction that we cannot swap m with a good if m is not at a specific location? We can always locate the index where m currently resides (it may move). Since m can swap with any good, we can bring m to any good position we want by swapping it with the element at that position. So connectivity holds.
Thus condition is correct.
One more nuance: The condition a[i] % m != 0 checks divisibility. However, what about the case where m = 0? No. Also note: a[i] may be large, but mod is fine.
Now, double-check sample 4 with our algorithm: m=2, a=[7,5,2,2,4], sorted b=[2,2,4,5,7]. Check indices: 0:7%2=1 -> 7 !=2 -> mismatch => NO. Good.
Thus answer is correct.
Now let's test a few custom cases:
Case: n=2, a=[2,3], m=2. Bad: 3%2=1, check a[1]=3 vs b[1]=? sorted=[2,3]; index1: a[1]=3 vs b[1]=3 ok? Actually indices: 0:2 (good), 1:3(bad) => b[1]=3 matches => YES. Is it possible? Array already non-decreasing, so YES.
Case: a=[3,2], m=2. sorted=[2,3]. Check index0:3%2=1 => 3 !=2 => NO. Indeed cannot sort because 3 cannot move, 2 can move? 2 can swap with 3? gcd(2,3)=1 not 2, so cannot swap. So array cannot be sorted. Good.
Case: a=[4,2,6,3] m=2? min=2. a=[4,2,6,3]. sorted=[2,3,4,6]. Bad: elements not divisible by 2: 3 only at index3. Check index3: 3 vs sorted[3]=6 mismatch -> NO. But wait, can we sort? We have good: 4,2,6. Need final: index0=2, index1=3, index2=4, index3=6. Bad 3 is at index3, but needs to be at index1. So impossible. NO.
Case: a=[4,3,2,6] m=2, sorted=[2,3,4,6]. Bad: 3 at index1? Actually a: index0=4(good),1=3(bad),2=2(good),3=6(good). Check index1: 3 vs sorted[1]=3