You are given an array $a_1, a_2, \dots, a_n$ where all $a_i$ are integers and greater than $0$.

 In one operation, you can choose two different indices $i$ and $j$ ($1 \le i, j \le n$). If $gcd(a_i, a_j)$ is equal to the minimum element of the whole array $a$, you can swap $a_i$ and $a_j$. $gcd(x, y)$ denotes the greatest common divisor (GCD) of integers $x$ and $y$.

 Now you'd like to make $a$ non-decreasing using the operation any number of times (possibly zero). Determine if you can do this.

 An array $a$ is non-decreasing if and only if $a_1 \le a_2 \le \ldots \le a_n$.


-----Input-----

 The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

 The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of array $a$.

 The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots a_n$ ($1 \le a_i \le 10^9$) — the array itself.

 It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.


-----Output-----

 For each test case, output "YES" if it is possible to make the array $a$ non-decreasing using the described operation, or "NO" if it is impossible to do so.


-----Example-----
Input
4
1
8
6
4 3 6 6 2 9
4
4 5 6 7
5
7 5 2 2 4

Output
YES
YES
YES
NO



-----Note-----

 In the first and third sample, the array is already non-decreasing.

 In the second sample, we can swap $a_1$ and $a_3$ first, and swap $a_1$ and $a_5$ second to make the array non-decreasing.

 In the forth sample, we cannot the array non-decreasing using the operation.

Source: codeforces | Difficulty: EASY
Tags: Number theory

Sorting

Mathematics

Constructive algorithms


Solutions:
from sys import stdin
import math
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	a = [int(x) for x in inp().split()]
	mini = min(a)
	sA = sorted(a)
	for i in range(n):
		if a[i] != sA[i]:
			if a[i] % mini != 0:
				print('NO')
				break
	else:
		print('YES')


t = int(input())
for i in range(t):
	z = True
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	c = b[0]
	if a == b:
		print('YES')
	else:
		for j in range(n):
			if a[j] != b[j]:
				if a[j] % c != 0:
					print('NO')
					z = False
					break
		if z:
			print('YES')


q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	b = a.copy()
	b.sort()
	odp = 'YES'
	for i in range(n):
		if a[i] % m != 0:
			if a[i] != b[i]:
				odp = 'NO'
	print(odp)


def check(arrayList1, arrayList2):
	f = 0
	minList1 = min(arrayList1)
	arrayList2.sort()
	for item in range(0, len(arrayList1)):
		if arrayList1[item] != arrayList2[item] and arrayList1[item] % minList1:
			f = 1
	if f:
		print('NO')
	else:
		print('YES')
t = int(input())
for i in range(0, t):
	n = int(input())
	lists1 = list((int(num) for num in input().strip().split()))[:n]
	lists2 = lists1.copy()
	check(lists1, lists2)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	k = sorted(l)
	c = min(l)
	f = 0
	for j in range(n):
		if l[j] % c != 0:
			if l[j] != k[j]:
				print('NO')
				f = 1
				break
	if f == 0:
		print('YES')


def solve(a, b):
	m = min(a)
	for i in range(len(a)):
		if a[i] != b[i] and a[i] % m:
			return False
	return True
for z in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	if solve(a, b):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	x = min(arr)
	temp = []
	temp.extend(arr)
	temp.sort()
	flag = 0
	for i in range(n):
		if arr[i] != temp[i]:
			if arr[i] % x == 0:
				continue
			flag = 1
			break
	if flag == 0:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	x = min(a)
	f = True
	for i in range(n):
		if a[i] != b[i] and a[i] % x:
			f = False
	if f == False:
		print('NO')
	else:
		print('YES')


for q in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	b = sorted(a)
	r = 'YES'
	for i in range(n):
		if a[i] != b[i] and a[i] % m != 0:
			r = 'NO'
			break
	print(r)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = min(a)
	flag = 'YES'
	arr = sorted(a)
	for i in range(n):
		if a[i] % s and a[i] != arr[i]:
			flag = 'NO'
			break
	print(flag)


from sys import stdin, stdout
from collections import Counter, deque
import math
cin = stdin.readline
cout = stdout.write

def inpn():
	return int(cin())

def inpl():
	return list(map(int, cin().split()))

def inps():
	return cin()

def invr():
	return map(int, cin().split())

def outs(s):
	cout(s)

def outn(n):
	cout(str(n))

def outl(l):
	for i in l:
		cout(str(i) + ' ')

def outb(s):
	cout(str(s))
for _ in range(inpn()):
	n = inpn()
	l = inpl()
	l1 = sorted(l)
	if l == l1:
		outs('YES')
		cout('\n')
	else:
		m = min(l)
		for (i, j) in enumerate(l):
			if j % m != 0:
				if l[i] != l1[i]:
					outs('NO')
					cout('\n')
					break
		else:
			outs('YES')
			cout('\n')


import math
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	x = []
	for i in a:
		x.append(i)
	x.sort()
	mini = x[0]
	q = 0
	for i in range(n):
		if x[i] != a[i] and a[i] % mini > 0:
			q = 1
			break
	if q == 0:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sa = sorted(a)
	mino = min(a)
	if n == 1:
		print('YES')
	elif sa == a:
		print('YES')
	else:
		an = False
		for i in range(n):
			if a[i] != sa[i]:
				if a[i] % mino != 0:
					an = True
					print('NO')
					break
		if an == False:
			print('YES')


import math
test_case = int(input())
while test_case:
	test_case -= 1
	myara = []
	num = int(input())
	myara.extend(map(int, input().split()))
	yourara = sorted(myara)
	flag = True
	for x in range(num):
		if not (myara[x] == yourara[x] or myara[x] % yourara[0] == 0):
			flag = False
			break
	if flag == False:
		print('NO')
	else:
		print('YES')


import math
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sa = sorted(a)
	mino = min(a)
	if n == 1:
		print('YES')
	elif sa == a:
		print('YES')
	else:
		an = False
		l = []
		for i in range(n):
			if a[i] != sa[i]:
				l.append(a[i])
		for i in range(len(l)):
			if l[i] % mino != 0:
				an = True
				print('NO')
				break
		if an == False:
			print('YES')


import math
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(a)
	b.sort()
	c = []
	for i in range(0, len(a)):
		if a[i] != b[i]:
			c.append(a[i])
	if len(c) == 0:
		print('YES')
	else:
		x = min(a)
		p = 0
		for i in c:
			if i % x != 0:
				p = 1
				break
		if p == 1:
			print('NO')
		else:
			print('YES')


import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
from collections import deque
from functools import cmp_to_key
import math

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	for _ in range(iin()):
		n = iin()
		l = ain()
		x = sorted(l)
		if x == l:
			print('YES')
		else:
			mini = x[0]
			res = x[0]
			flag = 0
			for i in range(n):
				if x[i] != l[i]:
					if res != math.gcd(res, l[i]):
						flag = 1
						break
			if flag == 1:
				print('NO')
			else:
				print('YES')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
from sys import stdin, stdout
import math
import time
from functools import lru_cache
from collections import Counter
import heapq
try:
	for _ in range(int(input())):
		n = int(input())
		arr = [int(i) for i in input().split()]
		arr2 = arr.copy()
		arr2.sort()
		if arr2 == arr:
			print('YES')
			continue
		else:
			flg = True
			x = arr2[0]
			for i in range(n):
				if arr[i] % x == 0:
					continue
				if arr[i] == arr2[i] and flg:
					flg = True
				else:
					flg = False
			if flg:
				print('YES')
			else:
				print('NO')
except EOFError as e:
	print(e)


for s in [*open(0)][2::2]:
	a = [*map(int, s.split())]
	mn = min(a)
	b = sorted(a)
	for (x, y) in zip(a, b):
		if x != y and x % mn != 0:
			print('NO')
			break
	else:
		print('YES')


import sys
import os
import math
import copy
from math import gcd
from bisect import bisect
from io import BytesIO, IOBase
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from itertools import accumulate

def Int():
	return int(sys.stdin.readline())

def Mint():
	return map(int, sys.stdin.readline().split())

def Lstr():
	return list(sys.stdin.readline().strip())

def Str():
	return sys.stdin.readline().strip()

def Mstr():
	return map(str, sys.stdin.readline().strip().split())

def List():
	return list(map(int, sys.stdin.readline().split()))

def Hash():
	return dict()

def Mod():
	return 1000000007

def Ncr(n, r, p):
	return fact[n] * (ifact[r] * ifact[n - r] % p) % p

def Most_frequent(list):
	return max(set(list), key=list.count)

def Mat2x2(n):
	return [List() for _ in range(n)]

def Lcm(x, y):
	return x * y // gcd(x, y)

def dtob(n):
	return bin(n).replace('0b', '')

def btod(n):
	return int(n, 2)

def common(l1, l2):
	return set(l1).intersection(l2)

def solve(a):
	b = sorted(a)
	for i in range(len(a)):
		if a[i] == b[i] or a[i] % b[0] == 0:
			pass
		else:
			return 'NO'
	return 'YES'

def solution():
	for _ in range(Int()):
		n = Int()
		a = List()
		print(solve(a))
solution()


from sys import stdin, stdout
from bisect import bisect_right, bisect_left
from math import gcd
n = int(stdin.readline())
for i in range(n):
	r = False
	a = int(stdin.readline())
	lst = [int(i) for i in stdin.readline().split()]
	lst2 = sorted(lst)
	k = min(lst2)
	for i in range(len(lst)):
		if lst[i] != lst2[i]:
			if gcd(lst[i], k) != k:
				stdout.write('NO' + '\n')
				r = True
				break
	if r:
		continue
	stdout.write('YES' + '\n')


import math

def is_dec(a):
	for i in range(1, n):
		if a[i] < a[i - 1]:
			return True
	return False
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	if not is_dec(a):
		print('YES')
	else:
		mn = min(a)
		sorteda = sorted(a)
		wrong = []
		for i in range(n):
			if a[i] != sorteda[i]:
				wrong.append(a[i])
		ans = ''
		for i in range(len(wrong)):
			if math.gcd(wrong[i], mn) != mn:
				ans = 'NO'
				break
		print(ans if ans else 'YES')


from math import *
from collections import *
from itertools import *
from functools import *
from bisect import *
from heapq import *
from operator import *
from sys import *
setrecursionlimit(100000000)
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	tmp = l.copy()
	tmp.sort()
	ans = 1
	for i in range(len(l)):
		if tmp[i] != l[i] and l[i] % tmp[0] != 0:
			print('NO')
			break
	else:
		print('YES')


def gcd(a, b):
	return a if b == 0 else gcd(b, a % b)

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	mn = min(a)
	b = [a[i] for i in range(n)]
	b.sort()
	ok = True
	for i in range(n):
		if a[i] != b[i] and a[i] % mn != 0:
			ok = False
			break
	print('YES' if ok else 'NO')
t = int(input())
while t > 0:
	solve()
	t -= 1


import sys, math, heapq, copy
from collections import defaultdict, deque
from bisect import bisect_left, bisect_right
from functools import cmp_to_key
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = l[:]
	x.sort()
	m = x[0]
	flag = 0
	for i in range(n):
		if l[i] != x[i]:
			if l[i] % m != 0:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	b.sort()
	m = min(a)
	flag = True
	for i in range(n):
		if a[i] != b[i] and a[i] % m > 0:
			flag = False
	if flag:
		print('YES')
	else:
		print('NO')


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	minm = min(l)
	if len(l) == 1 or l == sorted(l):
		print('YES')
	else:
		temp = sorted(l)
		flag = 0
		for i in range(n):
			if l[i] != temp[i] and l[i] % minm != 0:
				flag = 1
				break
		if flag == 0:
			print('YES')
		else:
			print('NO')


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = []
	for i in range(n):
		b.append(a[i])
	mini = min(a)
	a.sort()
	counter = 0
	for i in range(n):
		if a[i] != b[i] and a[i] % mini != 0:
			counter = 1
			break
	if counter == 1:
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	y = sorted(l)
	x = min(l)
	a = []
	b = []
	for i in range(n):
		if l[i] % x == 0:
			a.append(l[i])
			b.append(i)
	a.sort()
	for i in range(len(b)):
		l[b[i]] = a[i]
	if l == y:
		print('YES')
	else:
		print('NO')


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
t = ip()
for _ in range(t):
	n = ip()
	arr = list(inp())
	m = min(arr)
	pos = []
	ele = []
	for i in range(n):
		if gcd(m, arr[i]) == m:
			pos.append(i)
			ele.append(arr[i])
	ele.sort()
	for i in range(len(pos)):
		arr[pos[i]] = ele[i]
	if sorted(list(arr)) == arr:
		print('YES')
	else:
		print('NO')


import math
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))

	def gcd(a, b):
		if b == 0:
			return a
		else:
			return gcd(b, a % b)
	i = 0
	m = min(s)
	b = 0
	s1 = s.copy()
	s1.sort()
	c = 0
	d = 0
	se = set()
	if s == s1:
		c = 1
		print('YES')
	else:
		k = 0
		for i in range(n):
			if s1[i] != s[i] and s[i] % m > 0:
				k = 1
		if k == 1:
			print('NO')
		else:
			print('YES')


for s in [*open(0)][2::2]:
	a = (*map(int, s.split()),)
	b = sorted(a)
	print('YNEOS'[any((y % b[0] for (x, y) in zip(a, b) if x ^ y))::2])


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	li = sorted(l)
	c = 0
	mi = li[0]
	for i in range(n):
		if l[i] % mi != 0 and l[i] != li[i]:
			c = 1
			break
	if c == 0:
		print('YES')
	else:
		print('NO')


import sys

def rl():
	return [int(w) for w in sys.stdin.readline().split()]

def gcd(i, j):
	if j == 0:
		return i
	return gcd(j, i % j)
for _ in range(rl()[0]):
	(n,) = rl()
	a = rl()
	b = sorted(a)
	m = min(a)
	flag = True
	for i in range(n):
		if a[i] != b[i] and a[i] % m != 0:
			print('NO')
			flag = False
			break
	if flag:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().strip().split(' ')))
	B = sorted(A)
	m = min(A)
	flag = 0
	for i in range(len(A)):
		if A[i] % m != 0:
			if A[i] != B[i]:
				flag = 1
				break
	if flag:
		print('NO')
	else:
		print('YES')


from math import gcd
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(p) for p in input().split()]
	sortedArr = sorted(arr)
	m = sortedArr[0]
	if arr == sortedArr:
		print('YES')
		continue
	count = 0
	for i in range(len(arr)):
		if arr[i] != sortedArr[i]:
			if gcd(arr[i], m) != m:
				count += 1
				print('NO')
				break
	if not count:
		print('YES')


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split(' ')))
	minimum = min(li)
	temp = list(sorted(li))
	i = 0
	res = True
	while i < n:
		if li[i] != temp[i]:
			if li[i] % minimum != 0:
				res = False
				break
		i += 1
	if res:
		print('YES')
	else:
		print('NO')


def fun(l, n):
	x = min(l)
	l_copy = l.copy()
	l_copy.sort()
	for i in range(n):
		if l[i] != l_copy[i] and l[i] % x != 0:
			return 'NO'
	return 'YES'
t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	print(fun(l, n))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	b = sorted(a)
	for i in range(n):
		if a[i] != b[i] and a[i] % m != 0 and (b[i] != 0):
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	m = min(a)
	l = sorted([x for x in a if not x % m])
	b = a
	for i in range(n):
		if not a[i] % m:
			b[i] = 0
	for (i, x) in zip(range(n), b):
		if not x:
			b[i] = l[0]
			l.pop(0)
	if sorted(a) == b:
		print('YES')
	else:
		print('NO')


import os
import sys, math
from io import BytesIO, IOBase

def main():
	t = int(input())
	for case in range(t):
		n = int(input())
		l = list(map(int, input().split()))
		s = sorted(l)
		mn = min(l)
		ans = True
		for i in range(n):
			if s[i] != l[i] and l[i] % mn != 0:
				ans = False
		print('YES') if ans else print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	size = int(input())
	flag = 1
	arr = list(map(int, input().split()))
	arr2 = sorted(arr)
	for i in range(size):
		if arr2[i] != arr[i]:
			if arr[i] % arr2[0]:
				flag = 0
				break
	print('YES' if flag else 'NO')


from sys import stdout, stdin
(_input, _print) = (stdin.readline, stdout.write)
(_sorted, _min, _range, _int) = (sorted, min, range, int)

def solution():

	def computeGCD(x, y):
		while y:
			(x, y) = (y, x % y)
		return x
	for _ in _range(_int(_input())):
		n = _int(_input())
		a = [_int(i) for i in _input().split()]
		ans = _sorted(a)
		min_gcd = _min(a)
		for i in _range(n):
			if a[i] != ans[i]:
				if min_gcd == computeGCD(a[i], min_gcd) and min_gcd == computeGCD(ans[i], min_gcd):
					continue
				else:
					print('NO')
					break
		else:
			print('YES')
solution()


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def solve():
	n = get_int()
	ls = get_list()
	mini = min(ls)
	temp = []
	factors = []
	copy_ls = []
	for val in ls:
		if val % mini == 0:
			factors.append(val)
			copy_ls.append(0)
		else:
			copy_ls.append(val)
			temp.append(val)
	rem_ls = list(sorted(temp))
	if rem_ls != temp:
		print('NO')
	else:
		factors.sort()
		i = 0
		le = len(factors)
		j = 0
		while i < le:
			while j < n and copy_ls[j] != 0:
				j += 1
			copy_ls[j] = factors[i]
			i += 1
		if copy_ls != list(sorted(copy_ls)):
			print('NO')
		else:
			print('YES')

def main():
	solve()
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cur = min(a)
	b = sorted(a)
	good = 1
	for i in range(n):
		if a[i] != b[i] and a[i] % cur > 0:
			good = 0
	if good:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = sorted(l)
	if l == l1:
		print('YES')
	else:
		mi = l1[0]
		f = 0
		for (i, j) in zip(l, l1):
			if i != j:
				if i % mi != 0:
					f = 1
					break
		if f == 1:
			print('NO')
		else:
			print('YES')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = sorted(l)
	m = min(l)
	f = 0
	for i in range(n):
		if l[i] != s[i] and l[i] % m != 0:
			f = 1
			break
	if f == 0:
		print('YES')
	else:
		print('NO')


cases = int(input())
for i in range(cases):
	n = int(input())
	l2 = input().split(' ')
	arr = []
	for a in l2:
		arr.append(int(a))
	smallest = min(arr)
	copy = arr[:]
	copy.sort()
	if copy == arr:
		print('YES')
		continue
	arr2 = []
	for j in range(n):
		e1 = arr[j]
		e2 = copy[j]
		if e1 != e2:
			arr2.append(e1)
	pos = True
	for e in arr2:
		if e % smallest != 0:
			pos = False
			break
	if pos:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
for f in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = min(a)
	poss = True
	rest = []
	for i in range(n):
		if a[i] % d == 0:
			rest.append(a[i])
			a[i] = 0
	rest.sort()
	j = 0
	if a[0] != 0:
		poss = False
	else:
		j = 1
		a[0] = rest[0]
	for i in range(1, n):
		if a[i] == 0:
			a[i] = rest[j]
			j += 1
		if a[i] < a[i - 1]:
			poss = False
	if poss:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	lst1 = sorted(lst)
	flag = 0
	for i in range(n):
		if lst[i] == lst1[i]:
			continue
		elif lst[i] % lst1[0] != 0:
			flag = 1
			break
	if flag:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = sorted(a)
	flag = False
	k = b[0]
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % k != 0:
				flag = True
				break
	print('NO' if flag else 'YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = min(a)
	c = sorted(a)
	diff = []
	for i in range(n):
		if a[i] != c[i]:
			diff.append(a[i])
	if len(diff) == 0:
		print('Yes')
	else:
		count = 0
		for i in range(len(diff)):
			if diff[i] % b != 0:
				count += 1
				break
			else:
				diff[i] = diff[i] // b
		if count != 0:
			print('No')
		else:
			print('Yes')


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	m = b[0]
	u = 0
	for (i, j) in zip(a, b):
		if i != j:
			if math.gcd(i, m) != m:
				u = 1
				break
	if u == 0:
		print('YES')
	else:
		print('NO')


import sys
inp = sys.stdin.buffer.readline
inar = lambda : list(map(int, inp().split()))
inin = lambda : inar()[0]
inst = lambda : inp().decode().rstrip()
from math import gcd
_T_ = inin()
for _t_ in range(_T_):
	n = inin()
	a = inar()
	mil = min(a)
	b = a[:]
	b.sort()
	check = []
	poss = True
	for i in range(n):
		if a[i] != b[i]:
			if gcd(a[i], b[i]) % mil == 0:
				pass
			else:
				poss = False
	if poss:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
from math import gcd, sqrt, factorial
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	n = I()
	a = L()
	p = sorted(a)
	mn = p[0]
	flg = True
	for i in range(n):
		if a[i] % mn != 0 and p[i] != a[i]:
			flg = False
			break
	print('YES' if flg else 'NO')


from collections import *
import sys
mod = 10 ** 9 + 7

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	b = arr.copy()
	b.sort()
	mn = min(arr)
	f = True
	for i in range(n):
		if arr[i] != b[i] and arr[i] % mn:
			f = False
			break
	if f:
		print('YES')
	else:
		print('NO')
for i in range(int(input())):
	main()


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	oa = a[:]
	a.sort()
	notpos = False
	mi = a[0]
	for i in range(n):
		if oa[i] != a[i]:
			if oa[i] % mi != 0:
				notpos = True
				break
	if notpos:
		print('NO')
	else:
		print('YES')


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True
for t in range(ii()):
	n = ii()
	arr = list(mi())
	brr = arr[:]
	arr = sorted(arr)
	min_ = arr[0]
	for i in range(n):
		if arr[i] != brr[i]:
			if brr[i] % min_ == 0:
				continue
			else:
				print('NO')
				break
	else:
		print('YES')


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = sorted(a)
	mn = min(a)
	f = 1
	for (cur, des) in zip(a, b):
		if cur != des and cur % mn != 0:
			f = 0
			break
	print('YES' if f else 'NO')


import math, sys
from collections import defaultdict, Counter, deque
INF = float('inf')
MOD = 10 ** 9 + 7

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactors(n):
	factors = []
	i = 2
	while i ** 2 <= n:
		while n % i == 0:
			factors.append(i)
			n //= i
		i += 1
	if n > 1:
		factors.append(n)
	return factors

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def a():
	(n, k) = vars()
	if k >= n:
		print(k - n)
	elif n % 2 == k % 2:
		print(0)
	else:
		print(1)

def b():
	(z1, o1, t1) = vars()
	(z2, o2, t2) = vars()
	s1 = '0' * z1 + '1' * o1 + '2' * t1
	s2 = '2' * t2 + '0' * z2 + '1' * o2
	ans = 0
	for i in range(len(s1)):
		mul = int(s1[i]) * int(s2[i])
		if s1[i] > s2[i]:
			ans += mul
		if s1[i] < s2[i]:
			ans -= mul
	print(ans)

def c():
	n = int(input())
	arr = array()
	inc = sorted(arr)
	mini = min(arr)
	res = 'YES'
	for i in range(n):
		if arr[i] % mini != 0 and arr[i] != inc[i]:
			res = 'NO'
			break
	print(res)
t = 1
t = int(input())
for _ in range(t):
	c()


import math

def check(lst, slst):
	for (l, s) in zip(lst, slst):
		if l != s and l % slst[0] != 0:
			print('NO')
			return
	print('YES')
	return
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	slst = sorted(lst)
	check(lst, slst)


from sys import stdin
import math
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().rstrip().split()))
	sortedA = sorted(a)
	req = min(a)
	for i in range(n):
		if a[i] != sortedA[i]:
			if math.gcd(req, a[i]) != req:
				print('NO')
				break
	else:
		print('YES')


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):
	n = II()
	aa = LI()
	bb = sorted(aa)

	def ok():
		for (a, b) in zip(aa, bb):
			if a == b:
				continue
			if a % bb[0]:
				return False
		return True
	if ok():
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	m = min(A)
	X = []
	idx = []
	for (i, a) in enumerate(A):
		if a % m == 0:
			X.append(a)
			idx.append(i)
	X.sort()
	for (i, x) in zip(idx, X):
		A[i] = x
	for i in range(n - 1):
		if A[i] > A[i + 1]:
			print('No')
			break
	else:
		print('Yes')


t = int(input())
for hh in range(0, t):
	n = int(input())
	b = []
	flag = 0
	a = [int(x) for x in input().split()]
	for i in range(0, n):
		b.append(a[i])
	mn = min(a)
	b.sort()
	for i in range(0, n):
		if a[i] % mn != 0:
			posnow = a[i]
			posnext = b[i]
			if a[i] != b[i]:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	z = sorted(a)
	if a == z:
		print('YES')
	else:
		m = min(a)
		flag = 0
		arr = []
		for i in range(n):
			if z[i] != a[i]:
				arr.append(a[i])
		for i in arr:
			if i % m != 0:
				flag = 1
				break
		if flag == 1:
			print('NO')
		else:
			print('YES')


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mint = min(a)
	li = list()
	idx = list()
	for i in range(len(a)):
		if math.gcd(mint, a[i]) == mint:
			li.append(a[i])
			idx.append(i)
	li.sort()
	for i in range(len(li)):
		a[idx[i]] = li[i]
	print('YES' if sorted(a) == a else 'NO')


import sys
import math
from collections import defaultdict, Counter, deque
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d1 = sorted(a)
	m = min(a)
	for j in range(n):
		if a[j] != d1[j] and a[j] % m != 0:
			print('NO')
			break
	else:
		print('YES')


import math
t = int(input())
for i in range(t):
	n = int(input())
	data = list(map(int, input().split()))
	l = sorted(data)
	m = min(data)
	if any((l[i] % m != 0 and l[i] != data[i] for i in range(n))):
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.buffer.readline

def solution():
	n = int(input())
	l = list(map(int, input().split()))
	s = sorted(l)
	m = min(l)
	flag = 0
	for i in range(n):
		if l[i] % m:
			if s[i] != l[i]:
				flag = 1
	if flag:
		print('NO')
	else:
		print('YES')
t = int(input())
for _ in range(t):
	solution()


def process(n, li):
	t = min(li)
	lookup = set()
	for i in li:
		if i % t == 0:
			lookup.add(i)
	temp = list(li)
	temp.sort()
	for i in range(0, n):
		if temp[i] == li[i] or li[i] in lookup:
			pass
		else:
			print('NO')
			return
	print('YES')
tests = int(input())
for i in range(tests):
	n = int(input())
	li = list(map(int, input().split()))
	process(n, li)


q = int(input())
ans = []
for _ in range(q):
	n = int(input())
	S = list(map(int, input().split()))
	x = min(S)
	T = sorted(S)
	flg = True
	for (a, b) in zip(S, T):
		if a != b:
			if a % x != 0:
				flg = False
	ans.append('YES' if flg else 'NO')
print('\n'.join(map(str, ans)))


def check(a, n):
	c = a.copy()
	c.sort()
	m = c[0]
	for i in range(n):
		if a[i] % m != 0:
			if a[i] != c[i]:
				return 'NO'
	return 'YES'
T = int(input())
for t in range(T):
	n = int(input())
	ar = input().split()
	a = []
	for i in range(n):
		a.append(int(ar[i]))
	print(check(a, n))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	temp = arr[:]
	temp.sort()
	mini = min(arr)
	flag = 0
	for i in range(n):
		if arr[i] != temp[i]:
			if arr[i] % mini != 0:
				flag = 1
				break
	if flag == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	m = b[0]
	res = 0
	for x in range(n):
		if a[x] != b[x] and a[x] % m != 0:
			res = 1
			break
	if res:
		print('NO')
	else:
		print('YES')


from math import gcd
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	newList = sorted(l)
	m = newList[0]
	div = newList[0]
	for i in range(n):
		if l[i] != newList[i]:
			div = gcd(div, l[i])
	if div % m == 0:
		print('YES')
	else:
		print('NO')


def gcd(x, y):
	if x < y:
		(x, y) = (y, x)
	if x % y == 0:
		return y
	return gcd(y, x % y)
for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	m = l[:]
	l.sort()
	flag = 0
	for i in range(n):
		if m[i] == l[i]:
			continue
		elif gcd(m[i], l[0]) == l[0]:
			continue
		else:
			flag = 1
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


import os
import heapq
import sys, threading
import math
import bisect
import operator
from collections import defaultdict
from io import BytesIO, IOBase

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def inar():
	return [int(k) for k in input().split()]

def lcm(num1, num2):
	return num1 * num2 // gcd(num1, num2)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = inar()
		el = min(arr)
		counter = 0
		lis = arr.copy()
		lis.sort()
		for i in range(n):
			if arr[i] == lis[i]:
				continue
			elif arr[i] % el == 0:
				continue
			else:
				counter = 1
				break
		if counter:
			print('NO')
		else:
			print('YES')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if a == sorted(a):
		print('YES')
		continue
	flag = True
	l = sorted(a)
	ele = l[0]
	for i in range(len(a)):
		if a[i] != l[i]:
			if a[i] % ele:
				flag = False
				break
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	mi = min(a)
	b = a[:]
	b.sort()
	temp = 0
	for i in range(n):
		if a[i] != b[i] and a[i] % mi != 0:
			temp = 1
			break
	if temp == 1:
		print('NO')
	else:
		print('YES')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mini = min(a)
	mult = []
	check = [0] * n
	for i in range(n):
		if a[i] % mini == 0:
			check[i] = 1
			mult.append(a[i])
	mult.sort()
	k = 0
	for i in range(n):
		if check[i] == 1:
			a[i] = mult[k]
			k += 1
	if sorted(a) == a:
		print('YES')
	else:
		print('NO')


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = sorted(a)
	m = min(a)
	highest = -1
	res = 'YES'
	for i in range(n):
		if a[i] != b[i] and a[i] % m != 0:
			res = 'NO'
			break
	print(res)


from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())

def GCD(x, y):
	return GCD(y, x % y) if y else x
for _ in range(int(input())):
	n = int(input())
	a = list(mapi())
	b = sorted(a)
	mn = min(a)
	f = 1
	for i in range(n):
		if a[i] != b[i] and GCD(a[i], mn) != mn:
			f = 0
			break
	print('YES' if f else 'NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	yes = True
	for i in range(1, n):
		if arr[i - 1] > arr[i]:
			yes = False
			break
	if yes:
		print('YES')
		continue
	brr = [(arr[i], i) for i in range(n)]
	minelem = min(arr)
	brr.sort()
	yes = True
	for i in range(n):
		if brr[i][1] != i:
			if brr[i][0] % minelem != 0:
				yes = False
				break
	print('YES' if yes else 'NO')


from collections import defaultdict
import io, os
import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	(dict1, dict2) = (defaultdict(list), defaultdict(list))
	a = [int(i) for i in input().split() if i != '\n']
	copy = a[:]
	copy.sort()
	mina = min(a)
	for i in range(n):
		dict1[a[i]].append(i)
		dict2[copy[i]].append(i)
	for i in dict1:
		if i % mina != 0:
			dict1[i].sort()
			dict2[i].sort()
			if dict1[i] != dict2[i]:
				sys.stdout.write('NO' + '\n')
				break
	else:
		sys.stdout.write('YES' + '\n')


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		b = a[:]
		b.sort()
		k = min(a)
		if k == 1:
			print('YES')
			continue
		c = []
		d = []
		for (i, v) in enumerate(a):
			if v % k == 0:
				c.append(i)
				d.append(v)
		d.sort()
		for (i, v) in enumerate(c):
			a[v] = d[i]
		if a == b:
			print('YES')
		else:
			print('NO')
	return
main()


import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input().strip('\n'))
si = lambda : input().strip('\n')
dgl = lambda : list(map(int, input().strip('\n')))
f = lambda : map(int, input().strip('\n').split())
il = lambda : list(map(int, input().strip('\n').split()))
ls = lambda : list(input().strip('\n'))
lsc = lambda : list(input().strip('\n').split(' '))
lsi = lambda : [int(i) for i in ls()]
let = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(ii()):
	n = ii()
	l = il()
	temp1 = []
	temp2 = []
	mn = min(l)
	for i in range(n):
		if l[i] % mn == 0:
			temp1.append(i)
			temp2.append(l[i])
	temp2.sort()
	for (i, j) in zip(temp1, temp2):
		l[i] = j
	print('YNEOS'[l != sorted(l)::2])


list_ans = []
for _ in range(int(input())):
	n = int(input())
	list1 = list(map(int, input().split()))
	a = min(list1)
	list2 = list1[:]
	list2.sort()
	for i in range(n):
		if list1[i] != list2[i]:
			if list1[i] % a != 0:
				list_ans.append('NO')
				break
	else:
		list_ans.append('YES')
for item in list_ans:
	print(item)


for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	c = sorted(b)
	z = 'YES'
	k = c[0]
	for i in range(a):
		if b[i] != c[i]:
			if b[i] % k != 0 or c[i] % k != 0:
				z = 'NO'
				break
	print(z)


for t in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	x_ans = sorted(x)
	ans = 'YES'
	for i in range(n):
		if x[i] != x_ans[i] and x[i] % x_ans[0] != 0:
			ans = 'NO'
			break
	print(ans)


import sys
from math import gcd
input = sys.stdin.readline

def solve():
	n = int(input())
	al = list(map(int, input().split()))
	cop = al.copy()
	cop.sort()
	if n == 1:
		print('YES')
		return
	view = []
	sor = True
	for i in range(n):
		if al[i] != cop[i]:
			sor = False
		if al[i] != cop[i] or al[i] % cop[0] == 0:
			view.append(al[i])
	if sor:
		print('YES')
		return
	great = gcd(view[0], view[1])
	for i in range(2, len(view)):
		great = gcd(great, view[i])
	print('YES' if great == cop[0] else 'NO')

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


def gcd(l):
	res = l[0]
	for i in range(1, len(l)):
		a = res
		b = l[i]
		while a != 0:
			(a, b) = (b % a, a)
		res = b
	return res
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print('YES')
	elif sorted(l) == l:
		print('YES')
	else:
		b = sorted(l)
		temp = []
		for i in range(n):
			if l[i] != b[i]:
				temp.append(l[i])
		temp.sort()
		res = gcd(temp)
		if res % b[0] == 0:
			print('YES')
		else:
			print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	a = l.copy()
	a.sort()
	for j in range(n):
		if l[j] != a[j] and l[j] % a[0] != 0:
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	k = min(lst)
	lk = []
	for i in range(len(lst)):
		if lst[i] % k == 0:
			lk.append(lst[i])
			lst[i] = -1
	lk = sorted(lk)
	idx = 0
	for i in range(len(lst)):
		if lst[i] == -1:
			lst[i] = lk[idx]
			idx += 1
	if lst == sorted(lst):
		print('YES')
	else:
		print('NO')


def check(a, b):
	small = min(a)
	for i in range(len(a)):
		if a[i] != b[i] and a[i] % small:
			return False
	return True
for z in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = a.copy()
	b.sort()
	if check(a, b):
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()
for _ in range(int(I1())):
	n = int(I1())
	a = list(I())
	b = sorted(a)
	m = min(a)
	f = 0
	for i in range(n):
		if a[i] != b[i] and a[i] % m != 0:
			f = 1
			break
	if f == 0:
		print('YES')
	else:
		print('NO')


from collections import deque
from math import ceil, sqrt
import sys
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	p = min(a)
	b = []
	for i in a:
		if i % p == 0:
			b.append(i)
	b.sort()
	j = 0
	for i in range(n):
		if a[i] % p == 0:
			a[i] = b[j]
			j += 1
	c = a.copy()
	c.sort()
	if a == c:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log, pi, atan
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b
for _ in range(int(inp())):
	n = iinp()
	a = lmp()
	mn = min(a)
	ml = []
	dv = l1d(n)
	for i in range(n):
		if a[i] % mn == 0:
			ml.append(a[i])
		else:
			dv[i] = a[i]
	ml.sort()
	j = 0
	for i in ml:
		while dv[j] > 0:
			j += 1
		dv[j] = i
		j += 1
	flg = False
	for i in range(n - 1):
		if dv[i] > dv[i + 1]:
			flg = True
			break
	if flg:
		print('NO')
	else:
		print('YES')


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	small = min(a)
	for i in range(n):
		if a[i] != b[i] and a[i] % small != 0:
			print('NO')
			return
	print('YES')

def main():
	t = int(input())
	while t:
		solve()
		t -= 1
main()


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	m = min(a)
	b = []
	s = 'YES'
	for i in a:
		b.append(i)
	b.sort()
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % m != 0 or b[i] % m != 0:
				s = 'NO'
				break
	print(s)


from collections import deque
from collections import OrderedDict
import math
import sys
import os
import threading
import bisect
import operator
import heapq
from atexit import register
from io import BytesIO
import io
from sys import stdin, stdout
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	minE = min(ar)
	arSort = [0] * n
	for i in range(n):
		arSort[i] = ar[i]
	Answer = 'YES'
	arSort.sort()
	for i in range(n):
		if arSort[i] != ar[i]:
			if ar[i] % minE != 0:
				Answer = 'NO'
	print(Answer)
sys.exit(0)

class Person:

	def __init__(self, name, age):
		self.name = name
		self.age = age
p1 = Person('heelo', 27)
print(help(Person))
age = 26
name = 'Swaroop'
print('Возрас {} -- {} лет'.format(name, age))
print(help(object))


from sys import stdin, stdout
input = stdin.readline
import math

def geti():
	return map(int, input().strip().split())

def getl():
	return list(map(int, input().strip().split()))

def gets():
	return input('')

def geta():
	return int(input())

def print_s(s):
	stdout.write(s + '\n')

def solve():
	n = geta()
	a = getl()
	b = sorted(a)
	for i in range(1, n):
		if b[i] != a[i] and b[i] % b[0] != 0:
			return 'NO'
	return 'YES'
for _ in range(geta()):
	print(solve())


import math
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	A = min(a)
	h = sorted(a)
	c = True
	ans = 0
	flagg = [True] * n
	for i in range(n):
		if a[i] == h[i]:
			flagg[i] = False
		elif a[i] % A != 0:
			ans = -n - 1
	for i in range(n):
		if a[i] % A == 0:
			c = True
			ans += 1
	if ans > 1 and a[0] % A == 0:
		print('YES')
	elif flagg == [False] * n:
		print('YES')
	else:
		print('NO')


from math import gcd
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	li = list(l)
	l.sort()
	fl = list()
	for i in range(n):
		if l[i] != li[i]:
			fl.append(l[i])
	if len(fl) > 0:
		m = min(l)
		gx = m
		for i in fl:
			gx = gcd(gx, i)
		if gx == m:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	s = b[0]
	c = []
	for i in a:
		if i % s == 0:
			c.append(i)
	j = 0
	c.sort()
	for i in range(n):
		if a[i] % s == 0:
			x = c[j]
			a[i] = x
			j += 1
	if a == b:
		print('YES')
	else:
		print('NO')


import math
for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	m = min(arr)
	if n == 1:
		print('YES')
	else:
		ans = 'YES'
		b = sorted(arr)
		for i in range(n):
			if arr[i] != b[i] and arr[i] % m > 0:
				ans = 'NO'
				break
		print(ans)


TC = int(input())
while TC > 0:
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	print('NO' if any((a[i] != b[i] and a[i] % b[0] for i in range(n))) else 'YES')
	TC -= 1


from bisect import bisect_left, bisect_right
from sys import stdin, stdout, maxsize
import math
from functools import cmp_to_key
R = lambda : stdin.readline().strip()
RL = lambda f=None: list(map(f, R().split(' '))) if f else list(R().split(' '))
output = lambda x: stdout.write(str(x) + '\n')
output_list = lambda x: output(' '.join(map(str, x)))
for tc in range(int(R())):
	n = int(R())
	a = RL(int)
	b = a.copy()
	m = min(a)
	b.sort()
	ans = True
	for i in range(n):
		if a[i] != b[i] and (a[i] % m or b[i] % m):
			ans = False
			break
	if ans:
		output('YES')
	else:
		output('NO')


import sys
import math
import heapq
import collections
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

def input():
	return fast_reader().strip()

def print(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer('\n')

def printspace(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer(' ')

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	n = inputnum()
	a = inputlist()
	b = sorted(a)
	good = True
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % b[0] != 0 or b[i] % b[0] != 0:
				good = False
				break
	if good:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	m = sorted(li)
	if li == m:
		print('YES')
	elif m[0] == 1:
		print('YES')
	else:
		z = 0
		mi = min(li)
		for i in range(n):
			if li[i] != m[i]:
				if li[i] % mi != 0:
					print('NO')
					z = 1
					break
		if z == 0:
			print('YES')


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	ar1 = ar.copy()
	ar1.sort()
	count = 0
	min1 = ar1[0]
	for i in range(n):
		if ar[i] != ar1[i]:
			if ar[i] % min1 != 0:
				count = 1
				break
	if count == 1:
		print('NO')
	else:
		print('YES')


from math import gcd
for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	k = sorted(b)
	ans = 'YES'
	for i in range(a):
		if b[i] != k[i]:
			if b[i] % k[0]:
				ans = 'NO'
				break
	print(ans)


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def func(A):
	if len(A) == 1:
		return 'YES'
	low = min(A)
	s = sorted(A)
	copy = [True] * len(A)
	for i in range(len(A)):
		if gcd(low, A[i]) != low:
			copy[i] = False
	for i in range(len(copy)):
		if copy[i] == False:
			if A[i] != s[i]:
				return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	print(func(A))


def answer(n, arr):
	s = sorted(arr)
	m = s[0]
	for i in range(n):
		if arr[i] != s[i]:
			if arr[i] % m != 0:
				return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(answer(n, arr))


import sys, math
import io, os

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
INF = float('inf')
mod = int(1000000000.0) + 7
for t in range(int(data())):
	n = int(data())
	a = mdata()
	a1 = sorted(a)
	k = 1
	for i in range(n):
		if a[i] != a1[i] and a[i] % a1[0] != 0:
			k = 0
			break
	if k:
		out('YES')
	else:
		out('NO')


import math
from collections import Counter, defaultdict, deque

def int1():
	return int(input())

def map1():
	return map(int, input().split())

def list1():
	return list(map(int, input().split()))
mod = pow(10, 9) + 7

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def solve():
	n = int1()
	l1 = list1()
	min1 = min(l1)
	x = 0
	for i in range(n):
		x = gcd(x, l1[i])
	if min1 == x:
		print('YES')
	else:
		l2 = l1.copy()
		l2.sort()
		for i in range(n):
			if l1[i] == l2[i]:
				continue
			elif l1[i] != l2[i] and l1[i] % min1 != 0:
				print('NO')
				return
		print('YES')
for _ in range(int(input())):
	solve()


def calc(a):
	m = min(a)
	b = list()
	for item in a:
		if item % m == 0:
			b.append(item)
	b.sort()
	res = True
	current_ordered = 0
	b_index = 0
	for item in a:
		if item % m == 0:
			item = b[b_index]
			b_index += 1
		if item >= current_ordered:
			current_ordered = item
		else:
			res = False
			break
	return res

def get_ints():
	return [int(n) for n in input().split()]

def get_floats():
	return [float(n) for n in input().split()]

def seq2str(seq):
	return ' '.join((str(item) for item in seq))
a = get_ints()
assert len(a) == 1
t = a[0]
for i in range(t):
	a = get_ints()
	assert len(a) == 1
	n = a[0]
	a = get_ints()
	assert len(a) == n
	res = calc(a)
	print('YES' if res else 'NO')


T = int(input())
for _ in range(T):
	n = int(input())
	x = list(map(int, input().split()))
	y = [i for i in x]
	y.sort()
	small = min(x)
	ok = False
	for i in range(len(x)):
		if x[i] != y[i]:
			if x[i] % small == 0 and y[i] % small == 0:
				ok = True
			else:
				ok = False
				break
		else:
			ok = True
	if ok:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	minm = min(arr)
	hashed = [False] * n
	div = []
	for i in range(n):
		if arr[i] % minm == 0:
			hashed[i] = True
			div.append(arr[i])
	div.sort()
	j = 0
	temp = list(arr)
	for i in range(n):
		if hashed[i] == True:
			arr[i] = div[j]
			j += 1
	temp.sort()
	if temp == arr:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
from math import gcd
from functools import reduce

def read():
	N = int(input().strip())
	A = list(map(int, input().strip().split()))
	return (N, A)

class UnionFind:

	def __init__(self, n):
		self.n = n
		self.parent = [x for x in range(n)]

	def find(self, x):
		if self.parent[x] == x:
			return x
		else:
			self.parent[x] = self.find(self.parent[x])
			return self.parent[x]

	def union(self, x, y):
		x = self.find(x)
		y = self.find(y)
		if x > y:
			(x, y) = (y, x)
		self.parent[y] = x

	def parents(self):
		return [self.find(x) for x in range(self.n)]

def divisor(n):
	divisors = []
	i = 1
	while i * i <= n:
		if n % i == 0:
			divisors.append(i)
			if i * i != n:
				divisors.append(n // i)
		i += 1
	return list(sorted(divisors))

def solve(N, A):
	B = list(sorted(A))
	M = B[0]
	d = dict()
	for i in range(N):
		if A[i] != B[i]:
			d[B[i]] = True
	e = list(d.keys())
	if len(e) == 0:
		return 'YES'
	k = reduce(gcd, e)
	return 'YES' if k % M == 0 else 'NO'
T = int(input().strip())
for t in range(T):
	inputs = read()
	print('%s' % solve(*inputs))


for _ in range(int(input())):
	n = int(input())
	a = list(map(lambda x: int(x), input().split()))
	mx = min(a)
	x = []
	y = []
	for (i, item) in enumerate(a):
		if item % mx == 0:
			x.append(i)
			y.append(item)
	y.sort()
	i = 0
	while i < len(x):
		a[x[i]] = y[i]
		i += 1
	i = 0
	flag = True
	while i < n - 1:
		if a[i + 1] < a[i]:
			flag = False
			break
		i += 1
	if flag:
		print('YES')
	else:
		print('NO')


import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
decimal.getcontext().prec = 46

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def modefiedSieve():
	mx = 10 ** 7 + 1
	sieve = [-1] * mx
	for i in range(2, mx):
		if sieve[i] == -1:
			sieve[i] = i
			for j in range(i * i, mx, i):
				if sieve[j] == -1:
					sieve[j] = i
	return sieve

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]))

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def DFS(n, s, adj):
	visited = [False for i in range(n + 1)]
	stack = []
	stack.append(s)
	while len(stack):
		s = stack[-1]
		stack.pop()
		if not visited[s]:
			visited[s] = True
		for node in adj[s]:
			if not visited[node]:
				stack.append(node)

def maxSubArraySum(a, size):
	maxint = 10 ** 10
	max_so_far = -maxint - 1
	max_ending_here = 0
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far

def solve():
	n = int(input())
	arr = list_input()
	temp = sorted(arr)
	if arr == temp:
		print('YES')
		return
	minn = temp[0]
	b = []
	for i in range(n):
		if arr[i] % minn == 0:
			b.append(arr[i])
			arr[i] = -1
	if b == []:
		print('NO')
		return
	b.sort()
	for i in range(n):
		if arr[i] == -1:
			arr[i] = b.pop(0)
	if arr == temp:
		print('YES')
		return
	print('NO')
t = 1
t = int(input())
for _ in range(t):
	solve()


import sys
import os.path
import heapq
from sys import stdin, stdout
from collections import *
from math import gcd, ceil, floor
mod = int(1000000000.0 + 7)
m1 = float('inf')
if os.path.exists('Updated prg/Input3d.txt'):
	sys.stdout = open('Updated prg/Output3d.txt', 'w')
	sys.stdin = open('Updated prg/Input3d.txt', 'r')

def sinp():
	return input()

def ninp():
	return int(sinp())

def mapinp():
	return map(int, sinp().split())

def smapinp():
	return map(str, sinp().split())

def linp():
	return list(mapinp())

def sl():
	return list(sinp().split())

def power(n):
	return 1 << n

def prnt(a):
	print(a)

def prntl(a):
	print(a, end=' ')

def prntlist(l1):
	print(*l1)
for _ in range(ninp()):
	n = ninp()
	a = linp()
	if a == sorted(a):
		prnt('YES')
	else:
		a1 = sorted(a)
		flag = False
		m1 = min(a)
		for i in range(n):
			if a[i] != a1[i]:
				if a[i] % m1 != 0:
					flag = True
		if flag:
			prnt('NO')
		else:
			prnt('YES')


def check(a, b):
	c = sorted(a)
	tmp = [0] * sum(b)
	j = 0
	for i in range(len(b)):
		if b[i] == 1:
			tmp[j] = a[i]
			j += 1
	tmp.sort()
	j = 0
	for i in range(len(b)):
		if b[i] == 1:
			a[i] = tmp[j]
			j += 1
	for i in range(len(a)):
		if a[i] != c[i]:
			return False
	return True
T = int(input())
for t in range(T):
	n = int(input())
	a = [int(k) for k in input().split()]
	m = min(a)
	b = [0] * len(a)
	for i in range(len(a)):
		if a[i] % m == 0:
			b[i] = 1
	print('YES') if check(a, b) else print('NO')


def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = sorted(l)
	mini = x[0]
	f = 0
	for i in range(n):
		if l[i] != mini:
			if gcd(mini, l[i]) == mini or l[i] == x[i]:
				pass
			elif gcd(mini, l[i]) != mini and l[i] != x[i]:
				print('NO')
				f = 1
				break
	if f == 0:
		print('YES')


import sys
from math import gcd
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	A = [a[i] for i in range(n)]
	A.sort()
	m = min(a)
	check = all((a[i] == A[i] or a[i] % m == 0 for i in range(n)))
	print('YES' if check else 'NO')


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = sorted(l)
	for i in range(n):
		if l[i] != s[i] and l[i] % s[0] != 0:
			print('NO')
			break
	else:
		print('YES')


def solve(arr, n):
	m = min(arr)
	a = []
	for i in range(n):
		if arr[i] % m == 0:
			a.append(arr[i])
			arr[i] = 'a'
	a.sort(reverse=True)
	for i in range(n):
		if arr[i] == 'a':
			arr[i] = a.pop()
	for i in range(1, n):
		if arr[i] < arr[i - 1]:
			return 'NO'
	return 'YES'
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr, n))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = min(l)
	p = sorted(l)
	for i in range(len(l)):
		if l[i] != p[i] and l[i] % m != 0:
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	x1 = int(input())
	flag = 0
	arr = list(map(int, input().split()))
	sorted_arr = sorted(arr)
	mi = min(arr)
	for i in range(len(arr)):
		if sorted_arr[i] != arr[i]:
			if arr[i] % mi == 0:
				flag = 0
				continue
			else:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	minx = min(a)
	b = sorted(a)
	ans = 'YES'
	for i in range(n):
		if a[i] != b[i] and a[i] % minx != 0:
			ans = 'NO'
			break
	print(ans)


t = input()
t = int(t)

def solve(n, arr):
	a_min = min(arr)
	sorted_arr = [0 for _ in range(n)]
	new_arr = [0 for _ in range(n)]
	for i in range(n):
		if arr[i] % a_min != 0:
			sorted_arr[i] = arr[i]
		else:
			new_arr[i] = arr[i]
	new_arr = sorted(new_arr)
	for i in range(n):
		if new_arr[i] > 0:
			break
	cnt = 0
	cnt1 = i
	while cnt < n:
		if sorted_arr[cnt] == 0:
			sorted_arr[cnt] = new_arr[cnt1]
			cnt1 += 1
		cnt += 1
	for i in range(n - 1):
		if sorted_arr[i + 1] < sorted_arr[i]:
			print('NO')
			return
	print('YES')
	return
for i in range(t):
	n = input()
	n = int(n)
	arr = list(map(int, input().split()))
	solve(n, arr)


def solve(n, arr):
	doneSort = sorted(arr)
	smallest = doneSort[0]
	index = 0
	while index < n:
		if arr[index] != doneSort[index]:
			if arr[index] % smallest == 0 and doneSort[index] % smallest == 0:
				pass
			else:
				return 'NO'
		index += 1
	return 'YES'
for x in range(int(input())):
	n = int(input())
	seq = list(map(int, input().split()))
	print(solve(n, seq))


t = int(input())
for i in range(0, t):
	m = int(input())
	arr = list(map(int, input().strip().split()))[:m]
	arr1 = []
	for j in arr:
		arr1.append(j)
	arr.sort()
	y = arr[0]
	ct = 0
	for j in range(0, m):
		if arr[j] != arr1[j]:
			if arr1[j] % y != 0:
				print('NO')
				ct += 1
				break
	if ct == 0:
		print('YES')


def isSortedArrayPossible(arrayLength, array, sortedArray):
	minValue = sortedArray[0]
	if any((array[i] != sortedArray[i] and array[i] % minValue for i in range(arrayLength))):
		return 'NO'
	return 'YES'

def testCase_1401c():
	arrayLength = int(input())
	array = tuple(map(int, input().split(' ')))
	return (arrayLength, array, sorted(array))
nTestCases = int(input())
testCases = tuple((testCase_1401c() for x in range(nTestCases)))
tuple((print(isSortedArrayPossible(*testCase)) for testCase in testCases))


def gcd(a: int, b: int) -> int:
	return a if b == 0 else gcd(b, a % b)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	c = []
	for (x, y) in zip(a, b):
		if x != y:
			c += (x,)
	if len(c):
		g = c[0]
		for x in c[1:]:
			g = gcd(g, x)
		if g % min(a) == 0:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	a = arr.copy()
	m = min(arr)
	a.sort()
	flag = 0
	for i in range(len(arr)):
		if a[i] != arr[i]:
			if arr[i] % m != 0:
				flag = -1
				break
	if flag == -1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	kl = sorted(arr)
	if 1:
		flag = 1
		mn = kl[0]
		for i in range(n):
			if arr[i] != kl[i]:
				if arr[i] % mn != 0:
					flag = 0
					break
		if flag:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = l[:]
	m.sort()
	me = min(l)
	main = []
	for i in range(len(l)):
		if m[i] != l[i]:
			main.append(l[i])
	if len(l) == 1:
		print('YES')
	else:
		f = 0
		for i in main:
			if i % me != 0:
				f = 1
				break
		if f == 0:
			print('YES')
		else:
			print('NO')


t = int(input())
while t:
	n = int(input())
	arr = [int(e) for e in input().split()]
	sort_arr = sorted(arr)
	mn = min(arr)
	not_pass = False
	for i in range(n):
		if arr[i] != sort_arr[i]:
			if arr[i] % mn != 0:
				not_pass = True
				break
	if not_pass:
		print('NO')
	else:
		print('YES')
	t -= 1


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	arr = sorted(a)
	if arr == a:
		print('YES')
	else:
		flag = 0
		for i in range(n):
			if a[i] != arr[i]:
				if a[i] % arr[0] != 0:
					print('NO')
					flag = 1
					break
		if flag == 0:
			print('YES')


import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
	(N,) = map(int, input().split())
	X = list(map(int, input().split()))
	mx = min(X)
	B = []
	vs = set()
	for i in range(N):
		if X[i] % mx == 0:
			vs.add(i)
			B.append(X[i])
	B.sort()
	B = B[::-1]
	for i in range(N):
		if i in vs:
			b = B.pop()
			X[i] = b
	for i in range(N - 1):
		if X[i] > X[i + 1]:
			print('NO')
			break
	else:
		print('YES')


def squareRoot(number, precision):
	start = 0
	(end, ans) = (number, 1)
	while start <= end:
		mid = int((start + end) / 2)
		print(mid)
		if mid * mid == number:
			ans = mid
			print('hi')
			break
		if mid * mid < number:
			start = mid + 1
		else:
			end = mid - 1
	increment = 0.1
	print(str(ans) + 'hhee')
	for i in range(0, precision):
		while ans * ans <= number:
			ans += increment
		ans = ans - increment
		increment = increment / 10
	return ans
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ar = list.copy(arr)
	ar.sort()
	m = ar[0]
	flag = 0
	for i in range(len(ar)):
		if ar[i] != arr[i]:
			if arr[i] % m != 0:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


from math import gcd
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	temp = a[:]
	temp.sort()
	x = temp[0]
	flag = True
	for i in range(n):
		if temp[i] == a[i]:
			continue
		else:
			flag = a[i] % x == 0
			if not flag:
				break
	if flag:
		print('YES')
	else:
		print('NO')


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	arr = l()
	temp = arr[:]
	temp.sort()
	g = temp[0]
	for i in range(n):
		if temp[i] != arr[i]:
			if g == -1:
				g = temp[i]
			else:
				g = gcd(temp[i], g)
	if g != temp[0]:
		out('NO')
		continue
	out('YES')


import sys
sys.setrecursionlimit(10 ** 6)
from collections import Counter, defaultdict as dft

def mp():
	return map(int, input().split())

def ml():
	return list(map(int, input().split()))
mxn = 10 ** 9

def solve():
	n = int(input())
	arr = ml()
	check = []
	mn = min(arr)
	for i in range(n):
		if arr[i] % mn:
			check.append(i)
	ls = sorted(arr)
	for i in check:
		if ls[i] != arr[i]:
			return 'NO'
	return 'YEs'
t = int(input())
for _ in range(t):
	print(solve())


def sol(lis):
	s = sorted(lis)
	m = min(lis)
	for i in range(len(lis)):
		if lis[i] != s[i] and lis[i] % m:
			print('NO')
			return
	print('YES')
	return
k = int(input())
for i in range(k):
	input()
	lis = list(map(int, input().split(' ')))
	sol(lis)


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	b.sort()
	ans = []
	if a == b:
		print('YES')
		continue
	for i in range(n):
		if a[i] != b[i]:
			ans.append(a[i])
	ans1 = gcd(ans[0], ans[1])
	for i in range(2, len(ans)):
		ans1 = gcd(ans[i], ans1)
	k = min(a)
	if ans1 != min(a):
		flag = 0
		for i in range(n):
			if a[i] == b[i]:
				if gcd(ans1, a[i]) == k:
					print('YES')
					flag = 1
					break
		if flag != 1:
			print('NO')
	else:
		print('YES')


r = int(input())
import math
for i in range(r):
	_ = int(input())
	l1 = list(map(int, input().split()))
	l2 = sorted(l1)
	status = 'YES'
	for (i, j) in zip(l1, l2):
		if i != j and i % l2[0] != 0:
			status = 'NO'
	print(status)


from sys import stdin
input = stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	(*a,) = map(int, input().split())
	b = sorted(a)
	for i in range(n):
		if a[i] % b[0] and a[i] != b[i]:
			print('NO')
			break
	else:
		print('YES')


from sys import stdin
from collections import Counter, defaultdict
from math import gcd
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	l = arr.copy()
	l.sort()
	mi = min(arr)
	f = 1
	for i in range(n):
		if arr[i] != l[i]:
			if gcd(gcd(l[i], arr[i]), mi) != mi:
				f = 0
	if f:
		print('YES')
	else:
		print('NO')


import math
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	line = input().split(' ')
	numbers = []
	for i in line:
		numbers.append(int(i))
	sorted_numbers = sorted(numbers)
	_min = sorted_numbers[0]
	good = True
	for i in range(0, n):
		if numbers[i] != sorted_numbers[i]:
			if numbers[i] % _min != 0:
				good = False
				break
	if good:
		print('YES')
	else:
		print('NO')


import math
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = sorted(l)
	minn = min(l)
	ans = True
	for j in range(n):
		if l[j] != k[j] and l[j] % minn != 0:
			ans = False
			break
	if ans == False:
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	s = [int(i) for i in input().split()]
	d = s.copy()
	d.sort()
	c = 1
	r = []
	a = d[0]
	for i in range(n):
		if d[i] != s[i]:
			if s[i] % a != 0:
				c = 0
				break
	if c:
		print('YES')
	else:
		print('NO')


import sys
from math import gcd
pprint = lambda s: print(' '.join(map(lambda x: str(x), s)))
input = lambda : sys.stdin.readline().strip()
ipnut = input
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a1 = list(sorted(a))
	g = min(a)
	f = True
	for i in range(n):
		if a[i] != a1[i]:
			if a[i] % g != 0:
				f = False
				break
	if f:
		print('YES')
	else:
		print('NO')


def iin():
	return int(input())

def lin():
	return list(map(int, input().split()))

def main():
	T = iin()
	while T:
		T -= 1
		n = iin()
		a = lin()
		a1 = sorted(a)
		mn = min(a)
		for i in range(n):
			if a[i] == a1[i]:
				pass
			elif a[i] % mn == 0:
				pass
			else:
				print('NO')
				break
		else:
			print('YES')
main()


from fractions import gcd

def tc():
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	moveable = []
	for x in a:
		if x % m == 0:
			moveable.append(x)
	moveable.sort()
	for i in reversed(range(n)):
		if a[i] % m == 0:
			a[i] = moveable.pop()
	if a == sorted(a):
		print('YES')
	else:
		print('NO')
T = int(input())
for _ in range(T):
	tc()


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	sortedA = sorted(a)
	if a == sortedA:
		print('YES')
	else:
		minVal = min(a)
		for (a, b) in zip(a, sortedA):
			if a != b and a % minVal != 0:
				print('NO')
				break
		else:
			print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sorted_a = sorted(a)
	mere = True
	for i in range(n):
		if a[i] != sorted_a[i] and a[i] % sorted_a[0] != 0:
			print('NO\n')
			mere = False
			break
	if mere:
		print('YES\n')


t = int(input())
while t != 0:
	n = int(input())
	arr = list(map(int, input().split()))
	arrSorted = sorted(arr)
	min = arrSorted[0]
	result = 'YES'
	for i in range(n):
		if arrSorted[i] % min != 0 and arrSorted[i] != arr[i]:
			result = 'NO'
			break
	print(result)
	t -= 1


for i in range(int(input())):
	n = int(input())
	ls = [int(a) for a in input().split()]
	ls1 = sorted(ls)
	mn = min(ls)
	fd = 1
	for j in range(n):
		if ls[j] != ls1[j]:
			if ls[j] % mn != 0:
				print('NO')
				fd = 0
				break
	if fd:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	p = min(a)
	v = []
	for j in a:
		if j % p == 0:
			v.append(j)
	v.sort()
	start = 0
	ans = True
	for i in range(n):
		if a[i] % p == 0:
			a[i] = v[start]
			start += 1
		if i > 0:
			if a[i - 1] > a[i]:
				ans = False
				break
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	p = b[0]
	f = False
	for j in range(n):
		if a[j] % p != 0:
			if a[j] != b[j]:
				f = True
	if f:
		print('NO')
	else:
		print('YES')


def great(arr):
	temp = sorted(arr)
	min_ = temp[0]
	i = 0
	while i < n:
		if arr[i] != temp[i] and arr[i] % min_ != 0:
			return 'NO'
		i += 1
	return 'YES'
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	print(great(a))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mn = min(l)
	sl = sorted(l)
	condition = True
	for i in range(n):
		if l[i] % mn:
			if l[i] != sl[i]:
				condition = False
				break
	print(['NO', 'YES'][condition])


T = int(input())
for t in range(0, T):
	n = int(input())
	array = list(map(int, input().split()))
	newarray = sorted(array)
	position = [False] * n
	divisible = [False] * n
	for i in range(0, n):
		if array[i] == newarray[i]:
			position[i] = True
		if array[i] % newarray[0] == 0:
			divisible[i] = True
	flag = 'YES'
	for j in range(0, n):
		if position[j] == False:
			if divisible[j] == False:
				flag = 'NO'
	print(flag)


from math import gcd
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	lis = arr[:]
	lis.sort()
	x = min(arr)
	flag = True
	for i in range(n):
		if lis[i] != arr[i] and arr[i] % x != 0:
			flag = False
	if flag:
		print('YES')
	else:
		print('NO')


from math import gcd
input = __import__('sys').stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	g = min(s)
	pp = []
	inds = []
	for i in range(n):
		if gcd(g, s[i]) == g:
			pp.append(s[i])
			inds.append(i)
	pp.sort()
	for i in range(len(inds)):
		s[inds[i]] = pp[i]
	print('YES' if s == sorted(s) else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	k = min(A)
	sA = A.copy()
	sA.sort()
	arr = []
	ans = None
	for i in range(n):
		if A[i] != sA[i]:
			arr.append(A[i])
			if A[i] % k != 0:
				ans = -1
				break
	if ans == -1:
		print('No')
	else:
		print('Yes')


t = int(input())
for i in range(t):
	n = int(input())
	nums = input()
	myarr = [0] * n
	k = 0
	s = 0
	for j in range(n):
		while k < len(nums) and nums[k] != ' ':
			k += 1
		myarr[j] = int(nums[s:k])
		k += 1
		s = k
	smallest = min(myarr)
	sarr = sorted(myarr)
	flag = True
	for j in range(n):
		if myarr[j] % smallest != 0 and sarr[j] != myarr[j]:
			flag = False
			break
	print('YES' if flag else 'NO')


def cando(n, a):
	x = min(a)
	if a == sorted(a):
		return 'YES'
	l = sorted(a)
	ind = []
	for i in range(n):
		if a[i] != l[i]:
			if a[i] % x != 0:
				return 'NO'
			ind.append(i)
	return 'YES'
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(cando(n, a))


import math

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	m = min(a)
	for i in range(n):
		if a[i] != b[i] and a[i] % m != 0:
			return 'NO'
	return 'YES'
t = int(input())
while t > 0:
	t -= 1
	print(solve())


n = int(input())

def gcd(a, b):
	if a > b:
		a = a % b
	else:
		b = b % a
	while a > 1 and b > 1:
		if a > b:
			gh = a
			a = a % b
		else:
			gh = b
			b = b % a
	if a == 0 or b == 0:
		return max(a, b)
	return min(a, b)

def find(l, c, mi):
	lis = []
	for i in range(len(l)):
		if l[i] != c[i]:
			lis += [c[i]]
	return lis

def check(lis, mi):
	for i in lis:
		if gcd(mi, i) != mi:
			return 'NO'
	return 'YES'
for i in range(n):
	m = int(input())
	l = list(map(int, input().split()))
	c = l[:]
	l = sorted(l)
	lis = find(l, c, l[0])
	print(check(lis, l[0]))


import math
for _ in range(int(input())):
	n = int(input())
	num = [int(i) for i in input().split()]
	num2 = num[:]
	num2.sort(reverse=True)
	zmin = min(num)
	for i in range(n):
		if num2[i] != num[n - 1 - i]:
			if zmin == math.gcd(zmin, num2[i]):
				continue
			else:
				print('NO')
				break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	for i in range(n):
		l1.append(l[i])
	l1.sort()
	m = l1[0]
	i = 0
	while i < n:
		if l[i] != l1[i]:
			if l[i] % m == 0:
				i += 1
			else:
				break
		else:
			i += 1
	if i == n:
		print('YES')
	else:
		print('NO')


from math import *
a = int(input())
for x in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	d = c.copy()
	d.sort()
	l = 0
	j = min(c)
	for x in range(b):
		if d[x] == c[x]:
			l += 1
		elif c[x] % j == 0:
			l += 1
		else:
			break
	if l == b:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = a.copy()
	a.sort()
	if l == a:
		print('YES')
	else:
		flag = 0
		pos = []
		temp = a[0]
		for i in range(n):
			if l[i] != a[i]:
				if l[i] % temp != 0:
					flag = 1
					print('NO')
					break
		if flag == 0:
			print('YES')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	lst = list(map(int, input().split()))
	lst2 = [i for i in lst]
	lst2.sort()
	m = min(lst)
	f = False
	for i in range(n):
		if lst[i] == lst2[i]:
			continue
		elif lst[i] != lst2[i]:
			if lst[i] % m == 0 and lst2[i] % m == 0:
				continue
			else:
				f = True
				break
	if f == True:
		print('NO')
	else:
		print('YES')


import math
import heapq
import bisect
import collections
import fractions
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	flag = False
	a.sort()
	for i in range(n):
		if a[i] != b[i] and a[i] % a[0] != 0:
			flag = True
			break
	if flag:
		print('NO')
	else:
		print('YES')


t = int(input())
from math import gcd

def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	b = a.copy()
	b.sort()
	res = True
	for i in range(0, n):
		if b[i] != a[i] and gcd(a[i], b[i]) % b[0] != 0:
			res = False
	if res:
		print('YES')
	else:
		print('NO')
for _ in range(t):
	solve()


import math

def calc(a_list, t_list):
	for (ind, a) in enumerate(a_list):
		if math.gcd(a, min(a_list)) != min(a_list):
			if t_list[ind] == a:
				pass
			else:
				print('NO')
				return
	print('YES')
	return
t = input()
for test in range(int(t)):
	n = int(input())
	a = input()
	a = a.split()
	a_list = [int(i) for i in a]
	t_list = a_list.copy()
	t_list.sort()
	if min(a_list) != 1:
		calc(a_list, t_list)
	else:
		print('YES')


def mere(n, a):
	b = sorted(a)
	g = b[0]
	for i in range(n):
		if a[i] == b[i]:
			continue
		elif a[i] % g == 0 and b[i] % g == 0:
			continue
		else:
			return 'NO'
	return 'YES'
t = int(input())
a = []
for i in range(t):
	n = int(input())
	array = list(map(int, input().split(' ')))
	a.append([n, array])
for x in a:
	print(mere(*x))


def solve(l, k, n):
	if l == k:
		return True
	mini = min(l)
	for i in range(n):
		if l[i] != k[i] and l[i] % mini != 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = sorted(l)
	if solve(l, k, n):
		print('YES')
	else:
		print('NO')


import math
from collections import defaultdict
ml = lambda : map(int, input().split())
ll = lambda : list(map(int, input().split()))
ii = lambda : int(input())
ip = lambda : input()
mod = 1000000000.0 + 7
t = ii()
for _ in range(t):
	n = ii()
	a = ll()
	b = a[:]
	b.sort()
	f = 0
	mn = min(a)
	for i in range(n):
		if a[i] != b[i] and math.gcd(b[i], a[i]) % mn:
			f = 1
			break
	if f:
		print('NO')
	else:
		print('YES')


from functools import reduce
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def inn():
	return int(input())
mo = 1000000007

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
for _ in range(int(input())):
	n = inn()
	a = arr()
	b = a[:]
	b.sort()
	res = []
	mn = min(a)
	flag = 0
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % mn != 0 or b[i] % mn != 0:
				res += [(-1, -1)]
				flag = 1
				break
			else:
				res += [(a[i] // mn, b[i] // mn)]
	if flag == 1:
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	x = sorted(s)
	t = x[0]
	q = []
	for i in range(n):
		if s[i] % t == 0:
			q.append(1)
		else:
			q.append(0)
	l = 'YES'
	for i in range(n):
		if x[i] != s[i] and q[i] == 0:
			l = 'NO'
	print(l)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	b = arr.copy()
	arr.sort()
	temp = arr[0]
	fail = 0
	for i in range(n):
		if arr[i] % temp != 0 and arr[i] != b[i]:
			fail = 1
	if fail == 1:
		print('NO')
	else:
		print('YES')


from math import gcd

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		b = sorted(a)
		m = b[0]
		ans = 'YES'
		for (x, y) in zip(a, b):
			if x != y:
				if gcd(x, m) != m:
					ans = 'NO'
					break
		print(ans)
main()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = sorted(arr)
	m = min(arr)
	ok = True
	for (a, b) in zip(arr, s):
		if a % m != 0 and a != b:
			ok = False
			break
	if ok:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	b = a.copy()
	b.sort()
	min_ = b[0]
	ok = True
	for i in range(n):
		if a[i] != b[i] and a[i] % min_ > 0:
			ok = False
			break
	if ok:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split(' ')))
	l = []
	m = min(a)
	for i in range(n):
		if a[i] % m != 0:
			l.append(i)
	b = sorted(a)
	flag = 0
	for i in l:
		if b[i] != a[i]:
			flag = 1
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


import math
test = int(input())
for tes in range(test):
	n = int(input())
	l = list(map(int, input().split()))
	m = sorted(l)
	minel = m[0]
	count = 0
	for k in range(n):
		if l[k] != m[k]:
			val = l[k]
			if minel != math.gcd(val, minel):
				count += 1
				break
	if count == 0:
		print('YES')
	else:
		print('NO')


import math
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(s) for s in input().split(' ')]
	min_ = min(a)
	s = [(i, j) for (i, j) in enumerate(a)]
	s.sort(key=lambda x: x[1])
	possible = True
	for i in range(len(s)):
		if i != s[i][0]:
			if s[i][1] % min_ != 0 or a[i] % min_ != 0:
				possible = False
				break
	if possible:
		print('YES')
	else:
		print('NO')


import math

def primeFactors(n):
	temp = []
	while n % 2 == 0:
		temp.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			temp.append(int(i))
			n = n / i
	if n > 2:
		temp.append(int(n))
	return temp

def hcfnaive(a, b):
	if b == 0:
		return a
	else:
		return hcfnaive(b, a % b)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	minn = min(a)
	indexx = []
	for i in range(n):
		if a[i] % minn == 0:
			check = hcfnaive(a[i], minn)
			ok = 1
			if check != minn:
				ok = 0
			if ok == 1:
				indexx.append(i)
	halfSorted = []
	for i in indexx:
		halfSorted.append(a[i])
	halfSorted = sorted(halfSorted)
	j = 0
	for i in indexx:
		a[i] = halfSorted[j]
		j += 1
	if sorted(a) == a:
		print('YES')
	else:
		print('NO')


import sys
input = lambda : sys.stdin.readline().strip()
char = [chr(i) for i in range(97, 123)]
CHAR = [chr(i) for i in range(65, 91)]
mp = lambda : list(map(int, input().split()))
INT = lambda : int(input())
rn = lambda : range(INT())
from math import ceil, sqrt, factorial, gcd
for _ in rn():
	n = INT()
	l = mp()
	l_min = min(l)
	h = list(l)
	h.sort()
	flag = 0
	for i in range(n):
		if l[i] != h[i] and l[i] % l_min != 0:
			flag = 1
	if flag == 0:
		print('YES')
	else:
		print('NO')


import sys, math
input = sys.stdin.readline
t = int(input())
for r in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l2 = sorted(l)
	mini = min(l)
	flag = True
	for i in range(n):
		if l[i] != l2[i]:
			if l[i] % mini != 0:
				flag = False
				break
	if flag == True:
		print('YES')
	else:
		print('NO')


l = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	min = a[0]
	for i in range(1, n):
		if min > a[i]:
			min = a[i]
	b = a.copy()
	b.sort()
	c = 0
	odd = 0
	t = 0
	for i in range(n):
		if a[i] % min != 0:
			t = i
			odd = a[i]
			break
	if odd == 0:
		c = 1
		l.append('YES')
	else:
		for i in range(n):
			if a[i] % min == 0:
				if b[i] % min != 0:
					c = 1
					l.append('NO')
					break
			elif i > t:
				if a[i] == odd:
					if i - t > 1:
						c = 1
						l.append('NO')
						break
					else:
						t = i
				elif a[i] < odd:
					c = 1
					l.append('NO')
					break
				else:
					t = i
					odd = a[i]
	if c == 0:
		l.append('YES')
for i in l:
	print(i)


T = int(input())
for i in range(0, T):
	k = 0
	n = int(input())
	l = list(map(int, input().split()))
	s = sorted(l)
	mini = min(l)
	for j in range(0, len(l)):
		if l[j] != s[j] and l[j] % mini > 0:
			k = 1
	if k:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	r = list(map(int, input().split()))
	b = r[:]
	b = sorted(b)
	mn = min(r)
	flag = 1
	for i in range(n):
		if r[i] != b[i]:
			if r[i] % mn:
				flag = 0
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	p = min(a)
	s = sorted(a)
	flag = 1
	for i in range(n):
		if a[i] != s[i] and a[i] % p != 0:
			flag = 0
			break
		else:
			flag = 1
	if flag == 1:
		print('YES')
	else:
		print('NO')


from math import gcd
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mini = min(a)
	b = sorted(a)
	pr = 0
	for i in range(n):
		if a[i] != b[i] and a[i] % mini != 0:
			print('NO')
			pr = 1
			break
	if pr == 0:
		print('YES')


import math
t = int(input())
a = []
b = []
for i in range(t):
	o = int(input())
	h = list(map(int, input().split()))
	b.append(h)
	a.append(o)

def tully(n, arr):
	flag = 1
	k = []
	b = arr.copy()
	u = min(arr)
	arr.sort()
	for i in range(n):
		if arr[i] % u != 0:
			if arr[i] != b[i]:
				flag = 0
				break
	if flag:
		return 'YES'
	else:
		return 'NO'
for i in range(t):
	y = tully(a[i], b[i])
	print(y)


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
t = int(fastinput())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, fastinput().split()))
	temp = a.copy()
	temp.sort()
	ans = True
	x = temp[0]
	j = 0
	for i in range(n):
		if a[i] != temp[i] and a[i] % x != 0:
			ans = False
			break
	if ans:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l2 = l[:]
	l2.sort()
	mini = min(l2)
	f = 0
	for i in range(n):
		if l2[i] != l[i] and l[i] % mini != 0:
			f = 1
			break
	if f == 1:
		print('NO')
	else:
		print('YES')


import math as m
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = l.copy()
	p.sort()
	d = dict()
	min1 = min(l)
	flag = 1
	for i in range(n):
		if p[i] != l[i]:
			if p[i] % min1 != 0:
				flag = 0
				break
	print(['NO', 'YES'][flag])


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())

def solve():
	n = itg()
	arr = list(mpint())
	sort = sorted(arr)
	mn = sort[0]
	for i in range(n):
		if arr[i] != sort[i] and arr[i] % mn != 0:
			return False
	return True
for case in range(1, itg() + 1):
	print('YES' if solve() else 'NO')


import sys

def getInput():
	return sys.stdin.readline()[:-1]
cases = int(getInput())
for z in range(cases):
	getInput()
	ints = list((int(x) for x in getInput().split(' ')))
	small = min(ints)
	result = True
	sortedInts = sorted(ints)
	for x in range(len(ints)):
		if ints[x] != sortedInts[x]:
			if ints[x] % small != 0:
				print('NO')
				result = False
				break
	if result:
		print('YES')


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = 1
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	chk = True
	mi = a[0]
	l = [(a[0], 0)]
	for i in range(n - 1):
		if a[i + 1] < a[i]:
			chk = False
		l.append((a[i + 1], i + 1))
		mi = min(mi, a[i + 1])
	l.sort()
	if chk:
		print('YES')
		continue
	else:
		cond = True
		for i in range(n):
			if i != l[i][1]:
				if l[i][0] % mi != 0:
					cond = False
					break
		if cond:
			print('YES')
		else:
			print('NO')


import math
for _ in range(int(input())):
	x = int(input())
	y = list(map(int, input().split()))
	t = sorted(y)
	z = []
	d = min(y)
	for i in range(x):
		if math.gcd(y[i], d) == d:
			z.append(y[i])
			y[i] = -1
	z.sort()
	j = 0
	for i in range(x):
		if y[i] == -1:
			y[i] = z[j]
			j = j + 1
	if y == sorted(y):
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = min(l)
	l1 = l.copy()
	l1.sort()
	flag = 0
	for i in range(len(l)):
		if l[i] != l1[i] and l[i] % a > 0:
			flag = 1
	if flag == 0:
		print('YES')
	else:
		print('NO')


import math
for _ in range(int(input())):
	n = int(input())
	b = list(map(int, input().split()))
	x = min(b)
	c = sorted(b)
	a = []
	for i in range(n):
		if b[i] != c[i]:
			a.append(b[i])
	if a:
		ans = a[0]
		y = True
		for i in range(1, len(a)):
			ans = math.gcd(ans, a[i])
		if ans % x == 0:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


import sys
I = lambda : int(input())
readline = lambda : sys.stdin.readline().strip('\n')
RM = readgen = lambda : map(int, readline().split(' '))
for _ in range(I()):
	(a, b) = (I(), [*RM()])
	c = sorted(b)
	m = c[0]
	for (i, j) in zip(b, c):
		if i != j and i % m:
			print('NO')
			break
	else:
		print('YES')
quit()
for _ in range(I()):
	(a, b) = ([*RM()], [*RM()])
	summ = 0
	minn = min(a[2], b[1])
	a[2] -= minn
	b[1] -= minn
	summ += minn
	minn = min(a[0], b[2])
	a[0] -= minn
	b[2] -= minn
	minn = min(a[2], b[2])
	a[2] -= minn
	b[2] -= minn
	minn = min(a[1], b[2])
	a[2] -= minn
	b[2] -= minn
	summ -= minn
	print(summ << 1)
quit()
for _ in range(I()):
	(n, k) = RM()
	if n <= k:
		print(k - n)
	else:
		print((n - k) % 2)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mi = min(a)
	b = a[:]
	b.sort()
	if b == a:
		print('YES')
	else:
		for i in range(n):
			if a[i] != b[i]:
				if a[i] % mi:
					print('NO')
					break
		else:
			print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	final = sorted(a)
	m = final[0]
	for i in range(n):
		if a[i] != final[i] and a[i] % m != 0:
			print('NO')
			break
	else:
		print('YES')


t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = sorted(a)
	result = 'YES'
	for index in range(0, n):
		if a[index] != b[index] and a[index] % b[0] != 0:
			result = 'NO'
	print(result)


t = int(input(''))
arr = []
for i in range(t):
	input('')
	arr.append(list(map(int, input().split())))
for i in arr:
	x = sorted(i)
	diff = x
	mn = x[0]
	flag = 0
	for j in range(len(i)):
		if x[j] == i[j]:
			continue
		elif x[j] % mn:
			flag = 1
			break
	if flag:
		print('NO')
	else:
		print('YES')


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = input().split()
	b = []
	for i in range(0, n):
		a[i] = int(a[i])
		b.append(a[i])
	b.sort()
	q = 0
	mi = b[0]
	for i in range(0, n):
		if b[i] % mi != 0:
			if a[i] != b[i]:
				q = 1
				break
	if q == 0:
		print('YES')
	else:
		print('NO')


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		m = min(a)
		k = []
		for j in range(len(a)):
			if a[j] % m == 0:
				k.append((j, a[j]))
		sk = sorted(k, key=lambda x: x[1])
		for j in range(len(sk)):
			a[k[j][0]] = sk[j][1]
		f = True
		for i in range(1, len(a)):
			if a[i] < a[i - 1]:
				f = False
				break
		if f:
			print('YES')
		else:
			print('NO')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	l1 = [int(x) for x in input().split()]
	target = sorted(l1.copy())
	k = min(l1)
	ruined = 0
	for x in range(n):
		if target[x] != l1[x]:
			if target[x] % k or l1[x] % k:
				ruined = 1
				break
	if ruined:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for j in a:
		b.append(j)
	b.sort()
	f = True
	m = min(a)
	for j in range(n):
		if a[j] % m != 0 and b[j] != a[j]:
			print('NO')
			f = False
			break
	if f:
		print('YES')


from math import gcd
from collections import deque

def solve(n, nums):
	min_num = min(nums)
	can = []
	cannot = deque([])
	for (i, num) in enumerate(nums):
		if gcd(num, min_num) != min_num:
			cannot.append((num, i))
		else:
			can.append((num, i))
	can.sort()
	can = deque(can)
	sorted_nums = []
	while cannot:
		(cannot_num, i) = cannot.popleft()
		while len(sorted_nums) < i:
			if not can:
				return False
			v = can.popleft()[0]
			if sorted_nums and sorted_nums[-1] > v:
				return False
			sorted_nums.append(v)
		if sorted_nums and sorted_nums[-1] > cannot_num:
			return False
		sorted_nums.append(cannot_num)
	if can and sorted_nums and (sorted_nums[-1] > can[0][0]):
		return False
	return True
for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	print('YES' if solve(n, nums) else 'NO')


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import bisect as bi
import heapq

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_right(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		l = list(In())
		z = l.copy()
		z.sort()
		mi = z[0]
		flag = 0
		for x in range(n):
			if l[x] != z[x]:
				if l[x] % mi != 0:
					flag = 1
					break
				else:
					continue
		if flag:
			no()
		else:
			yes()
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


t = int(input())
for k in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	ma = min(a)
	l = []
	for i in range(n):
		if a[i] % ma == 0:
			l.append(a[i])
			a[i] = 0
	l.sort()
	i = 0
	for j in range(n):
		if a[j] == 0:
			a[j] = l[i]
			i += 1
	for o in range(n - 1):
		if a[o] > a[o + 1]:
			print('NO')
			break
	else:
		print('YES')


for i in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	c = min(b)
	d = [] + b
	d.sort()
	e = [False for j in b]
	for j in range(a):
		if b[j] == d[j]:
			e[j] = True
		elif d[j] % c == 0:
			e[j] = True
	if False in e:
		print('NO')
	else:
		print('YES')


def solve(n, lst):
	lstS = sorted(lst)
	minn = lstS[0]
	div = []
	for i in lst:
		div.append(i % minn)
	for i in range(n):
		if div[i] != 0:
			if lst[i] != lstS[i]:
				return 'NO'
	return 'YES'
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	print(solve(n, lst))


MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import Counter
for _ in range(ii()):
	n = ii()
	a = il()
	b = []
	mn = min(a)
	c = 0
	for i in range(0, n):
		b.append(a[i])
	b.sort()
	for i in range(0, n):
		if a[i] != b[i]:
			if b[i] % mn != 0 and c == 0:
				c = 1
				print('NO')
	if c == 0:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lis = arr[:]
	lis.sort()
	p = min(arr)
	if n == 1:
		print('YES')
		continue
	k = 0
	j = 0
	for i in range(n):
		if arr[i] != lis[i]:
			if arr[i] % p == 0:
				k += 1
			else:
				j = 1
				break
	if j == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	s = list(arr)
	s.sort()
	m = min(arr)
	status = True
	for i in range(n):
		if arr[i] % m and arr[i] != s[i]:
			status = False
			print('NO')
			break
	if status:
		print('YES')


def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = sorted(l)
	f = 0
	for i in range(n):
		if l[i] % k[0] != 0 and l[i] != k[i]:
			f = 1
	if f == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	srt_arr = sorted(arr)
	k = min(arr)
	cant = False
	for (pos, i) in enumerate(arr):
		if i != srt_arr[pos] and i % k != 0:
			cant = True
			break
	if cant:
		print('NO')
	else:
		print('YES')


t = int(input())
for T in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	m = min(arr)
	arr2 = arr.copy()
	arr2.sort()
	flag = 0
	for i in range(n):
		if arr[i] != arr2[i]:
			if arr[i] % m != 0:
				flag = 1
				break
	if flag == 0:
		print('YES')
	else:
		print('NO')


from math import gcd
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(sorted(a))
	if l == a:
		print('YES')
	else:
		mini = min(a)
		flag = 1
		for i in range(n):
			if gcd(mini, a[i]) != mini and a[i] != l[i]:
				print('NO')
				flag = 0
				break
		if flag:
			print('YES')


import sys
import math, bisect
from collections import defaultdict
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def neo():
	return map(int, input().split())

def Neo():
	return list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	A = Neo()
	B = A.copy()
	B.sort()
	m = min(A)
	f = 0
	for i in range(n):
		if A[i] != B[i]:
			if math.gcd(A[i], m) != m:
				f = 1
				break
	if f == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr1 = sorted(arr)
	min_ = arr1[0]
	f = 0
	for i in range(n):
		if arr[i] != arr1[i] and arr[i] % min_ != 0:
			print('NO')
			f = 1
			break
	if f != 1:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	m = min(l)
	b = l.copy()
	b.sort()
	ans = 'YES'
	for i in range(n):
		if l[i] % m != 0:
			if l[i] != b[i]:
				ans = 'NO'
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	m = b[0]
	f = True
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % m != 0:
				f = False
	print('YES' if f else 'NO')


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = min(l)
	ans = 'YES'
	sl = list(sorted(l))
	for i in range(len(l)):
		if l[i] % m != 0 and sl[i] != l[i]:
			ans = 'NO'
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	ans = min(a)
	l = []
	case = False
	for i in range(n):
		if a[i] % ans != 0:
			l.append(i)
	op = sorted(a)
	for i in l:
		if a[i] != op[i]:
			case = True
	if case:
		print('NO')
	else:
		print('YES')


t = int(input())
while t:
	n = int(input())
	l = []
	l = list(map(int, input().split()))
	a = l.copy()
	a.sort()
	c = []
	for i in range(len(a)):
		if a[i] != l[i]:
			c.append(l[i])
	m = min(l)
	f = 0
	if len(c) == 0:
		print('YES')
	else:
		for i in range(len(c)):
			if c[i] % m == 0:
				f = 0
			else:
				f = 1
				break
		if f == 0:
			print('YES')
		else:
			print('NO')
	t -= 1


r = lambda : map(int, input().split())
l = lambda : list(map(int, input().split()))
(t,) = r()
for j in range(t):
	(n,) = r()
	a = l()
	b = sorted(a)
	m = b[0]
	ans = 1
	for i in range(n):
		if a[i] != b[i] and (b[i] % m != 0 or a[i] % m != 0):
			ans = 0
			break
	if ans == 0:
		print('NO')
	else:
		print('YES')


test = int(input())
for t in range(test):
	n = int(input())
	arr = input().split()
	b = sorted(arr, key=int)
	m = 1000000007
	for i in range(n):
		if int(arr[i]) < m:
			m = int(arr[i])
	flag = 0
	for i in range(n):
		if int(arr[i]) != int(b[i]):
			if int(arr[i]) % m != 0:
				flag = 1
	if flag == 0:
		print('YES')
	elif flag == 1:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(j) for j in input().split()]
	srr = sorted(arr)
	mini = srr[0]
	ext = []
	for j in range(n):
		if arr[j] % mini != 0:
			if arr[j] != srr[j]:
				print('NO')
				break
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sorted_a = sorted(a)
	minimum = min(a)
	valid = True
	for j in range(n):
		if not (a[j] % minimum == 0 or a[j] == sorted_a[j]):
			valid = False
	if valid:
		print('YES')
	else:
		print('NO')


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	key = min(arr)
	arr1 = sorted(arr)
	flag = 0
	for i in range(n):
		if arr[i] % key != 0 and arr[i] != arr1[i]:
			flag = 1
			break
	if flag:
		print('NO')
	else:
		print('YES')


import math
for _ in range(int(input())):
	n = int(input())
	a = [int(o) for o in input().split()]
	a1 = [[a[i], i] for i in range(n)]
	a1.sort()
	s = set(a)
	mi = min(a)
	flag = 0
	for i in range(n):
		if a[i] % mi != 0:
			if a[i] != a1[i][0]:
				flag = 1
				break
	print('YES' if flag == 0 else 'NO')


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	minA = min(A)
	sortA = sorted(A)
	ans = 'YES'
	for i in range(N):
		if A[i] == sortA[i]:
			continue
		elif A[i] % minA == 0:
			continue
		else:
			ans = 'NO'
			break
	print(ans)


def check(lst, slst):
	for i in range(len(lst)):
		if lst[i] != slst[i] and lst[i] % slst[0] != 0:
			print('NO')
			return
	print('YES')
	return
for _ in range(int(input())):
	input()
	lst = list(map(int, input().split()))
	check(lst, sorted(lst))


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	m = min(a)
	for i in range(n):
		if a[i] % m == 0:
			b.append(a[i])
	b.sort()
	cur = 0
	for j in b:
		while a[cur] % m != 0:
			cur += 1
		a[cur] = j
		cur += 1
	for i in range(1, n):
		if a[i - 1] > a[i]:
			print('NO')
			return
	print('YES')
t = int(input())
for _ in range(t):
	solve()


from sys import stdin
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	a = [int(x) for x in inp().split()]
	aSorted = sorted(a)
	minimum = min(a)
	b = []
	for i in a:
		if i % minimum == 0:
			b.append(0)
		else:
			b.append(1)
	counter = 0
	for (i, j) in zip(a, aSorted):
		if i != j and b[counter] == 1:
			print('NO')
			break
		counter += 1
	else:
		print('YES')


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	possible = True
	a_sort = sorted(a)
	minv = min(a)
	for i in range(n):
		if a[i] != a_sort[i] and a[i] % minv != 0:
			possible = False
	print('YES' if possible else 'NO')


from math import gcd
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	arr = sorted(a)
	res = []
	for i in range(n):
		if a[i] != arr[i]:
			res.append(a[i])
	if len(res) == 0:
		print('YES')
		continue
	minm = arr[0]
	g = arr[0]
	for x in res:
		g = gcd(g, x)
	if g == minm:
		print('YES')
	else:
		print('NO')


import sys
import string
import math
import heapq
from collections import defaultdict
from functools import lru_cache
from collections import Counter
from fractions import Fraction

def mi(s):
	return map(int, s.strip().split())

def lmi(s):
	return list(mi(s))

def tmi(s):
	return tuple(mi(s))

def mf(f, s):
	return map(f, s)

def lmf(f, s):
	return list(mf(f, s))

def js(lst):
	return ' '.join((str(d) for d in lst))

def jsns(lst):
	return ''.join((str(d) for d in lst))

def line():
	return sys.stdin.readline().strip()

def linesp():
	return line().split()

def iline():
	return int(line())

def mat(n):
	matr = []
	for _ in range(n):
		matr.append(linesp())
	return matr

def matns(n):
	mat = []
	for _ in range(n):
		mat.append([c for c in line()])
	return mat

def mati(n):
	mat = []
	for _ in range(n):
		mat.append(lmi(line()))
	return mat

def pmat(mat):
	for row in mat:
		print(js(row))

def solve(arr):
	mm = min(arr)
	arr2 = list(sorted(arr))
	for (start, end) in zip(arr, arr2):
		if start == end:
			continue
		elif start % mm != 0 or end % mm != 0:
			print('NO')
			return
	print('YES')

def main():
	t = iline()
	for _ in range(t):
		line()
		solve(lmi(line()))
main()


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	a = sorted(l)
	s = min(l)
	f = 1
	for i in range(n):
		if l[i] != a[i]:
			if l[i] % s != 0:
				f = 0
				break
	if f:
		print('YES')
	else:
		print('NO')


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	m = min(arr)
	d = {}
	for i in range(n):
		if arr[i] % m != 0:
			if arr[i] in d:
				d[arr[i]].append(i)
			else:
				d[arr[i]] = [i]
	arr.sort()
	cond = True
	for i in range(n):
		if arr[i] % m != 0:
			if i not in d[arr[i]]:
				cond = False
				break
	if cond == True:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(a)
	b.sort()
	possible = 'YES'
	for i in range(n):
		if not b[i] == a[i] and (not a[i] % b[0] == 0):
			possible = 'NO'
			break
	print(possible)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	sorts = list(s)
	sorts.sort()
	mini = min(s)
	unpos = []
	for i in range(n):
		if s[i] != sorts[i]:
			unpos.append(s[i])
	flag = True
	for i in unpos:
		if i % mini != 0:
			flag = False
			break
	if flag:
		print('YES')
	else:
		print('NO')


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math
from collections import defaultdict
for _ in range(ri()):
	n = ri()
	l = ria()
	m = min(l)
	x = []
	y = []
	for i in range(n):
		if l[i] % m == 0:
			x.append(i)
			y.append(l[i])
	y.sort()
	for i in range(len(x)):
		l[x[i]] = y[i]
	k = 0
	for i in range(n - 1):
		if l[i] > l[i + 1]:
			k = 1
	print('NO') if k else print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(u) for u in input().split()]
	b = [x for x in a]
	a.sort()
	m = a[0]
	flag = 0
	for i in range(n):
		if a[i] != b[i]:
			if b[i] % m != 0:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


from math import gcd
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ap = a.copy()
	ap.sort()
	tf = True
	mn = min(a)
	for i in range(n):
		if a[i] != ap[i] and a[i] % mn != 0:
			tf = False
			break
	if tf:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	l2 = sorted(l)
	m = l2[0]
	for i in range(len(l)):
		if l[i] != l2[i] and l[i] % m != 0:
			print('NO')
			flag = 1
			break
	if flag == 0:
		print('YES')
	t -= 1


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = [int(x) for x in input().split(' ')]
		assert n == len(a)
		gcd = min(a)
		ideal = sorted(a)
		possible = True
		for i in range(len(a)):
			if a[i] % gcd != 0 and a[i] != ideal[i]:
				possible = False
				break
		if possible:
			print('YES')
		else:
			print('NO')
main()


import sys
for _ in range(int(sys.stdin.readline().rstrip())):
	n = int(sys.stdin.readline().rstrip())
	a = list(map(int, sys.stdin.readline().rstrip().split()))
	target = min(a)
	sort_result = sorted(a)
	check = [0] * n
	for i in range(n):
		if a[i] == sort_result[i]:
			check[i] = 1
	result = 'YES'
	for i in range(n):
		if check[i] == 0 and a[i] % target != 0:
			result = 'NO'
			break
	print(result)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	a = min(A)
	B = A.copy()
	B.sort()
	ans = 'Yes'
	for i in range(n):
		if A[i] % a and A[i] != B[i]:
			ans = 'No'
			break
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(v) for v in input().split()]
	b = a[:]
	a.sort()
	f = 0
	for j in range(n - 1, -1, -1):
		if a[j] != b[j]:
			if b[j] % a[0] != 0:
				f = 1
	if f == 0:
		print('YES')
	else:
		print('NO')


def readA():
	return list(map(int, input().split()))

def intPut():
	return int(input())

def arrIt(arr, n):
	result = 0
	for i in arr:
		result += i
	return result

def binarySearch(arr, l, r, x):
	while l <= r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def solve(arr, n):
	result = 'YES'
	m = min(arr)
	swapLst = []
	for i in arr:
		if i % m == 0:
			swapLst.append(i)
	arrSort = sorted(arr)
	lsw = len(swapLst)
	if lsw > 0:
		swapLst.sort()
	move_lst = []
	for i in range(n):
		if arr[i] != arrSort[i]:
			move_lst.append(arr[i])
	for i in move_lst:
		if binarySearch(swapLst, 0, lsw - 1, i) < 0:
			return 'NO'
	return result
for _ in range(intPut()):
	n = intPut()
	arr = readA()
	print(solve(arr, n))


import sys, math as mt
import heapq as hp
import collections as cc
import math as mt
import itertools as it
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	l = I()
	temp = sorted(l.copy())
	mm = min(l)
	tf = 1
	for i in range(n):
		if temp[i] != l[i] and mt.gcd(l[i], mm) != mm:
			tf = 0
	if tf:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout

def input():
	return stdin.readline().strip()

def ans(A):
	m = min(A)
	sA = sorted(A)
	for i in range(len(A)):
		if A[i] == sA[i]:
			continue
		if A[i] % m != 0:
			return 'NO'
	return 'YES'
T = int(input())
for t in range(T):
	input()
	A = input().split()
	A = list(map(int, A))
	print(ans(A))


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		mini = min(a)
		sorta = sorted(a)
		if sorta == a:
			print('YES')
			continue
		c1 = 0
		c2 = 0
		for i in range(n):
			if a[i] != sorta[i]:
				c1 += 1
				gg = gcd(mini, a[i])
				if gg == mini:
					c2 += 1
		if c1 == c2:
			print('YES')
		else:
			print('NO')
BUFSIZE = 8192

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	k = l.copy()
	k.sort()
	m = min(l)
	for i in range(len(l)):
		if k[i] != l[i]:
			if math.gcd(l[i], m) != m:
				print('NO')
				break
	else:
		print('YES')


t = sorted
for s in [*open(0)][2::2]:
	(*a,) = map(int, s.split())
	m = min(a)
	b = [i for i in range(len(a)) if a[i] % m < 1]
	for (i, x) in zip(b, t((a[i] for i in b))):
		a[i] = x
	print('YNEOS'[t(a) < a::2])


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	b = sorted(a)
	m = b[0]
	ch = True
	for i in range(n):
		if a[i] == b[i]:
			continue
		if a[i] % m != 0:
			ch = False
			break
	print('YES' if ch else 'NO')


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	n = int(z())
	l = zzz()
	ll = sorted(l)
	m = ll[0]
	ans = 'YES'
	for i in range(n):
		if l[i] % m != 0:
			if l[i] != ll[i]:
				ans = 'NO'
				break
	print(ans)


import sys
input = sys.stdin.readline

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0, hi=None):
	if hi == None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
from itertools import permutations
import math

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	ind = []
	val = []
	for i in range(n):
		if gcd(a[i], m) == m:
			ind.append(i)
			val.append(a[i])
	val.sort()
	for x in range(len(ind)):
		a[ind[x]] = val[x]
	if sorted(a) == a:
		print('YES')
	else:
		print('NO')
for _ in range(int(input())):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	mi = l[0]
	c = 0
	for i in range(1, n):
		mi = min(l[i], mi)
		if l[i] < l[i - 1]:
			c = 1
	if c == 0:
		print('YES')
	else:
		l1 = sorted(l)
		for i in range(n):
			if l[i] % mi == 0:
				continue
			elif l[i] != l1[i]:
				print('NO')
				break
		else:
			print('YES')


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	ars = sorted(ar)
	a = [0] * n
	p = []
	m = min(ar)
	for i in range(n):
		if ar[i] % m == 0:
			a[i] = 1
			p.append(ar[i])
	p.sort()
	p.reverse()
	for j in range(n):
		if a[j] == 1:
			ar[j] = p.pop()
	if ar == ars:
		print('YES')
	else:
		print('NO')


def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		smallest = min(a)
		x = sorted(a)
		move = [x % smallest == 0 for x in a]
		if all((i == j or m for (i, j, m) in zip(a, x, move))):
			print('YES')
		else:
			print('NO')
main()


import os
from io import BytesIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline

def lii():
	return list(map(int, input().split()))

def ii():
	return int(input())
gcd = lambda m, n: m if not n else gcd(n, m % n)
t = ii()
for _ in range(t):
	n = ii()
	A = lii()
	g = min(A)
	goal = sorted(A)
	flag = True
	for (a, b) in zip(A, goal):
		if a != b:
			tmp = gcd(a, b)
			if tmp % g != 0:
				flag = False
				break
	if flag:
		print('YES')
	else:
		print('NO')


def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def MAP2():
	return map(float, input().split())

def LIST():
	return list(map(int, input().split()))

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter, defaultdict
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]

def solve(a, b):
	if b == 0:
		return a
	else:
		return solve(b, a % b)
for tt in range(INT()):
	n = INT()
	arr = LIST()
	l = sorted(arr)
	if l == arr:
		print('YES')
	else:
		flag = True
		mn = min(arr)
		for i in range(n):
			if arr[i] != l[i] and arr[i] % mn != 0:
				flag = False
				break
		if flag:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = sorted(l)
	l2 = []
	mi = l1[0]
	ans = 0
	for i in range(n):
		if l[i] % mi != 0:
			if l1[i] != l[i]:
				ans = 1
				break
	if ans == 0:
		print('YES')
	else:
		print('NO')


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	n = II()
	a = LI()
	c = sorted(a)
	boo = True
	m = c[0]
	for i in range(n):
		if a[i] != c[i] and c[i] % m != 0:
			boo = False
			break
	print('YES' if boo else 'NO')


from math import gcd

def gcditer(x):
	ret = x[0]
	for i in range(1, len(x)):
		ret = gcd(ret, x[i])
	return ret
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	m = min(a)
	c = [a[i] for i in range(n) if a[i] != b[i] or a[i] % m == 0]
	if len(c) == 0:
		print('YES')
	elif gcditer(c) == m:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
input = stdin.readline
from math import gcd
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	mini = b[0]
	c = []
	for i in range(n):
		if b[i] != a[i]:
			c.append(i)
	for i in c:
		if gcd(a[i], mini) != mini:
			print('NO')
			break
	else:
		print('YES')


def parr(arr):
	print(*arr, sep=' ')

def gcd(a, b):
	while b:
		if b % a == 0:
			break
		tmp = a
		a = b % a
		b = tmp
	return a
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	d = min(a)
	yes = True
	for i in range(n):
		if a[i] % d != 0 and a[i] != b[i]:
			yes = False
			break
	print('YES') if yes else print('NO')


import sys
input = sys.stdin.readline
ins = lambda : input().rstrip()
ini = lambda : int(input().rstrip())
inm = lambda : map(int, input().split())
inl = lambda : list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))
ans = []
t = ini()
for _ in range(t):
	n = ini()
	a = inl()
	b = sorted(a)
	m = min(a)
	tmp = True
	for i in range(n):
		if a[i] != b[i] and a[i] % m != 0:
			tmp = False
			break
	ans.append('YES' if tmp else 'NO')
out(ans)


import sys
from math import gcd

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	A = LIST()
	movable = [0] * N
	mn = min(A)
	B = []
	for i in range(N):
		if gcd(A[i], mn) == mn:
			movable[i] = 1
			B.append(A[i])
	B.sort()
	j = 0
	ans = A[:]
	for i in range(N):
		if movable[i]:
			ans[i] = B[j]
			j += 1
	if sorted(A) == ans:
		YES()
	else:
		NO()


from math import *
from sys import *
input = stdin.readline
for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = sorted(l)
	f = 0
	m = min(l)
	for i in range(n):
		if l[i] % m != 0:
			if l[i] != d[i]:
				f = 1
				break
	if f == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	m = min(ar)
	b = ar[:]
	b.sort()
	flag = 0
	for i in range(n):
		if ar[i] != b[i]:
			if ar[i] % m != 0:
				flag = 1
				break
	if flag:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(item) for item in input().split()]
	smallest = min(a)
	div = []
	ret = [0] * n
	for (i, item) in enumerate(a):
		if item % smallest == 0:
			div.append(item)
		else:
			ret[i] = item
	div.sort(reverse=True)
	for (i, item) in enumerate(ret):
		if item == 0:
			ret[i] = div.pop()
	ok = True
	for (r1, r2) in zip(ret, ret[1:]):
		if r1 > r2:
			ok = False
			break
	if ok:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = sorted(a)
	mn = min(a)
	flag = True
	for i in range(n):
		if l[i] != a[i]:
			if l[i] % mn != 0:
				flag = False
				break
	if flag:
		print('YES')
	else:
		print('NO')


import sys
li = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : sys.stdin.readline().strip().split()
ni = lambda : int(sys.stdin.readline().strip())
nsi = lambda : sys.stdin.readline().strip()
from collections import defaultdict as df
from math import *
from functools import reduce

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
t = ni()
for _ in range(t):
	n = ni()
	dl = []
	l = li()
	ans = []
	for i in l:
		dl.append(i)
	if n == 1:
		print('YES')
	else:
		l.sort()
		for i in range(n):
			if l[i] != dl[i]:
				ans.append(l[i])
		if len(ans) == 0:
			print('YES')
		else:
			res = ans[0]
			for c in ans[1:]:
				res = gcd(res, c)
			if res % min(l) == 0:
				print('YES')
			else:
				print('NO')


I = lambda : map(int, input().split())
for _ in [0] * int(input()):
	input()
	a = list(I())
	b = sorted(a)
	x = b[0]
	for (i, j) in zip(a, b):
		if i % x != 0 and i != j:
			print('NO')
			break
	else:
		print('YES')


import sys
T = int(sys.stdin.readline().strip())
for t in range(0, T):
	n = int(sys.stdin.readline().strip())
	a = list(map(int, sys.stdin.readline().strip().split()))
	m = min(a)
	b = sorted(a)
	v = True
	for i in range(0, n):
		if a[i] != b[i] and a[i] % m != 0:
			v = False
	if v == True:
		print('YES')
	else:
		print('NO')


from math import *
from bisect import bisect_left as bl
from bisect import bisect_right as br
from collections import Counter as ctr
from collections import deque as dq
from array import array
from re import search
li = lambda : list(map(int, input().split()))
arr = lambda a: array('i', a)
bi = lambda n: bin(n).replace('0b', '')
yn = lambda f: print('NYOE S'[f::2])
sbstr = lambda a, s: search('.*'.join(a), s)

def solve():
	for _ in range(int(input())):
		n = int(input())
		a = li()
		b = a[:]
		b.sort()
		if a == b:
			print('YES')
		else:
			m = b[0]
			f = True
			for i in range(n):
				if b[i] != a[i]:
					f = not a[i] % m
					if not f:
						break
			yn(f)
solve()


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	x = []
	for i in a:
		b.append(i)
	m = min(b)
	c = sorted(a)
	for i in range(n):
		if b[i] != c[i]:
			x.append(b[i])
	for i in range(len(x)):
		if x[i] % m != 0:
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = input()
	lis = list(map(int, input().strip().split()))
	sortedlist = sorted(lis)
	ans = 'YES'
	least = min(lis)
	if least == 1:
		print(ans)
	else:
		for i in range(len(lis)):
			if lis[i] % least == 0:
				continue
			elif lis[i] != sortedlist[i]:
				ans = 'NO'
			else:
				continue
		print(ans)


gans = []
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	u = []
	w = []
	mn = min(s)
	for i in range(n):
		if s[i] % mn != 0:
			w.append((s[i], i))
		else:
			u.append(s[i])
	u.sort()
	ind = 0
	s = []
	for i in range(len(u)):
		while ind < len(w) and len(s) == w[ind][1]:
			s.append(w[ind][0])
			ind += 1
		s.append(u[i])
	while ind < len(w) and len(s) == w[ind][1]:
		s.append(w[ind][0])
		ind += 1
	for i in range(1, n):
		if s[i] < s[i - 1]:
			gans.append('NO')
			break
	else:
		gans.append('YES')
print('\n'.join(gans))


import sys

def solve(n, a):
	b = sorted(a)
	m = b[0]
	for i in range(n):
		if b[i] % m != 0 and a[i] != b[i]:
			print('NO')
			sys.stdout.flush()
			return
	print('YES')
	sys.stdout.flush()
T = int(input())
for t in range(T):
	n = int(input())
	a = [int(it) for it in input().split(' ')]
	solve(n, a)


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
for cases in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = l[:]
	d.sort()
	count = 0
	for i in range(0, n):
		if l[i] == d[i]:
			count += 1
		elif gcd(l[i], d[i]) == d[0] or gcd(l[i], d[i]) % d[0] == 0:
			count += 1
		else:
			break
	if count == n:
		print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import heapq
for j in range(int(input())):
	n = int(input())
	vals = list(map(int, input().split()))
	minny = min(vals)
	numbers = []
	inds = []
	heapq.heapify(numbers)
	heapq.heapify(inds)
	nv = []
	iv = []
	for s in range(n):
		if vals[s] % minny == 0:
			heapq.heappush(numbers, vals[s])
			heapq.heappush(inds, s)
		else:
			nv.append(vals[s])
			iv.append(s)
	vals.sort()
	broke = False
	while len(numbers) > 0:
		a1 = heapq.heappop(numbers)
		a2 = heapq.heappop(inds)
		if vals[a2] == a1:
			continue
		else:
			broke = True
			break
	for s in range(len(nv)):
		if vals[iv[s]] == nv[s]:
			continue
		else:
			broke = True
			break
	if broke == False:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	al = list(map(int, input().split()))
	s_al = al[:]
	s_al.sort()
	for (i, a) in enumerate(al):
		if a % s_al[0] != 0:
			if s_al[i] != a:
				print('NO')
				break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a_min = min(a)
	a1 = sorted(a)
	fl = True
	for i in range(n):
		if a[i] != a1[i]:
			if a[i] % a_min == 0 and a1[i] % a_min == 0:
				pass
			else:
				fl = False
				print('NO')
				break
	if fl:
		print('YES')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sa = [x for x in a]
	sa.sort()
	minA = min(a)
	ans = True
	for i in range(n):
		if a[i] % minA and a[i] != sa[i]:
			ans = False
			break
	if ans:
		print('YES')
	else:
		print('NO')


import math
t = int(input())
for _ in range(t):
	n = int(input().strip())
	arr = list(map(int, input().strip().split()))
	minVal = min(arr)
	if minVal == 1:
		print('YES')
		continue
	arr2 = sorted(arr)
	f = True
	for i in range(n):
		if arr2[i] != arr[i] and math.gcd(arr[i], arr2[i]) % minVal != 0:
			f = False
			break
	if f:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	li_1 = []
	li = list(map(int, input().split()))
	for j in li:
		li_1.append(j)
	li_1.sort()
	m = min(li)
	x = 0
	for j in range(n):
		if li[j] != li_1[j] and li[j] % m != 0:
			x = 1
			break
	if x == 0:
		print('YES')
	else:
		print('NO')


def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = sorted(l)
	dl = []
	if l == s:
		print('YES')
	else:
		for i in range(n):
			if l[i] != s[i]:
				dl.append(l[i])
		m = min(l)
		c = 0
		for i in dl:
			gcd = find_gcd(i, m)
			if gcd == m:
				c += 1
		if c == len(dl):
			print('YES')
		else:
			print('NO')


t = int(input())
ans = []

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
while t:
	n = int(input())
	l = list(map(int, input().split()))
	m = min(l)
	ls = sorted(l)
	c = 'YES'
	for i in range(len(l)):
		if l[i] != ls[i]:
			if gcd(m, ls[i]) != m:
				c = 'NO'
				break
	ans.append(c)
	t = t - 1
for i in ans:
	print(i)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mini = min(a)
	k = 0
	x = [-1] * (n + 1)
	y = [1000000000] * (n + 1)
	for i in range(n):
		if a[i] % mini == 0:
			x[k] = i
			y[k] = a[i]
			k += 1
	y = sorted(y)
	for i in range(k):
		a[x[i]] = y[i]
	k = 0
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			k = 1
	if k:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	brr = sorted(arr)
	k = brr[0]
	ret = 0
	for i in range(n):
		if arr[i] != brr[i] and arr[i] % k > 0:
			ret = 1
			break
	if ret:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	length = int(input())
	a = list(map(int, input().split()))
	small = min(a)
	b = []
	for i in range(length):
		if a[i] % small == 0:
			b.append(a[i])
			a[i] = 0
	if length == 1:
		print('YES')
	else:
		b.sort()
		j = 0
		k = 0
		for i in range(length):
			if a[i] == 0:
				a[i] = b[j]
				j += 1
		for i in range(1, length):
			if a[i - 1] <= a[i]:
				pass
			else:
				k = 1
				break
		if k == 0:
			print('YES')
		else:
			print('NO')


from collections import defaultdict
import sys
import math

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	a = get_array()
	b = sorted(a)
	flag = False
	k = b[0]
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % k != 0:
				flag = True
				break
	if flag:
		print('NO')
	else:
		print('YES')


import sys
readline = sys.stdin.buffer.readline

def i1():
	return int(readline())

def nl():
	return [int(s) for s in readline().split()]

def nlp(x):
	return [int(s) + x for s in readline().split()]

def nll(n):
	return [[int(s) for s in readline().split()] for i in range(n)]

def mll(n):
	return ([int(s) for s in readline().split()] for i in range(n))

def s1():
	return readline().rstrip()

def sn(n):
	return [readline().rstrip() for i in range(n)]

def sm(n):
	return (readline().rstrip() for i in range(n))

def redir(s):
	global readline
	import os
	fn = sys.argv[0] + f'/../in-{s}.txt'
	readline = open(fn).readline if os.path.exists(fn) else readline
redir('c')
t = i1()

def f(n, arr):
	arr2 = sorted(arr)
	minval = arr2[0]
	for (x, x2) in zip(arr, arr2):
		if x != x2 and x % minval != 0:
			return 'NO'
	return 'YES'
print('\n'.join((f(i1(), nl()) for _ in range(t))))


def read():
	return [int(x) for x in input().split()]
(cas,) = read()
for _ in range(cas):
	(n,) = read()
	x = read()
	m = min(x)
	xx = sorted((d for d in x if d % m == 0))
	i = 0
	r = []
	for j in range(len(x)):
		if x[j] % m == 0:
			r.append(xx[i])
			i += 1
		else:
			r.append(x[j])
	print('YES' if r == sorted(r) else 'NO')


for ti in range(int(input())):
	n = int(input())
	li = [int(i) for i in input().split()]
	sl = sorted(li)
	mn = 1145141919810
	for (p, i) in enumerate(li):
		mn = min(i, mn)
	ans = True
	for (p, i) in enumerate(li):
		if i % mn and sl[p] != i:
			ans = False
			break
	if ans:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	srr = sorted(arr)
	mini = min(arr)
	for (x, y) in zip(arr, srr):
		if x != y:
			if x % mini != 0 or y % mini != 0:
				print('NO')
				break
	else:
		print('YES')


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10 ** 8)
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=2 ** 51, func=lambda a, b: a & b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
for ik in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	w = min(l)
	e = l + []
	e.sort()
	if e == l:
		print('YES')
		continue
	s = set()
	for i in range(n):
		if l[i] == e[i]:
			continue
		else:
			s.add(l[i])
	g = 0
	s.add(w)
	for i in s:
		g = math.gcd(g, i)
	if g == w:
		print('YES')
	else:
		print('NO')


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
sys.setrecursionlimit(10 ** 6)
I = sys.stdin.readline

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def main():
	ans = ''
	for _ in range(int(ii())):
		n = ii()
		arr = li()
		if n == 1:
			ans += 'YES\n'
		else:
			tmp1 = sorted(arr)
			tmp2 = []
			for i in range(n):
				if arr[i] != tmp1[i]:
					tmp2.append(arr[i])
			if len(tmp2) == 0:
				ans += 'YES\n'
			else:
				x = min(arr)
				for i in tmp2:
					if i % x != 0:
						ans += 'NO\n'
						break
				else:
					ans += 'YES\n'
	print(ans)
main()


from math import gcd
for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	d = min(a)
	a1 = sorted(a)
	ok = True
	for i in range(n):
		if a[i] == a1[i]:
			continue
		if a[i] != a1[i] and a[i] % d != 0:
			ok = False
			break
	if ok:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = sorted(arr)
	m = min(arr)
	for x in range(n):
		if arr[x] % m != 0 and arr[x] != s[x]:
			print('NO')
			break
	else:
		print('YES')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class union_find:

	def __init__(self, n):
		self.n = n
		self.rank = [0] * n
		self.parent = [int(j) for j in range(n)]

	def union(self, i, j):
		i = self.find(i)
		j = self.find(j)
		if self.rank[i] == self.rank[j]:
			self.parent[i] = j
			self.rank[j] += 1
		elif self.rank[i] > self.rank[j]:
			self.parent[j] = i
		else:
			self.parent[i] = j

	def find(self, i):
		temp = i
		if self.parent[temp] != temp:
			self.parent[temp] = self.find(self.parent[temp])
		return self.parent[temp]
from math import log2, ceil
from collections import deque, Counter as CC

def main():
	for t in range(int(input())):
		n = int(input())
		l = [int(j) for j in input().split()]
		m = sorted(l)
		x = m[0]
		for i in range(n):
			if l[i] != m[i]:
				if l[i] % x != 0:
					print('NO')
					break
		else:
			print('YES')
main()


from math import gcd

def isSortedArrayPossible(arrayLength, array, sortedArray):
	minValue = sortedArray[0]
	for i in range(arrayLength):
		if array[i] != sortedArray[i] and array[i] % minValue:
			return 'NO'
	return 'YES'

def testCase_1401c():
	arrayLength = int(input())
	array = list(map(int, input().split(' ')))
	return (arrayLength, array, sorted(array))
nTestCases = int(input())
testCases = tuple((testCase_1401c() for x in range(nTestCases)))
tuple((print(isSortedArrayPossible(*testCase)) for testCase in testCases))


t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	y = sorted(x)
	c = 0
	mn = y[0]
	for i in range(n):
		if x[i] == y[i]:
			c += 1
		elif x[i] % mn == 0:
			c += 1
	if c == n:
		print('YES')
	else:
		print('NO')


from collections import *
import sys

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))

def computeGCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
t = ri()
for _ in range(t):
	n = ri()
	aa = rl()
	bb = []
	for (i, ai) in enumerate(aa):
		bb.append([ai, i])
	bb.sort()
	mini = bb[0][0]
	ans = 1
	for i in range(n):
		b = bb[i][0]
		a = aa[i]
		if a == b:
			continue
		if a % mini != 0:
			ans = 0
			break
		if b % mini != 0:
			ans = 0
			break
	if ans == 1:
		print('YES')
	else:
		print('NO')


import sys
read = lambda : sys.stdin.readline().strip()
write = sys.stdout.write
for t in range(int(read())):
	n = int(read())
	a = list(map(int, read().split()))
	b = sorted(a)
	min_val = min(a)
	ok = 1
	for i in range(n):
		if a[i] != b[i] and a[i] % min_val != 0:
			ok = 0
			break
	if ok:
		write('YES\n')
	else:
		write('NO\n')


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	a1 = sorted(a)
	ans = 'YES'
	mn = min(a)
	for i in range(n):
		if a[i] != a1[i]:
			if a1[i] % mn != 0:
				ans = 'No'
				break
	print(ans)
	t -= 1


a = int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for i in range(0, a):
	b = int(input())
	arr = list(map(int, input().split()))
	fgcd = min(arr)
	brr = arr[:]
	brr.sort()
	ans = []
	k = 0
	for i in range(0, len(arr)):
		if not arr[i] == brr[i] and (not arr[i] % fgcd == 0):
			k = 1
			break
	if k == 1:
		print('NO')
	else:
		print('YES')


import math
T = int(input())
for i in range(T):
	n = int(input())
	a = list(map(int, list(input().split())))
	d = False
	b = a[:]
	b.sort()
	c = set()
	for i in range(n):
		if a[i] != b[i]:
			c.add(a[i])
	if len(c) == 0:
		print('YES')
		continue
	m = min(a)
	for i in c:
		if i % m != 0:
			print('NO')
			d = True
			break
	if not d:
		print('YES')


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	l = list(map(int, stdin.readline().split()))
	sl = sorted(l)
	m = sl[0]
	z = 0
	for i in range(n):
		if sl[i] != l[i]:
			if l[i] % m != 0:
				z = 1
				break
	if z == 1:
		print('NO')
	else:
		print('YES')


import math
T = int(input())
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	if a == sorted(a):
		print('YES')
		continue
	b = sorted(a)
	m = min(a)
	f = 1
	for x in range(0, n):
		if b[x] == a[x]:
			continue
		elif b[x] % m == 0:
			continue
		else:
			f = 0
			break
	if f:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	b = sorted(a)
	f = True
	for i in range(n):
		if a[i] != b[i] and a[i] % m != 0:
			f = False
			break
	if f:
		print('YES')
	else:
		print('NO')
t = int(input())
for _ in range(t):
	solve()


import sys
I = lambda : map(int, sys.stdin.readline().split())
pr = lambda x: sys.stdout.write(f'{x}\n')

def solve():
	(n,) = I()
	a = [*I()]
	m = min(a)
	for (i, x) in enumerate(sorted(a)):
		if a[i] != x and a[i] % m:
			return 'NO'
	return 'YES'

def main():
	for _ in range(*I()):
		print(solve())
main()


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	mn = min(arr)
	setBande = []
	seen = set()
	prod = mn
	for (ind, val) in enumerate(arr):
		if val % mn == 0:
			setBande.append([val, ind])
			seen.add(ind)
	setBande.sort()
	setBande = setBande[::-1]
	ans = []
	for i in range(n):
		if i in seen:
			(val, ind) = setBande.pop()
			ans.append(val)
		else:
			ans.append(arr[i])
	ok = 'YES'
	for i in range(1, n):
		if ans[i] < ans[i - 1]:
			ok = 'NO'
			break
	print(ok)


from sys import stdin, stdout

def INPUT():
	return list((int(i) for i in stdin.readline().split()))

def inp():
	return stdin.readline()

def out(x):
	return stdout.write(x)
import math
import random
for _ in range(int(input())):
	n = int(input())
	A = INPUT()
	B = A.copy()
	B.sort()
	if A == B:
		print('YES')
		continue
	d = min(A)
	bool = True
	for i in range(n):
		if A[i] != B[i]:
			if A[i] % d != 0:
				bool = False
				break
	if bool:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	cantmove = []
	minA = min(A)
	if minA == 1:
		print('YES')
		continue
	for i in range(n):
		if A[i] % minA != 0:
			cantmove.append(i)
	B = sorted(A)
	for index in cantmove:
		if B[index] != A[index]:
			print('NO')
			break
	else:
		print('YES')


import sys
input = sys.stdin.readline
from collections import *
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	b.sort()
	m = min(a)
	l = []
	for i in range(n):
		if a[i] % m == 0:
			l.append(a[i])
			a[i] = -1
	l.sort()
	idx = 0
	for i in range(n):
		if a[i] == -1:
			a[i] = l[idx]
			idx += 1
	if a == b:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mn = min(a)
	f = True
	li = sorted(a)
	for (i, j) in zip(li, a):
		if i != j and j % mn != 0:
			f = False
	if f == True:
		print('YES')
	else:
		print('NO')


from math import gcd
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	li = list(l)
	l.sort()
	z = 10 ** 9 + 1
	fl = list()
	for i in range(n):
		if l[i] != li[i]:
			fl.append(l[i])
	if len(fl) > 0:
		m = min(l)
		g = m
		for i in fl:
			g = gcd(g, i)
		if g == m:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	x = min(a)
	for i in range(n):
		if a[i] != b[i] and a[i] % x > 0:
			print('NO')
			return
	print('YES')
for _ in range(int(input())):
	solve()


T = int(input())
for z in range(T):
	n = int(input())
	s = input()
	a = list()
	b = list()
	for e in s.split():
		a.append(int(e))
		b.append(int(e))
	b.sort()
	mini = b[0]
	control = 1
	for i in range(n):
		if a[i] != b[i] and a[i] % b[0] != 0:
			print('NO')
			control = 0
			break
	if control == 1:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	k = sorted(a)
	m = k[0]
	flag = 0
	for i in range(n):
		if a[i] != k[i]:
			if a[i] % m != 0:
				flag = 1
				print('NO')
				break
	if flag == 0:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	data = list(map(int, input().split()))
	fdata = dict()
	ndata = sorted(data)
	m = min(data)
	for i in range(n):
		if ndata[i] == data[i]:
			continue
		elif ndata[i] % m == 0 and data[i] % m == 0:
			continue
		else:
			print('NO')
			break
	else:
		print('YES')


from math import gcd
for _ in range(int(input())):
	N = int(input())
	List = [int(x) for x in input().split()]
	Sorted = List.copy()
	Sorted.sort()
	Min = Sorted[0]
	flag = 0
	for i in range(N):
		if List[i] != Sorted[i]:
			if gcd(List[i], Min) == Min:
				continue
			else:
				flag = 1
				break
	if flag:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	v = [*l]
	v.sort()
	x = v[0]
	s = 'YES'
	for i in range(n):
		if l[i] != v[i]:
			if l[i] % x != 0:
				s = 'NO'
	print(s)


cases = int(input())
for t in range(cases):
	n = int(input())
	a = list(map(int, input().split()))
	minv = min(a)
	b = sorted(a)
	f = 0
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % minv == 0 and b[i] % minv == 0:
				continue
			else:
				f = 1
				break
	if f == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	back = sorted(arr)
	if back == arr:
		print('YES')
	else:
		mi = min(arr)
		check = 1
		for j in range(n):
			if arr[j] != back[j]:
				if arr[j] % mi != 0:
					check = 0
					break
		if check == 0:
			print('NO')
		else:
			print('YES')


a1 = int(input())
while a1 > 0:
	l = int(input())
	l1 = list(map(int, input().split(' ')))
	l2 = l1.copy()
	l1.sort()
	s = l1[0]
	c = 0
	for i in range(0, l):
		if l1[i] != l2[i]:
			if l2[i] % s != 0:
				c = c + 1
	if c > 0:
		print('NO')
	else:
		print('YES')
	a1 = a1 - 1


import math
t = int(input())
for w in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	l1 = []
	l2 = [0] * n
	k = min(l)
	for i in range(n):
		if l[i] % k == 0:
			l1 += [l[i]]
	l1.sort()
	j = 0
	for i in range(n):
		if l[i] % k == 0:
			l2[i] = l1[j]
			j += 1
		else:
			l2[i] = l[i]
	c = 1
	for i in range(n - 1):
		if l2[i] > l2[i + 1]:
			c = 0
			break
	if c == 1:
		print('YES')
	else:
		print('NO')


def check(a):
	flag = False
	for i in range(len(a) - 1):
		if a[i] > a[i + 1]:
			flag = True
			break
	return flag

def ans_f(a):
	x = min(a)
	old_a = a.copy()
	a.sort()
	new_a = []
	uni = []
	for i in range(len(a)):
		if old_a[i] - a[i] != 0:
			if old_a[i] % x != 0:
				return 'NO'
	return 'YES'
t = int(input())
ans = []
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	if check(a) == False:
		ans.append('YES')
	elif min(a) == 1:
		ans.append('YES')
	else:
		ans.append(ans_f(a))
for i in ans:
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	k = min(arr)
	arrc = arr.copy()
	arrc.sort()
	count = 0
	for i in range(n):
		if arr[i] % k == 0 or arrc[i] == arr[i]:
			count += 1
	if count == n:
		print('YES')
	else:
		print('NO')


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	lol = min(a)
	flag = False
	for i in range(n):
		if a[i] % lol != 0 and b[i] != a[i]:
			flag = True
			break
	if flag:
		print('NO')
	else:
		print('YES')


import sys
from math import sqrt
from collections import OrderedDict
inpt = sys.stdin.readline

def inp():
	return int(inpt())

def inlist():
	return list(map(int, inpt().split()))

def instr():
	s = inpt()
	return list(s[:len(s) - 1])

def invr():
	return map(int, inpt().split())

class node(object):

	def __init__(self, x, p, h):
		(self.x, self.p, self.h) = (x, p, h)
		self.children = []

	def add_child(self, obj):
		self.children.append(obj)

def main():
	t = inp()
	for l in range(t):
		n = inp()
		a = inlist()
		g = min(a)
		flag = True
		srt = sorted(a)
		nw = []
		for i in range(n):
			if a[i] % g == 0:
				nw.append(a[i])
		nw = sorted(nw)
		l = 0
		for i in range(n):
			if a[i] % g == 0:
				a[i] = nw[l]
				l += 1
		if a == srt:
			print('YES')
		else:
			print('NO')
main()


from sys import stdin, stdout
from math import gcd
test = int(stdin.readline())
for _ in range(test):
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	b = sorted(a)
	g = 0
	ha = min(a)
	if b == a:
		print('YES')
		continue
	ok = True
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % ha:
				ok = False
	if not ok:
		print('NO')
		continue
	print('YES')


import sys
read = lambda : sys.stdin.readline().strip()
for _ in range(int(read())):
	n = int(read())
	nums = list(map(int, read().split()))
	sorted_nums = sorted(nums)
	min_num = sorted_nums[0]
	for i in range(n):
		if nums[i] != sorted_nums[i]:
			if nums[i] % min_num != 0:
				print('NO')
				break
	else:
		print('YES')


import sys
input = sys.stdin.readline

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	mins = min(arr)
	persuit = sorted(arr)
	for i in range(n):
		if arr[i] != persuit[i]:
			if gcd(arr[i], mins) != mins:
				print('NO')
				break
	else:
		print('YES')


def sol():
	z1 = int(input())
	arr = list(map(int, input().split()))
	mini = min(arr)
	li = sorted(arr)
	for (i, j) in zip(li, arr):
		if i != j and j % mini != 0:
			return 'NO'
	return 'YES'
for _ in range(int(input())):
	print(sol())


t = int(input())
for i in range(0, t):
	n = int(input())
	arr = list(map(int, input().split()))
	arrSorted = sorted(arr)
	a = arrSorted[0]
	possible = True
	for j in range(0, len(arr)):
		if arr[j] != arrSorted[j]:
			if arr[j] % a != 0:
				possible = False
				break
	if possible:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	num = lis[:]
	num.sort()
	mi = num[0]
	f1 = 0
	for i in range(n - 1, -1, -1):
		if num[i] != lis[i]:
			if lis[i] % mi != 0:
				f1 = 1
				break
	if f1 == 0:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
from bisect import bisect_left
from bisect import bisect_right
from math import ceil, factorial

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')

def mine(n, a):
	f = 1
	x = min(a)
	b = sorted(a)
	this = []
	for i in range(n):
		if a[i] != b[i]:
			this.append(b[i])
	yo = x
	for i in this:
		yo = gcd(yo, i)
	if yo == x:
		return 'YES'
	else:
		return 'NO'
for _ in range(int(input())):
	n = int(input())
	a = int_array()
	print(mine(n, a))


Ans = []
t = int(input())
for i in range(t):
	len_a = int(input())
	a = list(map(int, input().split()))
	a1 = min(a)
	d = dict(enumerate(a))
	ind = []
	elem = []
	for k in range(len_a):
		if d[k] % a1 == 0:
			ind.append(k)
			elem.append(d[k])
	elem.sort()
	for j in range(len(ind)):
		d[ind[j]] = elem[j]
	result = [d[k] for k in range(len_a)]
	a.sort()
	if result == a:
		Ans.append('YES')
	else:
		Ans.append('NO')
for i in Ans:
	print(i)


import io
import os
import sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def prdbg(*args, **kwargs):
	print(*args, **kwargs, file=sys.stderr)
	pass

def get_str():
	return input().decode().strip()

def rint():
	return map(int, input().split())

def oint():
	return int(input())
t = oint()
for _ in range(t):
	n = oint()
	a = list(rint())
	mina = min(a)
	nai = []
	ai = []
	for i in range(n):
		if a[i] % mina:
			nai.append(i)
		else:
			ai.append(i)
	ai.sort(key=lambda i: a[i])
	aa = [0] * n
	for i in range(len(nai)):
		aa[nai[i]] = a[nai[i]]
	ii = 0
	for i in range(n):
		if aa[i] == 0:
			aa[i] = a[ai[ii]]
			ii += 1
	prev = aa[0]
	flag = 0
	for i in range(1, n):
		if aa[i] >= prev:
			prev = aa[i]
		else:
			flag = 1
			break
	if flag == 0:
		print('YES')
	else:
		print('NO')


from bisect import bisect_left, bisect_right
from itertools import combinations
from itertools import permutations
from bisect import bisect_left
from math import ceil
from math import radians
from heapq import heapify, heappush, heappop
import bisect
from math import pi
from collections import deque
from math import factorial
from math import log, ceil
from collections import defaultdict
from math import *
from sys import stdin, stdout
import itertools
import os
import sys
import threading
from collections import deque, Counter, OrderedDict, defaultdict
from heapq import *
from fractions import Fraction
mod = int(pow(10, 9) + 7)

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li1():
	return list(mi())

def fii():
	return int(stdin.readline())

def fsi():
	return str(stdin.readline())

def fmi():
	return map(int, stdin.readline().split())

def fli():
	return list(fmi())
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d:
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d:
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = defaultdict(list)
	for i in range(n):
		(x, y) = mi()
		d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans

def primeFactors(n):
	s = set()
	while n % 2 == 0:
		s.add(2)
		n = n // 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0 and i % 2 == 1:
			s.add(i)
			n = n // i
	if n > 2 and n % 2 == 1:
		s.add(n)
	return s

def find_all(a_str, sub):
	start = 0
	while True:
		start = a_str.find(sub, start)
		if start == -1:
			return
		yield start
		start += len(sub)

def SieveOfEratosthenes(n, isPrime):
	isPrime[0] = isPrime[1] = False
	for i in range(2, n):
		isPrime[i] = True
	p = 2
	while p * p <= n:
		if isPrime[p] == True:
			i = p * p
			while i <= n:
				isPrime[i] = False
				i += p
		p += 1
	return isPrime

def dijkstra(edges, f, t):
	g = defaultdict(list)
	for (l, r, c) in edges:
		g[l].append((c, r))
	(q, seen, mins) = ([(0, f, ())], set(), {f: 0})
	while q:
		(cost, v1, path) = heappop(q)
		if v1 not in seen:
			seen.add(v1)
			path = (v1, path)
			if v1 == t:
				return (cost, path)
			for (c, v2) in g.get(v1, ()):
				if v2 in seen:
					continue
				prev = mins.get(v2, None)
				next = cost + c
				if prev is None or next < prev:
					mins[v2] = next
					heappush(q, (next, v2, path))
	return float('inf')

def binsearch(a, l, r, x):
	while l <= r:
		mid = l + (r - 1) // 2
		if a[mid]:
			return mid
		elif a[mid] > x:
			l = mid - 1
		else:
			r = mid + 1
	return -1

def readTree(n):
	adj = [set() for _ in range(n)]
	for _ in range(n - 1):
		(u, v) = map(int, input().split())
		adj[u - 1].add(v - 1)
		adj[v - 1].add(u - 1)
	return adj

def treeOrderByDepth(n, adj, root=0):
	parent = [-2] + [-1] * (n - 1)
	ordered = []
	q = deque()
	q.append(root)
	depth = [0] * n
	while q:
		c = q.popleft()
		ordered.append(c)
		for a in adj[c]:
			if parent[a] == -1:
				parent[a] = c
				depth[a] = depth[c] + 1
				q.append(a)
	return (ordered, parent, depth)

def isSubSequence(str1, str2):
	m = len(str1)
	n = len(str2)
	j = 0
	i = 0
	while j < m and i < n:
		if str1[j] == str2[i]:
			j = j + 1
		i = i + 1
	return j == m

def nextPowerOf2(n):
	count = 0
	if n and (not n & n - 1):
		return n
	while n != 0:
		n >>= 1
		count += 1
	return 1 << count

def cou(n):
	c = 0
	while n > 1:
		c += 1
		n //= 2
	return c

def sortsec(l):
	return sorted(l, key=lambda x: x[1])

def BinarySearch(a, x):
	i = bisect_left(a, x)
	if i:
		return i - 1
	else:
		return -1

def subarray(A):
	r = set()
	p = {0}
	for x in A:
		p = {x | y for y in p} | {x}
		r |= p
	return len(r)
for _ in range(ii()):
	n = ii()
	l = li1()
	if n == 1:
		print('YES')
		continue
	s = sorted(l)
	if 1 in l:
		print('YES')
		continue
	for i in range(n):
		if gcd(s[0], l[i]) != s[0] and s[i] != l[i]:
			print('NO')
			break
	else:
		print('YES')


MOD = 1000000007
from collections import defaultdict as dd, Counter, deque

def si():
	return input()

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def mi():
	return map(int, input().split())

def out(v):
	print(v)

def spout():
	print(v, end=' ')

def d2b(n):
	return bin(n).replace('0b', '')

def twod(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def vow():
	return ['a', 'e', 'i', 'o', 'u']

def let():
	return [chr(i) for i in range(97, 123)]

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def ispow2(x):
	return x and (not x & x - 1)

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)
t = ii()
while t:
	t -= 1
	f = 0
	n = ii()
	a = li()
	b = sorted(a)
	for i in range(n):
		if a[i] != b[i] and a[i] % b[0] != 0:
			print('NO')
			f = 1
			break
	if f == 0:
		print('YES')


t = int(input().strip(' '))
for _ in range(t):
	n = int(input().strip(' '))
	a = list(map(int, input().strip(' ').split(' ')))
	m = min(a)
	sorta = sorted(a)
	for i in range(n):
		if a[i] != sorta[i]:
			if a[i] % m != 0 or sorta[i] % m != 0:
				print('NO')
				break
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	flag = 0
	mini = b[0]
	for i in range(n):
		if a[i] != b[i]:
			if b[i] % mini != 0:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	index1 = []
	index2 = {}
	k = min(arr)
	new = sorted(arr)
	for i in range(len(arr)):
		if arr[i] != new[i]:
			index1.append(i)
		if arr[i] % k == 0:
			index2[i] = 0
	temp = True
	for i in index1:
		if i not in index2:
			temp = False
			break
	if temp:
		print('YES')
	else:
		print('NO')


t = int(input())
for tt in range(t):
	n = int(input())
	a = input().split(' ')
	a = list(map(int, a))
	am = int(1000000000.0) + 1
	for i in range(n):
		if a[i] < am:
			am = a[i]
	sa = sorted(a)
	f = int(1)
	for i in range(n):
		if a[i] % am == 0 or a[i] == sa[i]:
			continue
		else:
			f = 0
	if f:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = l.copy()
	p.sort()
	m = min(l)
	c = 0
	i = 0
	for e in l:
		if e % m == 0:
			c += 1
		elif p[i] == e:
			c += 1
		i += 1
	if c == n:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(q, t) = (sorted(a), 1)
	for i in range(n):
		if a[i] != q[i] and a[i] % q[0]:
			t = 0
			break
	print('YES' if t else 'NO')


t = int(input())
while t:
	t += -1
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	for i in l:
		l1.append(i)
	l1.sort()
	ind = []
	for i in range(n):
		if l[i] != l1[i]:
			ind.append(i)
	mn = min(l)
	check = 1
	for i in ind:
		if l[i] % mn:
			check = 0
			break
	if check:
		print('YES')
	else:
		print('NO')


t = int(input())

def gcd(a, b):
	if b == 0:
		return a
	if a == 0:
		return b
	return gcd(b, a % b)
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	b.sort()
	l = []
	for i in range(n):
		if a[i] == b[i]:
			continue
		else:
			l.append(a[i])
	if len(l) == 0:
		print('YES')
		continue
	req = b[0]
	g = l[0]
	for i in range(1, len(l)):
		g = gcd(l[i], g)
	if g == req or gcd(g, req) == req:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	c = [0] * n
	m = min(a)
	for i in range(n):
		if a[i] % m == 0:
			c[i] = 1
			b.append(a[i])
	b.sort(reverse=True)
	for i in range(n):
		if c[i] == 1:
			a[i] = b.pop()
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			print('NO')
			break
	else:
		print('YES')


import heapq as hq
from heapq import heappop, heappush
from collections import deque, defaultdict, Counter
from sys import stdin, stdout

def inp():
	return int(stdin.readline())

def inlt():
	return list(map(int, stdin.readline().split()))

def insr():
	return list(stdin.readline().strip())

def invr():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def subset_sum_count(arr, n, sum):
	dp = [[0 for _ in range(sum + 1)] for _ in range(n + 1)]
	for i in range(n + 1):
		for j in range(sum + 1):
			if j == 0:
				dp[i][j] = 1
			elif arr[i - 1] <= j:
				dp[i][j] = dp[i - 1][j - arr[i - 1]] + dp[i - 1][j]
			else:
				dp[i][j] = dp[i - 1][j]
	return dp[n][sum]

def prefix(a):
	pre = []
	pre.append(a[0])
	for i in range(1, len(a)):
		pre.append(pre[i - 1] + a[i])
	return pre

def binary_search(func, lo, hi, abs_prec=1e-07):
	while abs(hi - lo) > abs_prec:
		mi = lo + (hi - lo) / 2
		if func(mi):
			hi = mi
		else:
			lo = mi
	return (lo + hi) / 2

def ternary_search(func, lo, hi, abs_prec=1e-07):
	while abs(hi - lo) > abs_prec:
		lo_third = lo + (hi - lo) / 3
		hi_third = hi - (hi - lo) / 3
		if func(lo_third) < func(hi_third):
			lo = lo_third
		else:
			hi = hi_third
	return (lo + hi) / 2

def discrete_binary_search(func, lo, hi):
	while lo < hi:
		mi = lo + (hi - lo) // 2
		if func(mi):
			hi = mi
		else:
			lo = mi + 1
	return lo

def discrete_ternary_search(func, lo, hi):
	while lo <= hi:
		lo_third = lo + (hi - lo) // 3
		hi_third = lo + (hi - lo) // 3 + (1 if 0 < hi - lo < 3 else (hi - lo) // 3)
		if func(lo_third) < func(hi_third):
			lo = lo_third + 1
		else:
			hi = hi_third - 1
	return lo
	return -1

def right_rotate(a, s):
	return a[s:] + a[:s]

def dec_to_bin(x):
	return int(bin(x)[2:])

def str_to_integer_list(n):
	a = []
	for i in range(len(n)):
		a.append(int(n[i]))
	return a

def list_to_str(l):
	s = ''
	for i in l:
		s += str(i)
	return s

def dijkstra(s, N, E):
	visited = set()
	dist = {}
	for i in range(1, N + 1):
		dist[i] = 1 << 29
	queue = [(dist[i], i) for i in range(1, N + 1)]
	hq.heappush(queue, (0, s))
	dist[s] = 0
	while queue:
		(d, u) = hq.heappop(queue)
		if u in visited:
			continue
		for t in E[u]:
			(v, r) = t
			if dist[v] > d + r:
				dist[v] = d + r
				hq.heappush(queue, (dist[v], v))
		visited.add(u)
	return dist

def prime_sieve(n):
	flag = n % 6 == 2
	sieve = bytearray((n // 3 + flag >> 3) + 1)
	for i in range(1, int(n ** 0.5) // 3 + 1):
		if not sieve[i >> 3] >> (i & 7) & 1:
			k = 3 * i + 1 | 1
			for j in range(k * k // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
			for j in range(k * (k - 2 * (i & 1) + 4) // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
	return sieve

def prime_list(n):
	res = []
	if n > 1:
		res.append(2)
	if n > 2:
		res.append(3)
	if n > 4:
		sieve = prime_sieve(n + 1)
		res.extend((3 * i + 1 | 1 for i in range(1, (n + 1) // 3 + (n % 6 == 1)) if not sieve[i >> 3] >> (i & 7) & 1))
	return res

def dijkstra(n, graph, start):
	(dist, parents) = ([float('inf')] * n, [-1] * n)
	dist[start] = 0
	queue = [(0, start)]
	while queue:
		(path_len, v) = heappop(queue)
		if path_len == dist[v]:
			for (w, edge_len) in graph[v]:
				if edge_len + path_len < dist[w]:
					(dist[w], parents[w]) = (edge_len + path_len, v)
					heappush(queue, (edge_len + path_len, w))
	return (dist, parents)

def path(start, end, parent):
	path = [end]
	while path[-1] != start:
		path.append(parent[path[-1]])
	path.reverse()
	return path

def bfs(graph, start, end):
	parent = {}
	queue = []
	queue.append(start)
	while queue:
		node = queue.pop(0)
		if node == end:
			return path(start, end, parent)
		for next in graph.get(node, []):
			if node not in queue:
				parent[next] = node
				queue.append(next)

def most_frequent(List):
	return max(set(List), key=List.count)

def subarray(arr, n):
	(ans, temp) = (1, 1)
	for i in range(1, n):
		if arr[i] == arr[i - 1]:
			temp = temp + 1
		else:
			ans = max(ans, temp)
			temp = 1
	ans = max(ans, temp)
	return ans

def solve(n):
	l = []
	while n != 0:
		if n % 10 != 7 and n % 10 != 4:
			return False
	return True

def lucky(n):
	l = []
	while n > 0:
		x = n % 10
		l.append(x)
		n = n // 10
	if l.count(4) + l.count(7) == len(l):
		return True
	else:
		return False

def solve():
	for _ in range(inp()):
		n = inp()
		a = inlt()
		x = min(a)
		b = sorted(a)
		flag = 0
		for i in range(n):
			if a[i] != b[i] and a[i] % x != 0:
				flag = 1
				break
		if flag == 1:
			print('NO')
		else:
			print('YES')
solve()


T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	N = n
	B = [0 for i in range(n + 1)]
	v = min(A)
	for i in range(N):
		if A[i] % v == 0:
			B[i] = 1
	C = []
	for i in range(N):
		C.append([A[i], i])
	C.sort()
	flag = 0
	for i in range(N):
		if C[i][1] != i and B[i] == 0:
			flag = 1
			break
	if flag == 0:
		print('YES')
	else:
		print('NO')


def solve(n, a):
	d = []
	m = min(a)
	for i in range(n):
		if a[i] % m == 0:
			d.append(a[i])
			a[i] = -1
	d.sort(reverse=True)
	for i in range(n):
		if a[i] == -1:
			a[i] = d.pop()
	for i in range(1, n):
		if a[i - 1] > a[i]:
			return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print('YES' if solve(n, a) else 'NO')


t = int(input())
for k in range(t):
	n = int(input())
	arr = list(map(int, input().rstrip().split()))
	arr2 = sorted(arr)
	min_e = min(arr)
	bool = True
	for i in range(n):
		if arr[i] == arr2[i] or arr[i] % min_e == 0:
			continue
		else:
			bool = False
			break
	if bool == False:
		print('NO')
	else:
		print('YES')


from math import log2, ceil, floor, gcd
from collections import defaultdict, OrderedDict, Counter, deque
from heapq import heappush, heappop, heapify, nlargest, nsmallest
import re

def inp():
	return input()

def list_inp():
	return list(map(int, input().split()))

def map_inp():
	return map(int, input().split())

def int_inp():
	return int(input())

def float_int():
	return float(input())

def isprime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for _ in range(int_inp()):
	n = int_inp()
	arr = list_inp()
	m = min(arr)
	f = 0
	sort_arr = sorted(arr)
	l = []
	for i in range(n):
		if arr[i] != sort_arr[i]:
			l.append(arr[i])
	gc = m
	for i in range(len(l)):
		gc = gcd(gc, l[i])
	if gc == m:
		print('YES')
	else:
		print('NO')


from __future__ import division, print_function
import os, sys
import math
import collections
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

class my_dictionary(dict):

	def __init__(self):
		self = dict()

	def add(self, key, value):
		self[key] = value

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(100000000)
	threading.stack_size(40960000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[1]

def sort2(l):
	return sorted(l, key=getKey, reverse=True)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(n - r))

def binary_search(arr, low, high, x):
	if high >= low:
		mid = (high + low) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binary_search(arr, low, mid - 1, x)
		else:
			return binary_search(arr, mid + 1, high, x)
	else:
		return -1

def ceil(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def padded_bin_with_complement(x):
	if x < 0:
		return bin(2 ** 16 - abs(x))[2:].zfill(16)
	else:
		return bin(x)[2:].zfill(16)

def binaryToDecimal(binary):
	binary1 = binary
	(decimal, i, n) = (0, 0, 0)
	while binary != 0:
		dec = binary % 10
		decimal = decimal + dec * pow(2, i)
		binary = binary // 10
		i += 1
	print(decimal)

def CountFrequency(my_list):
	freq = {}
	for item in my_list:
		if item in freq:
			freq[item] += 1
		else:
			freq[item] = 1
	return freq

def pos(a):
	b = [0] * len(a)
	c = sorted(a)
	for i in range(len(a)):
		for j in range(len(a)):
			if c[j] == a[i]:
				b[i] = j
				break
	return b

def smallestDivisor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			return i
		i += 2
	return n

def commonn(a, b, n):
	c = []
	for i in range(n):
		if a[i] == b[i]:
			c.append('-1')
		else:
			c.append(b[i])
	return c

def primeFactors(n):
	j = []
	while n % 2 == 0:
		j.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			j.append(int(n))
			n = n / i
	if n > 2:
		j.append(int(n))
	return j

def sumdigit(n):
	n = str(n)
	k = 0
	for i in range(len(n)):
		k += int(n[i])
	return k

def main():
	for _ in range(ii()):
		n = ii()
		a = li()
		e = []
		b = []
		f = 1
		c = sorted(a)
		minn = min(a)
		d = [0] * n
		for i in range(n):
			if a[i] != c[i]:
				if gcd(a[i], minn) != minn:
					f = 0
		if f:
			print('YES')
		else:
			print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def answer(n, A):
	ele = min(A)
	dp = [0] * n
	for i in range(n):
		if A[i] % ele == 0:
			dp[i] = 1
	l = sorted(A)
	for i in range(n):
		if dp[i] == 0:
			if l[i] != A[i]:
				return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(answer(n, arr))


testcase = int(input())
for t in range(testcase):
	n = int(input())
	arr = list(map(int, input().split()))
	final = []
	for i in arr:
		final.append(i)
	final.sort()
	misplaced = []
	m = min(arr)
	for i in range(n):
		if arr[i] != final[i]:
			misplaced.append(arr[i])
	ans = 1
	for i in misplaced:
		if i % m != 0:
			ans = 0
			break
	if ans == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	b = sorted(b)
	mn = min(a)
	flg = 1
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % mn:
				flg = 0
	if flg:
		print('YES')
	else:
		print('NO')


from math import gcd
for _ in range(int(input())):
	n = int(input())
	ai = list(map(int, input().split()))
	aic = sorted(ai)
	gcd_val = aic[0]
	updated = aic[0]
	for i in range(n):
		if ai[i] != aic[i]:
			updated = gcd(updated, ai[i])
	print('YES') if updated % gcd_val == 0 else print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	mi = min(l)
	x = l.copy()
	x.sort()
	for j in range(n):
		if x[j] != l[j]:
			if l[j] % mi != 0:
				flag = 1
				break
	if flag == 0:
		print('YES')
	else:
		print('NO')


def is_poss(n, a):
	m = min(a)
	b = sorted(a)
	for (c, el) in enumerate(a):
		if el % m == 0 or el == b[c]:
			continue
		return False
	return True

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		if is_poss(n, a):
			print('YES')
		else:
			print('NO')
main()


from math import gcd
t = int(input())
for _ in range(t):
	n = int(input())
	(*a,) = map(int, input().split())
	am = min(a)
	a1 = [ai for ai in a if ai % am == 0]
	a1.sort(reverse=True)
	a2 = [ai for ai in a if ai % am]
	a2.reverse()
	ax = []
	for i in range(n):
		if a[i] % am == 0:
			ax.append(a1.pop())
		else:
			ax.append(a2.pop())
	ans = 'YES'
	for i in range(n - 1):
		if ax[i] > ax[i + 1]:
			ans = 'NO'
			break
	print(ans)


def check(a, b, n):
	m = min(a)
	for i in range(n):
		if a[i] != b[i] and a[i] % m:
			return 0
	return 1
for z in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	if check(a, b, n):
		print('YES')
	else:
		print('NO')


from math import gcd

def answer(n, s):
	p = sorted(s)
	Min = min(s)
	for i in range(n):
		if gcd(Min, s[i]) != Min and s[i] != p[i]:
			return 'NO'
	return 'YES'
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	print(answer(n, s))


tst = int(input())
while tst > 0:
	tst = tst - 1
	a = int(input())
	ls = list()
	nmbr = input()
	nmbr = nmbr.split()
	for i in range(len(nmbr)):
		n = int(nmbr[i])
		ls.append(n)
	st = sorted(ls)
	mn = st[0]
	for i in range(len(ls)):
		if ls[i] % mn == 0:
			ls[i] = -1
	skip = True
	for i in range(len(ls)):
		if ls[i] != -1 and ls[i] != st[i]:
			skip = False
	if skip == False:
		print('NO')
	else:
		print('Yes')


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	lis = list(map(int, input().split()))
	m = min(lis)
	store = []
	pos = []
	for i in range(n):
		if lis[i] % m == 0:
			store.append(lis[i])
			pos.append(i)
	store.sort()
	for i in range(len(pos)):
		lis[pos[i]] = store[i]
	g = 0
	for i in range(n - 1):
		if lis[i] > lis[i + 1]:
			g = 1
			break
	if g:
		print('NO')
	else:
		print('YES')


from math import gcd
for _ in range(int(input())):
	x = int(input())
	inp = list(map(int, input().split()))
	mn = min(inp)
	nls = sorted(inp.copy())
	flag = 1
	for i in range(x):
		if nls[i] == inp[i]:
			continue
		if gcd(nls[i], inp[i]) % mn == 0:
			continue
		flag = 0
		break
	if flag == 1:
		print('YES')
	else:
		print('NO')


import sys

def main():
	T = int(sys.stdin.readline())
	while T:
		T -= 1
		N = int(sys.stdin.readline())
		A = [int(i) for i in sys.stdin.readline().rstrip().split()]
		min_element = min(A)
		A_sorted = sorted(A)
		FLAG = True
		for (i, j) in zip(A, A_sorted):
			if i == j:
				continue
			if GCD(i, min_element) != min_element or GCD(j, min_element) != min_element:
				FLAG = False
				break
		if FLAG:
			print('YES')
		else:
			print('NO')

def GCD(i, j):
	(i, j) = (max(i, j), min(i, j))

	def __gcd(i, j):
		if j == 0:
			return i
		return __gcd(j, i % j)
	return __gcd(i, j)
main()


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def solve():
	for _ in range(ii()):
		n = ii()
		a = li()
		mnx = min(a)
		m = {}
		x1 = []
		s = set()
		for i in range(n):
			if a[i] % mnx == 0:
				m[i] = 1
			x1.append([a[i], i])
		x1.sort()
		for i in range(n):
			if i != x1[i][1]:
				s.add(i)
				s.add(x1[i][1])
		x2 = list(s)
		f = 0
		for i in x2:
			if i not in m:
				f = 1
				break
		if f:
			print('NO')
		else:
			print('YES')
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a_copy = a[:]
	a_copy.sort()
	m = a_copy[0]
	i = 0
	while i < len(a):
		if a[i] != a_copy[i]:
			if a[i] % m == 0:
				i = i + 1
			else:
				break
		else:
			i = i + 1
	if i == len(a):
		print('YES')
	else:
		print('NO')


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
t = int(input())
for ii in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	m = min(a)
	ans = True
	for i in range(n):
		if a[i] != b[i]:
			v = gcd(a[i], b[i])
			if v % m != 0:
				ans = False
				break
	if ans:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	minimum = 1000000000.0
	count = 0
	li2 = []
	x = int(input())
	li = [int(x) for x in input().split()]
	for j in li:
		li2.append(j)
		minimum = min(minimum, j)
	li.sort()
	for k in range(x):
		if li[k] != li2[k] and li[k] % minimum > 0:
			count = 1
	if count:
		print('NO')
	else:
		print('YES')


import sys

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mini = min(a)
	sorta = sorted(a)
	if sorta == a:
		print('YES')
		continue
	c1 = 0
	c2 = 0
	for i in range(n):
		if a[i] != sorta[i]:
			c1 += 1
			gg = gcd(mini, a[i])
			if gg == mini:
				c2 += 1
	if c1 == c2:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	_min = min(a)
	d = []
	ids = []
	for i in range(len(a)):
		if a[i] % _min == 0:
			d.append(a[i])
			ids.append(i)
	d.sort()
	for i in range(len(d)):
		a[ids[i]] = d[i]
	for i in range(1, len(a)):
		if a[i - 1] > a[i]:
			print('NO')
			break
	else:
		print('YES')


from math import gcd
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	g = min(a)
	b = sorted(a)
	d = g
	for i in range(n):
		if a[i] != b[i]:
			d = gcd(d, a[i])
	if d == g:
		print('YES', flush=False)
	else:
		print('NO', flush=False)


for _ in range(int(input())):
	n = int(input())
	arr = [int(num) for num in input().split(' ')]
	arr2 = arr.copy()
	arr2.sort()
	a = arr2[0]
	ans = True
	for i in range(n):
		if arr[i] % a != 0:
			if arr[i] != arr2[i]:
				ans = False
				break
	if ans == True:
		print('Yes')
	else:
		print('No')


import sys

def main():
	tn = int(input())
	for ti in range(tn):
		n = int(input())
		A = [int(x) for x in input().split()]
		m = min(A)
		B = sorted(A)
		ans = True
		for (a, b) in zip(A, B):
			if a == b:
				continue
			if b % m != 0:
				ans = False
		print('YES' if ans else 'NO')
main()


t = int(input())
while t > 0:
	n = int(input())
	arr = [int(i) for i in input().split()]
	li = [arr[i] for i in range(n)]
	arr.sort()
	ans = 'YES'
	for i in range(n):
		if li[i] % arr[0] != 0:
			if li[i] != arr[i]:
				ans = 'NO'
				break
	print(ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	b = sorted(arr)
	flag = 0
	mini = b[0]
	for i in range(n):
		if arr[i] != b[i]:
			if b[i] % mini != 0:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


from collections import *
from functools import *
import math
import sys
inp = sys.stdin.readline
out = sys.stdout.write

def mi():
	return map(int, inp().split())

def li():
	return list(mi())

def ii():
	return int(inp())

def main():
	n = ii()
	a = li()
	mi = min(a)
	b = sorted(a)
	if a == b:
		return 'YES'
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % mi != 0:
				return 'NO'
	else:
		return 'YES'
for _ in range(ii()):
	print(main())


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = l[:]
	l1.sort()
	ans = 'YES'
	a = min(l)
	flag = 0
	for i in range(n):
		if l1[i] != l[i]:
			if l[i] % a != 0:
				flag = 1
	if flag == 1:
		print('NO')
	else:
		print('YES')


import math
t = int(input())
for h in range(t):
	n = int(input())
	lis1 = list(map(int, input().split()))
	s = lis1[:]
	s.sort()
	m = min(lis1)
	k = 0
	for i in range(n):
		if math.gcd(m, lis1[i]) != m:
			if s[i] != lis1[i]:
				print('NO')
				k = 1
				break
	if k == 0:
		print('YES')


import math

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))

def solve():
	n = Read.int()
	a = Read.list_int()
	sa = sorted(a)
	m = sa[0]
	for i in range(n):
		if sa[i] != a[i]:
			if a[i] % m != 0 or sa[i] % m != 0:
				print('NO')
				return
	print('YES')
query_count = Read.int()
while query_count:
	query_count -= 1
	solve()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	impflag = 0
	arr = []
	val = min(a)
	for i in range(n):
		if a[i] != b[i]:
			arr.append(b[i])
	for i in arr:
		if i % val != 0:
			impflag = 1
			break
	if impflag == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	sl = sorted(l)
	mini = sl[0]
	f = 0
	for j in range(n):
		if l[j] != sl[j] and l[j] % mini != 0:
			f = 1
			break
	if f == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	m = min(arr)
	(ind, val) = ([], [])
	for i in range(len(arr)):
		if arr[i] % m == 0:
			val.append(arr[i])
			ind.append(i)
	val.sort()
	for i in range(len(val)):
		arr[ind[i]] = val[i]
	st = True
	for i in range(len(arr) - 1):
		if arr[i] > arr[i + 1]:
			st = False
			break
	if st:
		print('YES')
	else:
		print('NO')
	t -= 1


T = int(input())
while T > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	x = min(arr)
	index = []
	ele = []
	fit = True
	for i in range(n):
		if arr[i] % x != 0:
			if len(index) > 0 and arr[i] < arr[index[-1]]:
				fit = False
			index.append(i)
		else:
			ele.append(arr[i])
	if fit is False:
		print('NO')
	elif len(index) == 0:
		print('YES')
	else:
		ele.sort()
		j = 0
		now = 0
		while j < len(ele):
			start = j
			while j < len(ele) and ele[j] < arr[index[now]]:
				j += 1
			count = j - start
			if now == 0:
				need = index[0]
			else:
				need = index[now] - index[now - 1] - 1
			if count != need:
				fit = False
				break
			now += 1
			if now == len(index):
				break
		if fit is True:
			print('YES')
		else:
			print('NO')
	T -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	sor = sorted(arr)
	if arr == sor:
		print('YES')
	else:
		mi = sor[0]
		flag = 0
		for i in range(n):
			if arr[i] != sor[i] and arr[i] % mi > 0:
				flag = 1
				break
		if flag == 1:
			print('NO')
		else:
			print('YES')


t = int(input())
for oo in range(t):
	n = int(input())
	a = [int(xx) for xx in input().split()]
	m = min(a)
	e = True
	w = -1
	niz = []
	b = []
	for i in range(n):
		c = a[i]
		if c % m == 0:
			niz.append(c)
			b.append(i)
	niz.sort()
	lent = len(b)
	for i in range(lent):
		j = b[i]
		q = niz[i]
		a[j] = q
	for i in range(1, n):
		if a[i] < a[i - 1]:
			e = False
			break
	if e:
		print('YES')
	else:
		print('NO')


from math import gcd
iter = int(input())
for _ in range(iter):
	n = int(input())
	a = list(map(int, input().split(' ')))
	aso = sorted(a)
	m = min(a)
	ans = 1
	for i in range(n):
		if a[i] != aso[i]:
			if a[i] % m or aso[i] % m:
				ans = 0
	if ans:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()][:n]
	l1 = sorted(l)
	gcd = min(l)
	for i in range(n):
		if l[i] != l1[i]:
			if l[i] % gcd != 0 or l1[i] % gcd != 0:
				print('NO')
				break
	else:
		print('YES')


from sys import stdin, stdout

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def find(arr, N):
	k = sorted(arr)
	m = min(arr)
	for i in range(len(arr)):
		if arr[i] != k[i] and (not gcd(arr[i], m) == gcd(m, k[i]) == m):
			return 'No'
	return 'YES'

def main():
	for _ in range(int(stdin.readline())):
		N = int(stdin.readline())
		arr = list(map(int, stdin.readline().split()))
		print(find(arr, N))
main()


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	b = sorted(a)
	ch = 1
	for i in range(n):
		if a[i] != b[i]:
			if math.gcd(a[i], m) != m:
				ch = 0
				break
	if ch == 1:
		print('YES')
	else:
		print('NO')


import math

def getArrayInt():
	inp = input().split()
	inp = [int(x) for x in inp]
	return inp
t = int(input())
mn = 1
while mn <= t:
	n = int(input())
	inputs = getArrayInt()
	sInput = sorted(inputs)
	res = ''
	for i in range(n):
		if inputs[i] == sInput[i]:
			continue
		elif inputs[i] % sInput[0] != 0:
			res = 'NO'
			break
	if res:
		print(res)
	else:
		print('YES')
	mn += 1


import math
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	asort = list(a)
	asort.sort()
	gcd = asort[0]
	ans = True
	for i in range(n):
		if a[i] != asort[i] and math.gcd(a[i], gcd) != gcd:
			ans = False
	if ans:
		print('YES')
	else:
		print('NO')


test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	arrs = sorted(arr)
	mis = []
	mini = arrs[0]
	for i in range(n):
		if arr[i] != arrs[i]:
			mis.append(arr[i])
	flag = True
	for i in range(len(mis)):
		if mis[i] % mini:
			flag = False
			break
	if flag:
		print('YES')
	else:
		print('NO')


from math import *
from sys import *
t = int(stdin.readline().strip())
for _ in range(t):
	n = int(stdin.readline().strip())
	a = list(map(int, stdin.readline().strip().split()))
	r = a.copy()
	r.sort()
	m = min(a)
	if m == 1:
		print('YES')
		continue
	l = []
	for i in range(n):
		k = a[i] % m
		if k == 0:
			l.append(0)
		else:
			l.append(1)
	for i in range(n):
		if l[i] == 1:
			if r[i] == a[i]:
				pass
			else:
				print('NO')
				break
	else:
		print('YES')


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	m = b[0]
	flag = True
	for i in range(n):
		if a[i] != b[i] and math.gcd(a[i], m) != m:
			flag = False
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	import math
	e = min(a)
	s = set(a)
	b = sorted(a)

	def yesno(a, b):
		flag = 1
		c = [0] * len(b)
		for i in range(len(a)):
			if a[i] == b[i]:
				c[i] = 1
		for i in range(len(a)):
			if c[i] == 1:
				continue
			if a[i] % e != 0:
				flag = 0
		if flag == 1:
			return 'YES'
		else:
			return 'NO'
	print(yesno(a, b))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = sorted(a)
	ans_ = True
	for i in range(n):
		if a[i] % ans[0] and a[i] != ans[i]:
			ans_ = False
	if ans_:
		print('YES')
	else:
		print('NO')


import sys
from collections import defaultdict
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	if lis == sorted(lis):
		print('YES')
		continue
	m = min(lis)
	l = lis[:]
	l.sort()
	ll = [0] * n
	for i in range(n):
		ll[i] = m + lis[i] % m
	for i in range(n):
		if lis[i] == l[i]:
			continue
		if ll[i] != m:
			break
	else:
		print('YES')
		continue
	print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	mi = b[0]
	f = 0
	for i in range(n):
		if a[i] == b[i]:
			continue
		elif a[i] % mi == 0:
			continue
		else:
			f = 1
			break
	if f == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for test in range(t):
	n = int(input())
	array = list(map(int, input().rstrip().split()))
	array2 = sorted(array)
	minm = array2[0]
	for j in range(n):
		if array[j] != array2[j] and array[j] % minm != 0:
			print('NO')
			break
	else:
		print('YES')


import math
from collections import Counter, defaultdict
I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
for _ in range(I()):
	n = I()
	a = LI()
	b = sorted(a)
	c = b[0]
	if c == 1 or a == sorted(a) or n == 1:
		print('YES')
	else:
		flag = 1
		for i in range(len(a)):
			if a[i] != b[i] and a[i] % c != 0:
				flag = 0
				break
		if flag:
			print('YES')
		else:
			print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	smol = min(arr)
	copy = list(arr)
	copy.sort()
	flag = True
	for j in range(n):
		if arr[j] % smol != 0 and arr[j] != copy[j]:
			flag = False
			break
	if flag == True:
		print('YES')
	else:
		print('NO')


def gcd(a, b):
	while b > 0:
		(a, b) = (b, a % b)
	return a
t = int(input())
while t:
	n = int(input())
	a = [int(i) for i in input().split()]
	m = min(a)
	a_s = sorted(a)
	answ = True
	for i in range(n):
		if a[i] != a_s[i]:
			if gcd(a[i], m) != m:
				answ = False
				break
	print('YES' if answ else 'NO')
	t -= 1


def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	c = []
	for i in range(n):
		if a[i] != b[i]:
			c.append(a[i])
	if len(c) == 0:
		print('YES')
	else:
		gcd = c[0]
		for i in range(1, len(c)):
			gcd = find_gcd(gcd, c[i])
		if gcd % min(a) == 0:
			print('YES')
		else:
			print('NO')


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	minim = min(arr)
	ans = arr.copy()
	ans.sort()
	count = 0
	for i in range(n):
		if ans[i] == arr[i]:
			continue
		elif arr[i] % minim != 0:
			count += 1
	if count == 0:
		print('YES')
	else:
		print('NO')


T = int(input())
for t in range(T):
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = l1.copy()
	l2.sort()
	s = 'YES'
	m = l2[0]
	for i in range(n):
		if l1[i] != l2[i]:
			if l1[i] % m != 0:
				s = 'NO'
				break
	print(s)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().strip().split()))
	m = min(A)
	b = []
	p = []
	for i in range(n):
		if A[i] % m == 0:
			b.append(A[i])
			p.append(i)
	b.sort()
	for i in range(len(p)):
		A[p[i]] = b[i]
	ans = 'YES'
	for i in range(len(A) - 1):
		if A[i + 1] < A[i]:
			ans = 'NO'
			break
	print(ans)


def argsort(seq):
	return sorted(range(len(seq)), key=seq.__getitem__)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	inds = argsort(a)
	m = min(a)
	flag = True
	for i in range(n):
		if not i == inds[i] and (not a[i] % m == 0):
			flag = False
			break
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	i = 0
	no = []
	aa = []
	while i < n:
		if a[i] % m != 0:
			no.append(i)
		else:
			aa.append(a[i])
		i += 1
	if not no:
		print('YES')
	else:
		aa.sort()
		for i in no:
			aa.insert(i, a[i])
		flag = True
		for i in range(n - 1):
			if aa[i] > aa[i + 1]:
				flag = False
				break
		if flag:
			print('YES')
		else:
			print('NO')


t = int(input())
res = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	min_a = min(a)
	b = sorted(a)
	flag = True
	for i in range(n):
		if a[i] != b[i] and a[i] % min_a != 0:
			flag = False
	res.append('YES' if flag else 'NO')
for v in res:
	print(v)


n = int(input())
for i in range(n):
	l = int(input())
	arr = [int(item) for item in input().split()]
	brr = sorted(arr)
	flag = 0
	for i in range(l):
		if arr[i] != brr[i]:
			flag = 1
			break
	if flag == 0:
		print('YES')
	elif flag == 1:
		mini = 10 ** 9 + 1
		for j in arr:
			if j < mini:
				mini = j
		slag = 0
		for i in range(l):
			if arr[i] == brr[i]:
				continue
			if arr[i] != brr[i]:
				if arr[i] % mini != 0:
					slag = 1
					break
		if slag == 0:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	l = sorted(a)
	m = l[0]
	for i in range(n):
		if a[i] != l[i] and a[i] % m:
			print('NO')
			break
	else:
		print('YES')


import math
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = min(l)
	l1 = l.copy()
	l1.sort()
	f = 1
	for i in range(n):
		if l[i] != l1[i] and l[i] % x != 0:
			f = 0
	if f == 1:
		print('YES')
	else:
		print('NO')


from math import gcd
t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	sortedar = sorted(ar)
	mn = sortedar[0]
	for i in range(n):
		if ar[i] == sortedar[i]:
			continue
		elif ar[i] % mn != 0 or sortedar[i] % mn != 0:
			print('NO')
			break
	else:
		print('YES')


def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	l1 = l.copy()
	l1.sort()
	minm = min(l)
	ans = True
	for i in range(n):
		if l[i] != l1[i]:
			if gcd(l[i], minm) != minm:
				ans = False
				break
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	m1 = min(a)
	c = 0
	b = sorted(a)
	for i in range(n):
		if a[i] % m1 == 0 or a[i] == b[i]:
			c += 1
	if c == n:
		print('YES')
	else:
		print('NO')


import math
t = int(input())
for i in range(t):
	n = int(input())
	arr = input().split(' ')
	if n == 1:
		print('YES')
		continue
	intarr = []
	for j in arr:
		intarr.append(int(j))
	mini = 1000000000
	for j in intarr:
		if j < mini:
			mini = j
	temp = intarr
	temp = sorted(temp)
	check = False
	for j in range(len(intarr)):
		if math.gcd(intarr[j], mini) == mini:
			continue
		elif intarr[j] == temp[j]:
			continue
		else:
			check = True
	if check == True:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict
con = 10 ** 9 + 7
INF = float('inf')

def getlist():
	return list(map(int, input().split()))
from math import gcd

def main():
	T = int(input())
	for _ in range(T):
		N = int(input())
		A = getlist()
		B = sorted(A)
		m = B[0]
		flag = [0] * N
		for i in range(N):
			if gcd(m, A[i]) == m:
				flag[i] = 1
		jud = 1
		for i in range(N):
			if A[i] != B[i] and flag[i] == 0:
				jud = 0
				break
		if jud == 1:
			print('YES')
		else:
			print('NO')
main()


import bisect
import math
for xoxo in range(1):

	def gc(x, y):
		while y:
			(x, y) = (y, x % y)
		return x
	for _ in range(int(input())):
		ans = 'YES'
		n = int(input())
		a = list(map(int, input().split()))
		b = sorted(a)
		m = b[0]
		c = []
		for i in range(n):
			if a[i] != b[i]:
				c.append([a[i], b[i]])
		for i in range(len(c)):
			if gc(c[i][0], c[i][1]) == m or gc(c[i][0], m) == m:
				continue
			ans = 'NO'
			break
		print(ans)


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = sorted(a)
	k = min(a)
	flag = 0
	for i in range(n):
		if a[i] != b[i] and a[i] % k != 0:
			flag = 1
			break
	if flag == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	t = min(a)
	b = sorted(a)
	p = 0
	for i in range(len(b)):
		if a[i] != b[i]:
			if a[i] % t != 0:
				p += 1
				break
	if p > 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	min1 = min(A)
	B = []
	for i in range(n):
		if A[i] % min1 == 0:
			B.append(A[i])
			A[i] = 0
	B.sort()
	index = 0
	for i in range(n):
		if A[i] == 0:
			A[i] = B[index]
			index += 1
	fl = 0
	for i in range(n - 1):
		if A[i] > A[i + 1]:
			fl = 1
			print('NO')
			break
	if fl == 0:
		print('YES')


import sys

def input():
	return sys.stdin.readline()[:-1]

def main():
	T = int(input())
	for i in range(T):
		N = int(input())
		A = list(map(int, input().split()))
		flag = [False] * N
		mina = min(A)
		B = []
		C = []
		for i in range(N):
			if A[i] % mina == 0:
				flag[i] = True
				B.append(A[i])
			else:
				C.append(A[i])
		B.sort()
		c1 = 0
		c2 = 0
		D = []
		for i in range(N):
			if flag[i]:
				D.append(B[c1])
				c1 += 1
			else:
				D.append(C[c2])
				c2 += 1
		flag = True
		for i in range(N - 1):
			if D[i] > D[i + 1]:
				flag = False
				break
		if flag:
			print('YES')
		else:
			print('NO')
main()


for i in range(int(input())):
	n = int(input())
	x = [int(i) for i in input().split()]
	y = sorted(x)
	z = y[0]
	flag = 0
	for i in range(len(y)):
		if y[i] % z:
			if y[i] != x[i]:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mini = min(a)
	b = a.copy()
	b.sort()
	last = 0
	possble = True
	for i in range(n):
		if a[i] % mini != 0 and a[i] != b[i]:
			possble = False
			break
	if possble == True:
		print('YES')
	else:
		print('NO')


import math
for j in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	k = l.copy()
	k.sort()
	a = k[0]
	e = 0
	if k == l:
		print('YES')
	else:
		for j in range(n):
			if l[j] != k[j]:
				if l[j] % a != 0:
					e = e + 1
		if e > 0:
			print('NO')
		else:
			print('YES')


T = int(input())
for _ in range(0, T):
	n = int(input())
	s = [int(x) for x in input().split()]
	mn = min(s)
	pos = [0] * len(s)
	for i in range(0, len(s)):
		if s[i] % mn == 0:
			pos[i] = 1
	L = sorted(s)
	temp = 'YES'
	for i in range(0, len(s)):
		if L[i] != s[i] and pos[i] == 0:
			temp = 'NO'
	print(temp)


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	a2 = sorted(arr)
	fl = 0
	for k in range(n):
		if arr[k] != a2[k]:
			if arr[k] % a2[0] != 0:
				fl = 1
				break
	if fl == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	N = int(input())
	array = [int(x) for x in input().split()]
	if array == sorted(array):
		print('YES')
	else:
		gcd = min(array)
		dummy = []
		for i in array:
			if i % gcd == 0:
				dummy.append(i)
		dummy = sorted(dummy)
		counter = 0
		for i in range(N):
			if array[i] % gcd == 0:
				array[i] = dummy[counter]
				counter += 1
		if array == sorted(array):
			print('YES')
		else:
			print('NO')


import sys
import copy
input = sys.stdin.readline
t = int(input())
for tt in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	can = True
	b = copy.deepcopy(a)
	b.sort()
	k = min(b)
	for i in range(n):
		if a[i] != b[i] and a[i] % k != 0:
			can = False
			break
	if not can:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	sorted_a = sorted(a)
	min_element = sorted_a[0]
	for (e, e_sorted) in zip(a, sorted_a):
		if e != e_sorted:
			if e % min_element:
				print('NO')
				break
	else:
		print('YES')


import math
t = int(input())
for w in range(t):
	n = int(input())
	arr = [int(a) for a in input().split()]
	v = []
	di = []
	f1 = 0
	f2 = 0
	for i in range(n):
		v.append(arr[i])
	v.sort()
	m = min(arr)
	for i in range(n):
		if arr[i] % m == 0:
			di.append(arr[i] // m)
		else:
			di.append(0)
	val2 = arr[0]
	for i in range(n):
		if di[i] == 0:
			if arr[i] != v[i]:
				f1 = 1
				break
		if i == 0:
			continue
		if arr[i] < arr[i - 1]:
			f2 = 1
	if f1 == 1:
		print('NO')
		continue
	if f2 == 0:
		print('YES')
		continue
	gc = 0
	for i in range(n):
		if di[i] != 0:
			if gc == 0:
				gc = di[i]
			else:
				gc = math.gcd(gc, di[i])
	if gc == 1:
		print('YES')
		continue
	print('NO')


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	b = []
	c = []
	for i in ar:
		b.append(i)
	b.sort()
	mi = b[0]
	co = 0
	for i in range(n):
		if ar[i] % mi != 0:
			c.append(i)
	for i in c:
		if ar[i] != b[i]:
			co = 1
			print('No')
			break
	if co == 0:
		print('Yes')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	sl = sorted(l)
	m = sl[0]
	z = 0
	for i in range(n):
		if sl[i] != l[i]:
			if l[i] % m != 0:
				z = 1
				break
	if z == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	x = []
	for i in range(len(lst)):
		x.append(lst[i])
	x.sort()
	minimum = x[0]
	flag = 0
	for i in range(len(lst)):
		if lst[i] % minimum != 0 and lst[i] != x[i]:
			flag = 1
			break
	if flag == 1:
		print('No')
	else:
		print('YES')


import copy
from math import gcd
n = int(input())
for _ in range(n):
	p = int(input())
	arr = list(map(int, input().split()))
	somEl = min(arr)
	new_arr = copy.deepcopy(arr)
	arr.sort()
	sompl = []
	for i in range(len(arr)):
		if arr[i] != new_arr[i]:
			sompl.append(new_arr[i])
	ans = somEl
	for som in sompl:
		ans = gcd(ans, som)
	if ans == somEl:
		print('YES')
	else:
		print('NO')


import sys
import math
import collections
import heapq
import queue
import itertools
import functools
import operator
import time
readline = sys.stdin.readline
IPS = lambda : readline().rstrip()
IP = lambda : int(readline().rstrip())
MP = lambda : map(int, readline().split())
LS = lambda : list(map(int, readline().split()))

def solve():
	for _ in range(IP()):
		n = IP()
		a = LS()
		mn = min(a)
		b = sorted(a)
		flg = 1
		temp = [b[i] for i in range(n) if b[i] != a[i]]
		for elem in temp:
			if elem % mn != 0:
				flg = 0
				break
		print('YES') if flg == 1 else print('NO')
solve()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	x = arr.copy()
	x.sort()
	u = []
	a = []
	m = {}
	r = {}
	flag = 0
	mi = min(arr)
	for i in range(n):
		if arr[i] != x[i]:
			if arr[i] % mi != 0:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for hh in range(t):
	n = int(input())
	li = [int(i) for i in input().split()]
	mini = min(li)
	lii = sorted(li)
	d = {}
	for i in range(len(li)):
		if li[i] % mini != 0:
			d[i] = 1
	flag = True
	for i in d:
		if li[i] != lii[i]:
			print('NO')
			flag = False
			break
	if flag:
		print('YES')


import sys
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(buff_readline())

def read_int_n():
	return list(map(int, buff_readline().split()))

def read_float():
	return float(buff_readline())

def read_float_n():
	return list(map(float, buff_readline().split()))

def read_str():
	return readline().strip()

def read_str_n():
	return readline().strip().split()

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap
from math import gcd

def slv(N, A):
	ma = min(A)
	b = []
	for (i, a) in enumerate(A):
		b.append(a % ma == 0)
	for (i, a) in enumerate(sorted(A)):
		if a != A[i] and (not b[i]):
			return 'NO'
	return 'YES'

def main():
	for _ in range(read_int()):
		N = read_int()
		A = read_int_n()
		print(slv(N, A))
main()


import math
import os
import random
import re
import string
from sys import *
from collections import *

def bs(arr, x, l, r):
	if l <= r:
		mid = (r + l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return bs(arr, x, l, mid - 1)
		else:
			return bs(arr, x, mid + 1, r)
	else:
		return -1

def solve():
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, stdin.readline().strip().split()))
		if n == 1:
			print('YES')
			continue
		x = l[:]
		x.sort()
		re = []
		m = x[0]
		for i in range(n):
			if l[i] == x[i]:
				pass
			else:
				re.append(l[i])
		if len(re) == 0:
			print('YES')
			continue
		g = 1
		for i in range(len(re)):
			if re[i] % m == 0:
				f = 1
			else:
				f = 0
				break
		if f == 1:
			print('YES')
		else:
			print('NO')
solve()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	minn = min(a)
	ans = True
	for i in range(n):
		if a[i] != b[i] and a[i] % minn != 0:
			ans = False
			break
	if ans:
		print('YES')
	else:
		print('NO')


TC = int(input())
while TC > 0:
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	l = []
	for i in range(n):
		if a[i] % b[0]:
			l.append(i)
	for i in l:
		if a[i] != b[i]:
			print('NO')
			break
	else:
		print('YES')
	TC -= 1


from collections import defaultdict

def solve(arr):
	m = min(arr)
	d = defaultdict(int)
	for i in range(len(arr)):
		if arr[i] % m == 0:
			d[i] = arr[i]
	for (k, v) in zip(sorted(d.keys()), sorted(d.values())):
		arr[k] = v
	if arr == sorted(arr):
		print('Yes')
	else:
		print('No')
t = int(input())
for i in range(t):
	input()
	solve(list(map(int, input().split())))


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	l = [int(x) for x in input().split()]
	m = min(l)
	s = l[:]
	s.sort()
	f = 0
	for i in range(n):
		if l[i] != s[i]:
			if l[i] % m != 0:
				f = 1
				break
	if f == 1:
		print('NO')
	else:
		print('YES')


tc = int(input())
while tc > 0:
	tc -= 1
	n = int(input())
	k = list(map(int, input().split()))
	p = min(k)
	temp = sorted(k)
	flag = True
	for i in range(n):
		if k[i] != temp[i] and k[i] % p != 0:
			flag = False
			break
	if flag:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
from collections import defaultdict
import math
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	mini = 1000000000000
	for i in range(n - 1):
		if l[i] > l[i + 1]:
			f = 1
		mini = min(l[i], l[i + 1], mini)
	if f == 0:
		print('YES')
	if f == 1:
		f = 0
		l1 = sorted(l)
		for i in range(n):
			if l[i] != l1[i]:
				if l[i] % mini != 0:
					f = 1
					break
		if f == 1:
			print('NO')
		else:
			print('YES')


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
sys.setrecursionlimit(10 ** 6)
from queue import PriorityQueue
from collections import Counter as cc
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
sys.setrecursionlimit(10 ** 6)
from queue import PriorityQueue
from collections import Counter as cc
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	hash = defaultdict(list)
	for i in range(n):
		hash[l[i]].append(i)
	z = l.copy()
	z.sort()
	flag = 0
	k = z[0]
	for i in range(n):
		if gcd(l[i], k) != k:
			if l[i] == z[i]:
				continue
			else:
				flag = 1
				break
	if flag:
		print('NO')
	else:
		print('YES')


from sys import stdin
import math
for _ in range(int(input())):
	n = int(stdin.readline().rstrip())
	l = list(map(int, stdin.readline().rstrip().split(' ')))
	r = sorted(l)
	mi = r[0]
	res = 'YES'
	for i in range(n):
		if l[i] != r[i]:
			if math.gcd(l[i], mi) != mi:
				res = 'NO'
				break
	print(res)


def main():
	import sys
	input = sys.stdin.buffer.readline
	t = int(input())

	def check():
		n = int(input())
		alst = list(map(int, input().split()))
		min_a = min(alst)
		blst = []
		for a in alst:
			if a % min_a == 0:
				blst.append(a)
		pos = 0
		blst.sort()
		for (i, a) in enumerate(alst):
			if a % min_a == 0:
				alst[i] = blst[pos]
				pos += 1
		bef = alst[0]
		for a in alst:
			if a < bef:
				return 'NO'
			bef = a
		return 'YES'
	for _ in range(t):
		print(check())
main()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	temp = arr[:]
	temp.sort()
	mn = min(arr)
	dp = []
	for i in arr:
		if i % mn == 0:
			dp.append(i)
	dp.sort()
	k = 0
	for i in range(n):
		if arr[i] % mn == 0:
			arr[i] = dp[k]
			k += 1
	if temp != arr:
		print('NO')
	else:
		print('YES')


def multiple_input():
	return map(int, input().split())

def list_input():
	return list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	a = list_input()
	b = sorted(a)
	start = {}
	end = {}
	pos = {}
	m = b[0]
	li = []
	for i in range(n):
		if b[i] in start:
			end[b[i]] = i
		else:
			start[b[i]] = i
			end[b[i]] = i
	for i in range(n):
		if a[i] % m != 0:
			li.append(i)
	flag = 0
	for i in li:
		if start[a[i]] <= i <= end[a[i]]:
			continue
		else:
			flag = 1
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


def can_be_sorted(arr):
	min_element = min(arr)
	sorted_arr = sorted(arr)
	for (s, n) in zip(sorted_arr, arr):
		if n % min_element != 0 and s != n:
			return 'NO'
	return 'YES'
results = []
for _ in range(int(input())):
	_ = input()
	arr = [int(el) for el in input().split(' ')]
	results.append(can_be_sorted(arr))
print('\n'.join(results))


for t in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	index = []
	val = min(num)
	temp = []
	for i in range(n):
		if num[i] % val == 0:
			index.append(i)
			temp.append(num[i])
	temp.sort()
	for i in range(len(index)):
		num[index[i]] = temp[i]
	if num == sorted(num):
		print('YES')
	else:
		print('NO')


import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase
sys.setrecursionlimit(10 ** 5)

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		ar = inpt()
		mn = min(ar)
		for i in range(n):
			ar[i] = (ar[i], i)
		ar.sort()
		gcd = mn
		ok = True
		for i in range(n):
			(a, b) = ar[i]
			gcd = math.gcd(a, mn)
			if gcd != mn and b != i:
				ok = False
				break
		if not ok:
			print('NO')
		else:
			print('YES')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	m = min(arr)
	s_arr = sorted(arr)
	val = []
	for i in range(len(arr)):
		if arr[i] == s_arr[i]:
			continue
		else:
			val.append((arr[i], s_arr[i]))
	flag = 1
	for i in val:
		if i[0] % m > 0:
			flag = 0
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = [int(c) for c in input().split()]
	sarr = sorted(arr)
	ans = []
	for i in range(len(arr)):
		if arr[i] != sarr[i]:
			ans.append(arr[i])
	k = min(arr)
	poss = True
	for i in ans:
		if i % k != 0:
			poss = False
			break
	if poss:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	b = a.copy()
	b.sort()
	f = 0
	for i in range(n):
		if a[i] != b[i] and a[i] % m != 0:
			f = 1
			break
	if f == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = sorted(a)
	k = c[0]
	x = 'YES'
	for i in range(n):
		if a[i] != c[i]:
			if a[i] % k != 0 or c[i] % k != 0:
				x = 'NO'
				break
	print(x)


def isDivisibleBy(n, x):
	if n % x == 0:
		return True
	else:
		return False
n = int(input())
arrays = []
lengths = []
for x in range(n):
	length = int(input())
	_list = [int(i) for i in input().split()]
	arrays.append(_list)
	lengths.append(lengths)
for array in arrays:
	final = sorted(array)
	minimum = final[0]
	multiples = [isDivisibleBy(i, minimum) for i in array]
	_bool = True
	for i in range(len(array)):
		if final[i] != array[i] and multiples[i] == False:
			_bool = False
			break
	if _bool:
		print('YES')
	else:
		print('NO')


import math
tests = int(input())
for t in range(tests):
	n = int(input())
	ls = list(map(int, input().split()))
	sort_ls = sorted(ls)
	min_val = sort_ls[0]
	if min_val == 1:
		print('YES')
	else:
		check_arr = []
		for i in range(n):
			if ls[i] != sort_ls[i]:
				check_arr.append(ls[i])
		check = True
		for item in check_arr:
			if math.gcd(item, min_val) != min_val:
				check = False
				break
		if check:
			print('YES')
		else:
			print('NO')


def calc(a, b):
	if a == 0:
		return b
	if b == 0:
		return a
	if a == b:
		return a
	while a > 0 and b > 0:
		if a > b:
			(a, b) = (b, a - b)
		else:
			(a, b) = (b - a, a)
	return max(a, b)
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	p = min(a)
	b = []
	for j in a:
		b.append(j)
	b.sort()
	c = []
	for i in range(0, n):
		if a[i] != b[i]:
			if a[i] % p:
				print('NO')
				break
	else:
		print('YES')
	t = t - 1


import sys, math as mt
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	l = I()
	f = 0
	p = sorted(l)
	rq = [0] * n
	g = min(l)
	for i in range(n):
		if l[i] != p[i]:
			rq[i] = l[i]
	for i in range(n):
		if rq[i] != 0 and mt.gcd(rq[i], g) != g:
			f = 1
			break
	print('YNEOS'[f::2])


import math

def main():
	n = int(input())
	a = list(map(int, input().split()))
	mn = min(a)
	new_lst = []
	for i in a:
		if i % mn == 0:
			new_lst.append(i)
	new_lst = sorted(new_lst)
	k = 0
	for i in range(n):
		if a[i] % mn == 0:
			a[i] = new_lst[k]
			k += 1
	new_lst = sorted(a)
	for i in range(n):
		if new_lst[i] != a[i]:
			print('NO')
			return
	print('YES')
t = int(input())
for i in range(t):
	main()


for t in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	l = sorted(arr)
	minVal = l[0]
	isDone = False
	for i in range(n):
		if arr[i] != l[i] and arr[i] % minVal != 0:
			print('NO')
			isDone = True
			break
	if not isDone:
		print('YES')


from math import *
t = int(input())
for o in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	c = []
	m = b[0]
	for i in range(n):
		if a[i] != b[i]:
			c.append(a[i])
	if len(c) == 0:
		print('YES')
	else:
		c.sort()
		l = len(c)
		f = 1
		for i in range(l):
			if c[i] % m != 0:
				f = 0
				break
		if f == 0:
			print('NO')
		else:
			f = 0
			for i in range(l):
				if gcd(m, c[i]) == m:
					f = 1
					break
			if f == 1:
				print('YES')
			else:
				print('NO')


import math
t = int(input())
for _ in range(t):
	n = int(input())
	ali = list(map(int, input().split()))
	q = sorted(ali)
	f = 0
	w = q[0]
	for i in range(n):
		if q[i] != ali[i]:
			if math.gcd(w, ali[i]) != w:
				f = 1
				break
	if f == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	b = sorted(arr)
	ans = 'yes'
	for i in range(len(arr)):
		if arr[i] != b[i] and arr[i] % b[0] != 0:
			ans = 'no'
	print(ans)
	t -= 1


import sys

def FastInt(zero=0):
	(_ord, nums, num, neg) = (lambda x: x, [], zero, False)
	(i, s) = (0, sys.stdin.buffer.read())
	try:
		while True:
			if s[i] >= b'0'[0]:
				num = 10 * num + _ord(s[i]) - 48
			elif s[i] == b'-'[0]:
				neg = True
			elif s[i] != b'\r'[0]:
				nums.append(-num if neg else num)
				(num, neg) = (zero, False)
			i += 1
	except IndexError:
		pass
	if s and s[-1] >= b'0'[0]:
		nums.append(-num if neg else num)
	return nums
inp = FastInt()
ii = 0

def inin(size=None):
	global ii
	if size == None:
		ni = ii
		ii += 1
		return inp[ni]
	else:
		ni = ii
		ii += size
		return inp[ni:ni + size]
from math import gcd
_T_ = inin()
for _t_ in range(_T_):
	n = inin()
	a = inin(n)
	mil = min(a)
	b = a[:]
	b.sort()
	check = []
	poss = True
	for i in range(n):
		if a[i] != b[i]:
			if gcd(a[i], b[i]) % mil == 0:
				pass
			else:
				poss = False
	if poss:
		print('YES')
	else:
		print('NO')


import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
import itertools
input = sys.stdin.readline
mo = 10 ** 9 + 7

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
for _ in range(inin()):
	n = inin()
	l = ain()
	k = sorted(l)
	f = 1
	mi = k[0]
	for i in range(n):
		if k[i] != l[i]:
			if math.gcd(k[i], l[i]) % mi != 0:
				f = 0
				break
	if f:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	sot = sorted(a)
	m = min(a)
	boo = True
	for i in range(n):
		if a[i] != sot[i] and a[i] % m != 0:
			boo = False
			break
	ans = 'YES' if boo else 'NO'
	print(ans)


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	listWithOrder = []
	for i in range(n):
		listWithOrder.append((a[i], i))
	minA = min(a)
	listWithOrder.sort()
	isSortable = True
	for i in range(n):
		if listWithOrder[i][1] != i and a[i] % minA != 0:
			isSortable = False
			break
	if isSortable:
		print('YES')
	else:
		print('NO')


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	n = int(inp())
	a = lis()
	new = sorted(a)
	m = min(a)
	swap = []
	for i in range(n):
		if a[i] == new[i]:
			continue
		else:
			swap.append(a[i])
	for i in range(len(swap)):
		if gcd(m, swap[i]) == m:
			continue
		else:
			print('NO')
			return
	print('YES')
testcase(int(inp()))


import sys
for i in range(0, int(sys.stdin.readline())):
	leng = sys.stdin.readline()
	l = list(map(int, sys.stdin.readline().split()))
	l2 = [j for j in l]
	l2.sort()
	minl = min(l)
	works = True
	for k in range(0, len(l)):
		if l[k] % minl != 0:
			works = l[k] == l2[k]
		if works == False:
			break
	if works:
		sys.stdout.write('YES\n')
	else:
		sys.stdout.write('NO\n')


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()
from random import randint
from collections import defaultdict, deque

class Graph(object):

	def __init__(self, dic):
		self.dict = dic
		self.vertex = set()

	def addedge(self, a, b):
		if self.dict[a] == []:
			self.vertex.add(a)
		if b != None:
			self.dict[a].append(b)

	def con_com_util(self, i, unvisited, count, comp):
		for j in self.dict[i]:
			if j in unvisited:
				unvisited.remove(j)
				comp[count].append(j)
				comp = self.con_com_util(j, unvisited, count, comp)
		return comp

	def conn_components(self):
		unvisited = self.vertex.copy()
		count = 0
		comp = defaultdict(list)
		while unvisited:
			i = unvisited.pop()
			comp[count].append(i)
			comp = self.con_com_util(i, unvisited, count, comp)
			count += 1
		return (count, comp)

def countSetBits(n):
	count = 0
	while n:
		n &= n - 1
		count += 1
	return count

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a
from bisect import bisect_left as bl

def solve():
	n = int(inp())
	a = lis()
	b = a.copy()
	b.sort()
	m = b[0]
	t = 1
	for i in range(n):
		if a[i] == b[i] or gcd(a[i], b[i]) % m == 0:
			pass
		else:
			t = -1
	if t == -1:
		print('NO')
	else:
		print('YES')
testcase(int(inp()))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
from math import gcd
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
for _ in range(int(ri())):
	n = int(ri())
	a = Ri()
	req = min(a)
	ans = [0] * n
	for i in range(n):
		if gcd(a[i], req) == req:
			ans[i] = 1
	a = [(a[i], i) for i in range(n)]
	a.sort(key=lambda x: x[0])
	flag = True
	for i in range(n):
		if a[i][1] == i:
			continue
		if ans[a[i][1]] == 1 and ans[i] == 1:
			continue
		flag = False
		break
	if flag:
		YES()
	else:
		NO()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def dfs(graph, start=0):
	n = len(graph)
	dp = [0] * n
	(visited, finished) = ([False] * n, [False] * n)
	stack = [start]
	while stack:
		start = stack[-1]
		if not visited[start]:
			visited[start] = True
			for child in graph[start]:
				if not visited[child]:
					stack.append(child)
		else:
			stack.pop()
			dp[start] += 1
			for child in graph[start]:
				if finished[child]:
					dp[start] += dp[child]
			finished[start] = True
	return (visited, dp)
from collections import deque
for _ in range(int(input()) if True else 1):
	n = int(input())
	a = list(map(int, input().split()))
	mi = min(a)
	b = []
	for i in range(n):
		if a[i] % mi == 0:
			b += [a[i]]
	b = sorted(b)
	j = 0
	for i in range(n):
		if a[i] % mi == 0:
			a[i] = b[j]
			j += 1
	print('YES' if a == sorted(a) else 'NO')


from fractions import gcd
t = int(input())
for _ in range(t):
	_ = int(input())
	a = list(map(int, input().split()))
	corr = sorted(a)
	if a == corr:
		print('YES')
		continue
	MIN = min(a)
	arr = [x for (x, y) in zip(a, corr) if x != y]
	if all([gcd(x, MIN) == MIN for x in arr]):
		print('YES')
	else:
		print('NO')


T = int(input())
for _ in range(T):
	(n, ar) = (int(input()), list(map(int, input().split())))
	mn = min(ar)
	p = [u for u in ar if u % mn == 0]
	p.sort()
	(res, j) = ([], 0)
	for i in range(n):
		if ar[i] % mn == 0:
			res.append(p[j])
			j += 1
		else:
			res.append(ar[i])
	ok = True
	for i in range(1, n):
		if res[i - 1] > res[i]:
			ok = False
			break
	print('YES' if ok else 'NO')


def f():
	t = int(input())
	while t != 0:
		t -= 1
		L = [int(i) for i in input().split()]
		(A, K) = (L[0], L[1])
		if K > A:
			print(K - A)
		elif A % 2 == 0 and K % 2 == 0:
			print(0)
		elif A % 2 != 0 and K % 2 != 0:
			print(0)
		else:
			print(1)

def B():
	t = int(input())
	while t != 0:
		t -= 1
		A = [int(i) for i in input().split()]
		B = [int(i) for i in input().split()]
		Ans = 0
		Ans = Ans + 2 * min(A[2], B[1])
		tmp = A[2]
		A[2] = A[2] - min(A[2], B[1])
		B[1] = B[1] - min(tmp, B[1])
		tmp = A[0]
		A[0] = A[0] - min(A[0], B[2])
		B[2] = B[2] - min(tmp, B[2])
		tmp = A[0]
		A[0] = A[0] - min(A[0], B[1])
		B[1] = B[1] - min(tmp, B[1])
		tmp = A[0]
		A[0] = A[0] - min(A[0], B[0])
		B[0] = B[0] - min(tmp, B[0])
		tmp = A[1]
		A[1] = A[1] - min(A[1], B[0])
		B[0] = B[0] - min(tmp, B[0])
		tmp = A[1]
		A[1] = A[1] - min(A[1], B[1])
		B[1] = B[1] - min(tmp, B[1])
		tmp = A[2]
		A[2] = A[2] - min(A[2], B[2])
		B[2] = B[2] - min(tmp, B[2])
		Ans = Ans - 2 * min(A[1], B[2])
		print(Ans)

def C():

	def GCD(a, b):
		while b != 0:
			t = b
			b = a % b
			a = t
		return a
	t = int(input())
	while t != 0:
		t -= 1
		n = int(input())
		A = [int(i) for i in input().split()]
		m = min(A)
		B = []
		for i in A:
			if i % m == 0:
				B.append(i)
		B.sort()
		j = 0
		for i in range(0, len(A)):
			if A[i] % m == 0:
				A[i] = B[j]
				j = j + 1
		f = True
		for i in range(0, len(A) - 1):
			if A[i] > A[i + 1]:
				f = False
				break
		if f == False:
			print('NO')
		else:
			print('YES')
C()


def gcd(a, b):
	if a == 0:
		return b
	else:
		return gcd(b % a, a)
for w in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a1 = sorted(a)
	m = min(a)
	c = 0
	if n == 1 or a == a1:
		print('YES')
	else:
		for i in range(n):
			if a[i] == a1[i]:
				continue
			elif gcd(a[i], a1[i]) == m:
				for j in range(i + 1, n):
					if a[j] == a1[i]:
						(a[i], a[j]) = (a[j], a[i])
						break
			elif a[i] % m != 0 or a1[i] % m != 0:
				c = 1
				break
			else:
				for j in range(i + 1, n):
					if a[j] == a1[i]:
						(a[i], a[j]) = (a[j], a[i])
						break
		if c == 1:
			print('NO')
		else:
			print('YES')


import math
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	index = []
	num = []
	for i in range(n):
		if a[i] % m == 0:
			index.append(i)
			num.append(a[i])
	num.sort()
	for i in range(len(index)):
		a[index[i]] = num[i]
	flag = 0
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			flag = 1
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	gmin = min(l)
	nl = list(l)
	l.sort()
	flag = 0
	for i in range(len(l)):
		if l[i] != nl[i]:
			if nl[i] % gmin == 0:
				continue
			else:
				flag = 1
				print('NO')
				break
	if flag == 0:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	b = sorted(a)
	m = b[0]
	flag = True
	for i in range(n):
		if a[i] != b[i] and a[i] % m != 0:
			print('NO')
			flag = False
			break
	if flag:
		print('YES')


def function(n, nums):
	a = sorted(nums)
	if nums == a:
		return 'YES'
	min_nums = min(nums)
	for i in range(len(nums)):
		if nums[i] != a[i]:
			if nums[i] % min_nums != 0:
				return 'NO'
	return 'YES'
t = int(input())
for k in range(t):
	n = int(input())
	nums = list(map(int, input().rstrip().split()))
	print(function(n, nums))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	l2 = []
	c = min(l)
	for j in range(n):
		if l[j] % c == 0:
			l1.append(l[j])
		else:
			l2.append(l[j])
	b = 0
	for j in range(1, len(l2)):
		if l2[j] >= l2[j - 1]:
			continue
		else:
			b = 1
	if b == 1:
		print('NO')
	else:
		l3 = []
		for j in range(n):
			l3.append(l[j])
		l3.sort()
		for j in range(n):
			if l[j] % c != 0:
				if l3[j] == l[j]:
					continue
				else:
					b = 1
		if b == 1:
			print('NO')
		else:
			print('YES')


from math import gcd
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	sortedarr = sorted(arr)
	flag = False
	for i in range(n):
		if arr[i] != sortedarr[i]:
			flag = True
			break
	if not flag:
		print('YES')
	else:
		m = min(arr)
		if arr[0] == m:
			possible = True
			for i in range(n):
				if arr[i] != sortedarr[i]:
					if gcd(m, arr[i]) != m:
						possible = False
						break
			if possible:
				print('YES')
			else:
				print('NO')
		else:
			first = arr[0]
			possible = False
			for i in range(1, n):
				if gcd(first, arr[i]) == m:
					possible = True
					break
			if not possible:
				print('NO')
			else:
				possible = True
				for i in range(n):
					if arr[i] != sortedarr[i]:
						if gcd(m, arr[i]) != m:
							possible = False
							break
				if possible:
					print('YES')
				else:
					print('NO')


primelist = []
import math
from bisect import bisect_right
for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	B = sorted(A)
	index = []
	for i in range(N):
		if B[i] != A[i]:
			index.append(A[i])
	if len(index) == 0:
		print('YES')
	else:
		t = min(A)
		temp = 0
		for i in range(len(index)):
			if index[i] % t != 0:
				temp = 1
				break
			else:
				index[i] //= t
		if temp == 1:
			print('NO')
		else:
			print('YES')


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
input = stdin.readline
for __ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	m = min(ar)
	a = sorted(ar)
	temp = []
	for i in range(n):
		if ar[i] != a[i]:
			temp.append(ar[i])
	ans = 'YES'
	for i in range(len(temp)):
		if temp[i] % m:
			ans = 'NO'
	print(ans)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007

def main():
	for t in range(I()):
		n = I()
		arr = L()
		lis = sorted(arr)
		if arr == lis:
			print('YES')
		else:
			m = min(arr)
			flag = 1
			for i in range(n):
				if arr[i] != lis[i]:
					if arr[i] % m != 0:
						flag = 0
						break
			if flag:
				print('YES')
			else:
				print('NO')
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	fl = 1
	for i in range(n):
		if a[i] == b[i]:
			continue
		elif a[i] % b[0] != 0:
			fl = 0
	if fl:
		print('YES')
	else:
		print('NO')


x = int(input())
for i in range(x):
	n = int(input())
	y = list(map(int, input().split()))
	a = []
	minima = y[0]
	for ll in range(n):
		a.append(y[ll])
	a.sort()
	flag = 1
	for kk in range(n):
		if a[kk] != y[kk]:
			if y[kk] % a[0] == 0:
				flag = 1
			else:
				flag = 0
				break
	if flag:
		print('YES')
	else:
		print('NO')


from math import gcd
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = sorted(l)
	flag = 1
	c = min(l)
	for i in range(n - 1, -1, -1):
		if l[i] == x[i]:
			pass
		elif gcd(c, l[i]) == c:
			pass
		else:
			flag = 0
			break
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	nondecreasing = True
	for i in range(1, n):
		if arr[i - 1] > arr[i]:
			nondecreasing = False
			break
	if nondecreasing:
		print('Yes')
	else:
		can_be_gcded = True
		min_element = min(arr)
		sorted_array = sorted(arr)
		for i in range(n):
			if arr[i] == sorted_array[i]:
				continue
			if arr[i] % min_element != 0:
				can_be_gcded = False
				break
		if can_be_gcded == True:
			print('Yes')
		else:
			print('No')


import math
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	br = ar[:]
	br.sort()
	cr = []
	ct = min(ar)
	for i in range(n):
		if ar[i] != br[i]:
			cr.append(ar[i])
	if len(cr) == 0:
		print('YES')
	else:
		for j in cr:
			ct = math.gcd(ct, j)
		if ct == min(ar):
			print('YES')
		else:
			print('NO')


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A):
	if A == sorted(A):
		return 'YES'
	mn = min(A)
	B = [-1 for i in range(N)]
	swappable = []
	for (i, x) in enumerate(A):
		if x % mn == 0:
			swappable.append(i)
		else:
			B[i] = x
	swappable.sort(key=lambda i: A[i])
	swappable = swappable[::-1]
	for i in range(N):
		if B[i] == -1:
			B[i] = A[swappable.pop()]
	if B == sorted(B):
		return 'YES'
	else:
		return 'NO'
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	ans = solve(N, A)
	print(ans)


import sys
import math
input = sys.stdin.readline

def inInt():
	return int(input())

def inStr():
	return input().strip('\n')

def inIList():
	return list(map(int, input().split()))

def inSList():
	return input().split()

def solve():
	n = inInt()
	a = inIList()
	sa = a.copy()
	sa.sort()
	m = min(a)
	for x in range(len(sa)):
		if sa[x] == a[x]:
			continue
		if sa[x] % m != 0:
			return False
	return True
tests = inInt()
for case in range(tests):
	print('YES' if solve() else 'NO')


def main():
	n = int(input())
	a = [int(s) for s in input().split()]
	b = sorted(a)
	am = min(a)
	for (i, e) in enumerate(a):
		if e % am != 0 and b[i] != e:
			return 'NO'
	return 'YES'
tests = int(input())
for _ in range(tests):
	print(main())


import copy
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	b = copy.copy(l)
	b.sort()
	m = min(l)
	a = []
	f = 0
	for j in range(n):
		if l[j] != b[j]:
			a.append(l[j])
			if l[j] % m != 0:
				print('NO')
				f = 1
				break
	else:
		print('YES')


import sys
from collections import defaultdict as dd
from collections import Counter as ccd
from itertools import permutations as pp
from itertools import combinations as cc
from random import randint as rd
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq as hq
import math
mod = pow(10, 9) + 7

def inp(flag=0):
	if flag == 0:
		return list(map(int, input().strip().split(' ')))
	elif flag == 1:
		return int(input())
	else:
		return input().strip()

def inp_fast(flag=0):
	if flag == 0:
		return list(map(int, sys.stdin.readline().split()))
	elif flag == 1:
		return int(sys.stdin.readline())
	else:
		return sys.stdin.readline()
t = inp(1)
while t:
	t -= 1
	n = inp(1)
	l = inp()
	ls = sorted(l)
	possible = True
	for i in range(n):
		if l[i] % ls[0] != 0 and l[i] != ls[i]:
			possible = False
			break
	if possible:
		print('YES')
	else:
		print('NO')


from sys import stdin
input = lambda : stdin.readline().strip()
from collections import deque as d
import math
l = len
T = int(input())
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	mn = min(a)
	b = sorted(a)
	flag = True
	for i in range(n):
		if a[i] != b[i] and a[i] % mn != 0:
			flag = False
	if flag:
		print('YES')
	else:
		print('NO')


import math
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	m = min(arr)
	lis2 = []
	for i in range(n):
		if math.gcd(arr[i], m) == m:
			lis2.append(arr[i])
			arr[i] = -1
	lis2.sort()
	z = 0
	check = False
	for i in range(n):
		if arr[i] == -1:
			arr[i] = lis2[z]
			z += 1
		if i != 0 and arr[i] < arr[i - 1]:
			check = True
	if not check:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	input()
	a = list(map(int, input().split()))
	b = sorted(a)
	f = True
	for i in range(len(a)):
		if a[i] != b[i] and a[i] % b[0]:
			f = False
	print('YES' if f else 'NO')


from sys import stdin, stdout
from math import gcd
input = stdin.readline
print = stdout.write

def gcda(l):
	if len(l) == 0:
		return 1
	res = l[0]
	for i in range(1, len(l)):
		res = gcd(res, l[i])
	return res
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	lsort = l[:]
	lsort.sort()
	s = set()
	for i in range(n):
		if l[i] != lsort[i]:
			s.add(l[i])
	if len(s) == 0:
		print('YES')
	elif gcda(list(s)) % min(l) == 0:
		print('YES')
	else:
		print('NO')
	print('\n')


from sys import stdin, stdout

def gc():
	return map(int, stdin.readline().strip().split())

def gar():
	return list(map(int, stdin.readline().strip().split()))

def gs():
	return stdin.readline().strip()

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
t = int(stdin.readline())
while t:
	t -= 1
	n = int(stdin.readline())
	ar = gar()
	sar = sorted(ar)
	m = sar[0]
	f = True
	for i in range(n):
		if ar[i] != sar[i]:
			if gcd(m, ar[i]) != m:
				f = False
				break
	if f:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout, setrecursionlimit
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
setrecursionlimit(100000)
mod = 1000000007
tc = ii1()
for _ in range(tc):
	n = ii1()
	arr = iia()
	temp = sorted(arr)
	if arr == temp:
		print('YES')
	else:
		ele = min(arr)
		for (i, j) in zip(arr, temp):
			if i != j and i % ele != 0:
				print('NO')
				break
		else:
			print('YES')


for t in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	d = min(a)
	b = sorted(a)
	ans = 'YES'
	for i in range(n):
		if a[i] != b[i]:
			if b[i] % d != 0:
				ans = 'NO'
				break
	print(ans)


def main():
	ntests = int(input())
	n = []
	ints = []
	output = []
	for i in range(ntests):
		n.append(int(input()))
		ints.append(list(map(int, input().split())))
		output.append('YES')
	for i in range(ntests):
		smallest = min(ints[i])
		sortedint = sorted(ints[i])
		for num in range(n[i]):
			if ints[i][num] % smallest != 0 and sortedint[num] != ints[i][num]:
				output[i] = 'NO'
				break
	for i in range(ntests):
		print(output[i])
main()


from sys import *

def gcd(temp1, temp2):
	if temp2 == 0:
		return temp1
	return gcd(temp2, temp1 % temp2)
for testcases in range(int(input())):
	num = int(input())
	lis = list(map(int, stdin.readline().strip().split()))
	lisi = lis.copy()
	lisi.sort()
	mini = lisi[0]
	flag = 1
	for i in range(num):
		if lisi[i] != lis[i]:
			if gcd(lis[i], mini) != mini:
				print('NO')
				flag = 0
				break
	if flag == 1:
		print('YES')


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	check = arr.copy()
	check.sort()
	m = min(arr)
	track = []
	for i in range(n):
		if arr[i] != check[i]:
			track.append(check[i])
	if len(track) == 0:
		print('YES')
	else:
		lock = 1
		h = []
		for i in track:
			if math.gcd(i, m) != m:
				lock = 0
				break
		if lock == 0:
			print('NO')
		else:
			print('YES')


t = int(input())
while t > 0:
	n = int(input())
	li = [int(i) for i in input().split()]
	sort_li = li.copy()
	sort_li.sort()
	min_num = sort_li[0]
	flage = True
	for i in range(0, n):
		if li[i] != sort_li[i]:
			if li[i] % min_num != 0:
				flage = False
				break
	if flage == True:
		print('YES')
	else:
		print('NO')
	t -= 1


def solve(a, n):
	p = sorted(a)
	s = min(a)
	f = 1
	for i in range(n):
		if a[i] != p[i]:
			if a[i] % s != 0:
				f = 0
				break
	if f:
		return 'YES'
	else:
		return 'NO'
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(a, n))


for _ in range(int(input())):
	n = int(input())
	array = list(map(int, input().split()))
	sort_ar = sorted(array)
	mini = sort_ar[0]
	result = True
	for i in range(len(array)):
		ar = array[i]
		if ar != sort_ar[i]:
			if ar % mini != 0:
				result = False
				break
		if i != len(array) - 1:
			minimum = sort_ar[i + 1]
	if result:
		print('YES')
	else:
		print('NO')


for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, a))
	b.sort()
	m = b[0]
	flag = True
	for i in range(n):
		if a[i] != b[i]:
			if b[i] % m != 0:
				flag = False
				break
	if flag == True:
		print('Yes')
	else:
		print('No')


import math
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a1 = sorted(a)
	mini = a1[0]
	for k in range(len(a)):
		if a[k] != a1[k] and math.gcd(mini, a[k]) != mini:
			print('NO')
			break
	else:
		print('YES')


def sol(n, l):
	gcd = min(l)
	ll = sorted(l)
	for i in range(n):
		if l[i] != ll[i] and ll[i] % gcd != 0:
			return 'No'
			break
	return 'Yes'
t = int(input())
for _ in range(t):
	n = int(input())
	print(sol(n, [*map(int, input().split())]))


import math
test_cases = int(input())
for test_case in range(test_cases):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	mn = b[0]
	diff = []
	count = 0
	for i in range(n):
		if a[i] - b[i] != 0:
			diff.append(a[i])
	for i in diff:
		if i % mn != 0:
			count += 1
			break
	if count > 0:
		print('NO')
	else:
		print('YES')


import sys, math
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	num = min(arr)
	if n == 1:
		print('YES')
	else:
		correct = sorted(arr)
		diff = []
		for i in range(n):
			if correct[i] != arr[i]:
				diff.append(arr[i])
		if not diff:
			print('YES')
		else:
			diff.sort()
			find = True
			for i in diff:
				if i % num != 0:
					find = False
					break
			if find:
				print('YES')
			else:
				print('NO')


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = a[::-1]
	b.sort()
	z = min(a)
	p = True
	for j in range(n):
		if a[j] != b[j]:
			if a[j] % z > 0:
				p = False
				break
	if p:
		print('YES')
	else:
		print('NO')


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
t = int(input())
while t > 0:
	n = int(input())
	check = [False] * n
	l = list(map(int, input().split()))
	d = {}
	mini = l[0]
	for i in range(n):
		mini = min(mini, l[i])
		if l[i] in d:
			d[l[i]] += 1
		else:
			d[l[i]] = 1
	k = 0
	for i in sorted(d.keys()):
		while d[i] != 0:
			if l[k] == i:
				check[k] = True
			k += 1
			d[i] -= 1
	for i in range(n):
		if check[i] == False:
			if gcd(l[i], mini) != mini:
				print('NO')
				break
	else:
		print('YES')
	t -= 1


for _ in range(int(input())):
	n = int(input())
	ar1 = list(map(int, input().split()))
	ar2 = [e for e in ar1]
	ar2.sort()
	m = ar2[0]
	if any([ar1[i] % m != 0 and ar1[i] != ar2[i] for i in range(n)]):
		print('NO')
	else:
		print('YES')


from sys import stdin, stdout

def main():
	t = int(stdin.readline())
	for _ in range(t):
		n = int(stdin.readline())
		array = [int(i) for i in stdin.readline().split()]
		smallest = array[0]
		ordered = True
		for i in range(1, n):
			if array[i] < smallest:
				smallest = array[i]
			if ordered and array[i] < array[i - 1]:
				ordered = False
		if ordered:
			result = 'YES\n'
		else:
			orderable = True
			s = sorted(array)
			for i in range(n):
				val = array[i]
				if val % smallest != 0:
					if s[i] != val:
						orderable = False
						break
			if orderable:
				result = 'YES\n'
			else:
				result = 'NO\n'
		stdout.write(result)
main()


for _ in range(int(input())):
	flag = 0
	n = int(input())
	a = [int(x) for x in input().split()]
	b = sorted(a)
	k = b[0]
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % k != 0:
				flag = 1
				break
	if flag:
		print('NO')
	else:
		print('YES')


import sys
import collections

def input():
	return sys.stdin.readline().rstrip()

def split_input():
	return [int(i) for i in input().split()]
tests = int(input())
for _ in range(tests):
	n = int(input())
	a = split_input()
	b = list(a)
	m = min(a)
	b.sort()
	for i in range(n):
		if b[i] != a[i] and (a[i] % m != 0 or b[i] % m != 0):
			print('NO')
			break
	else:
		print('YES')


from math import gcd
for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	lis = sorted(li)
	fg = 0
	m = min(li)
	for i in range(n):
		if li[i] != lis[i]:
			if li[i] % m != 0:
				fg = 1
				break
	if fg == 1:
		print('No')
	else:
		print('Yes')


for t in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split(' ', n - 1)]
	m = min(a)
	b = sorted([x for x in a if x % m == 0])
	j = 0
	for i in range(len(a)):
		if a[i] % m == 0:
			a[i] = b[j]
			j = j + 1
	if a == sorted(a):
		print('YES')
	else:
		print('NO')


import sys
import random
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
T = inp()

def gcd(a, b):
	if a == 0:
		return (b, 0, 1)
	(g, x1, y1) = gcd(b % a, a)
	x = y1 - b // a * x1
	y = x1
	return (g, x, y)

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for t in range(T):
	N = inp()
	A = inlt()
	n = min(A)
	B = []
	for a in A:
		B.append(a)
	B.sort()
	fl = True
	for a in range(N):
		if A[a] != B[a]:
			if gcd(A[a], n)[0] != n:
				fl = False
				break
	if fl:
		print('YES')
	else:
		print('NO')


import math
for x in range(int(input())):
	n = int(input())
	m = list(map(int, input().split()))
	a = sorted(m)
	r = 0
	for i in range(n):
		if m[i] != a[i]:
			if math.gcd(a[i], a[0]) < a[0]:
				r = 1
				break
	if r == 1:
		print('NO')
	else:
		print('YES')


def wrongplace(x, y):
	z = []
	for i in range(len(x)):
		if y[i] == x[i]:
			z.append(0)
		else:
			z.append(-1)
	return z

def divisible(x, num):
	z = []
	for i in range(len(x)):
		if x[i] % num == 0:
			z.append(0)
		else:
			z.append(-1)
	return z

def cmp(x, y):
	for i in range(len(x)):
		if x[i] == y[i]:
			if x[i] == -1:
				return False
	return True
n = int(input())
for i in range(n):
	z = []
	k = []
	k = int(input())
	v = list(map(int, input().split()))
	v1 = v.copy()
	v.sort()
	z = wrongplace(v, v1)
	k = divisible(v1, v[0])
	if cmp(k, z):
		print('YES')
	else:
		print('NO')


from sys import stdin
import math
ip = stdin.readline
for _ in range(int(ip())):
	n = int(ip())
	a = [[int(x), i] for (i, x) in enumerate(ip().split())]
	a.sort()
	mn = a[0][0]
	flag = True
	for j in range(n):
		(x, i) = a[j]
		if not j == i and x % mn:
			flag = False
			break
	print('Yes' if flag else 'No')


t = int(input())
for i in range(t):
	n = int(input())
	ar = [int(i) for i in input().split()]
	cop = ar.copy()
	cop.sort()
	f = cop[0]
	fix = []
	we = True
	for x in range(n):
		if ar[x] != cop[x]:
			fix.append(ar[x])
		else:
			pass
	for b in fix:
		if b % f == 0:
			pass
		else:
			print('NO')
			we = False
			break
	if we == False:
		pass
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	ref_arr = sorted(arr)
	m = min(arr)
	flag = 0
	for i in range(n):
		if arr[i] != ref_arr[i]:
			if arr[i] % m != 0:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


import sys
import math

def pro(arr):
	n = len(arr)
	f = 1
	for i in range(n - 1):
		if arr[i] > arr[i + 1]:
			f = 0
			break
	if f:
		print('YES')
		return
	lst = sorted(arr)
	mini = min(arr)
	for i in range(n):
		if arr[i] != lst[i]:
			if arr[i] % mini:
				print('NO')
				return
	print('YES')
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	pro(arr)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	minn = min(arr)
	sarr = sorted(arr)
	flag = 1
	for i in range(n):
		if arr[i] % minn != 0:
			if sarr[i] != arr[i]:
				print('NO')
				flag = 0
				break
	if flag:
		print('YES')


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print('YES')
	else:
		arr1 = sorted(arr)
		flag = True
		m = arr1[0]
		for i in range(n):
			if arr1[i] != arr[i] and arr[i] % m != 0:
				print('NO')
				flag = False
				break
		if flag:
			print('YES')


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sarr = sorted(arr)
	mn = min(arr)
	ok = True
	mrk = [1] * n
	for i in range(n):
		if arr[i] % mn != 0:
			mrk[i] = 0
	for i in range(n):
		if arr[i] != sarr[i]:
			if mrk[i] == 0:
				ok = False
				break
	if ok:
		print('YES')
	else:
		print('NO')


def solution(arr, ln):
	s_arr = sorted(arr)
	checker = [i for i in range(ln) if arr[i] != s_arr[i]]
	min_div = s_arr[0]
	for i in checker:
		if arr[i] % min_div != 0:
			return 'No'
	return 'Yes'

def main():
	for test in range(int(input())):
		ln = int(input())
		arr = list(map(int, input().split()))
		print(solution(arr, ln))
main()


def gt():
	return list(map(int, input().split()))
(t,) = gt()
for _ in range(t):
	n = gt()
	ar = gt()
	tmp = ar.copy()
	tmp.sort()
	mi = min(ar)
	for i in range(len(ar)):
		if ar[i] % mi > 0 and tmp[i] != ar[i]:
			print('NO')
			break
	else:
		print('YES')


def check(a, b, n):
	m = min(a)
	c = []
	d = []
	for i in range(n):
		if a[i] == b[i] or a[i] % m != 0:
			c.append(a[i])
		else:
			c.append(-1)
			d.append(a[i])
	d.sort()
	j = 0
	for i in range(n):
		if c[i] == -1:
			c[i] = d[j]
			j = j + 1
	if c == b:
		print('YES')
	else:
		print('NO')
for z in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	check(a, b, n)


import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

def solve(lst):
	console('----- solving ------')
	console(lst)
	if sorted(lst) == lst:
		return 'YES'
	minlst = min(lst)
	moveable_indexes = [i for (i, x) in enumerate(lst) if x % minlst == 0]
	moveable_subsegment = [x for x in lst if x % minlst == 0]
	moveable_subsegment = sorted(moveable_subsegment)
	lst2 = [x for x in lst]
	for (i, j) in enumerate(moveable_indexes):
		lst2[j] = moveable_subsegment[i]
	console(lst2)
	if sorted(lst2) == lst2:
		return 'YES'
	return 'NO'

def console(*args):
	return
for case_num in range(int(input())):
	_ = int(input())
	lst = list(map(int, input().split()))
	res = solve(lst)
	print(res)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(sorted(a))
	ch = 0
	for i in range(n):
		if a[i] == b[i]:
			continue
		if a[i] % b[0] != 0:
			ch = 1
			break
	if ch:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = sorted(l)
	f = 'YES'
	m = a[0]
	for i in range(n):
		if l[i] != a[i]:
			if a[i] % m != 0 or l[i] % m != 0:
				f = 'NO'
				print(f)
				break
	if f == 'YES':
		print(f)


import math

def merge(arr, l, m, r):
	n1 = m - l + 1
	n2 = r - m
	L = [0] * n1
	R = [0] * n2
	for i in range(0, n1):
		L[i] = arr[l + i]
	for j in range(0, n2):
		R[j] = arr[m + 1 + j]
	i = 0
	j = 0
	k = l
	while i < n1 and j < n2:
		if L[i] <= R[j]:
			arr[k] = L[i]
			i += 1
		else:
			arr[k] = R[j]
			j += 1
		k += 1
	while i < n1:
		arr[k] = L[i]
		i += 1
		k += 1
	while j < n2:
		arr[k] = R[j]
		j += 1
		k += 1

def mergeSort(arr, l, r):
	if l < r:
		m = (l + (r - 1)) // 2
		mergeSort(arr, l, m)
		mergeSort(arr, m + 1, r)
		merge(arr, l, m, r)
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lis = []
	for j in range(n):
		lis.append(arr[j])
	mergeSort(lis, 0, n - 1)
	num = []
	s = 0
	for j in range(n):
		if arr[j] != lis[j]:
			num.append(j)
			s += 1
	if s == 0:
		print('YES')
	else:
		num1 = []
		for j in range(s):
			num1.append(arr[num[j]])
		mergeSort(num1, 0, s - 1)
		lis = []
		x = 0
		y = min(arr)
		for j in range(s):
			if num1[j] % y != 0:
				x += 1
				break
		if x == 0:
			print('YES')
		else:
			print('NO')


from math import *
from collections import *
from random import *
from decimal import Decimal
from heapq import *
from bisect import *
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 5)

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())

def inp():
	return int(input())

def st1():
	return input().rstrip('\n')
t = inp()
while t:
	t -= 1
	n = inp()
	a = lis()
	z = min(a)
	b = sorted(a)
	c = []
	ma = []
	for i in range(n):
		if b[i] != a[i]:
			c.append(a[i])
		else:
			ma.append(a[i])
	re = 0
	for i in c:
		re = gcd(i, re)
	flag = 0
	if re == z:
		flag = 1
	for i in ma:
		x = gcd(re, i)
		if x == z:
			flag = 1
	if flag or n == 1 or b == a:
		print('YES')
	else:
		print('NO')


from collections import defaultdict as dd
import math
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	mini = min(l)
	lcopy = l[:]
	lcopy.sort()
	new_list = []
	flag = 0
	for i in range(n):
		if l[i] != lcopy[i]:
			new_list.append(l[i])
	if len(new_list) == 0:
		print('YES')
		continue
	for i in range(len(new_list)):
		if new_list[i] % mini != 0:
			flag = 1
	if flag == 1:
		print('NO')
	else:
		print('YES')


T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	mn = min(A)
	B = sorted(A)
	flag = 'YES'
	for i in range(N):
		if A[i] != B[i] and A[i] % mn != 0:
			flag = 'NO'
	print(flag)


def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		w = min(a)
		(l1, l2, l3) = ([], [], [])
		for i in range(n):
			if w == a[i]:
				l1.append(i)
			elif a[i] % w == 0:
				l2.append(i)
				l3.append(a[i])
		(x1, y1) = (len(l1), len(l2))
		b = sorted(a)
		l1.extend(l2)
		l1.sort()
		l3.sort()
		(j, k) = (0, 0)
		for i in l1:
			if k < x1:
				a[i] = w
				k += 1
			else:
				a[i] = l3[j]
				j += 1
		if a == b:
			print('YES')
		else:
			print('NO')
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = sorted(l)
	if l == l1:
		print('YES')
	else:
		m = min(l)
		for (i, j) in enumerate(l):
			if j % m != 0:
				if l[i] != l1[i]:
					print('NO')
					break
		else:
			print('YES')


import sys
from collections import defaultdict as dd
from collections import deque
from functools import *
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from math import *
from itertools import permutations

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def inc(d, c):
	d[c] = d[c] + 1 if c in d else 1

def bo(i):
	return ord(i) - ord('A')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j):
	return 0 <= i < n and 0 <= j < n

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
while t > 0:
	t -= 1
	n = fi()
	a = li()
	b = sorted(a)
	flag = 0
	p = min(a)
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % p:
				flag = 1
				break
	print('NO' if flag else 'YES')


def yesno(a):
	mini = a[0]
	non_decreasing = True
	for (i, x) in enumerate(a):
		if i > 0 and x < a[i - 1]:
			non_decreasing = False
		if x < mini:
			mini = x
	if non_decreasing:
		return 'YES'
	if a[0] % mini != 0:
		return 'NO'
	stuck = []
	mobile = []
	count = 0
	slots = dict()
	for (j, x) in enumerate(a):
		if x % mini == 0:
			count += 1
			mobile.append(x)
		elif len(stuck) > 0 and x < stuck[-1]:
			return 'NO'
		elif len(stuck) > 0 and x == stuck[-1]:
			if count > 0:
				return 'NO'
		else:
			stuck.append(x)
			slots[x] = count
			count = 0
	last_stuck = 0
	for stuck_one in stuck:
		slots_needed = len([x for x in mobile if x < stuck_one and x > last_stuck])
		if slots_needed != slots[stuck_one]:
			return 'NO'
		last_stuck = stuck_one
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	print(yesno(a))


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()

def p(f):
	print('Yes' if f else 'No')

def solve():
	n = II()
	a = LI()
	c = sorted(a)
	boo = True
	m = c[0]
	for i in range(n):
		if a[i] != c[i] and c[i] % m != 0:
			boo = False
			break
	p(boo)
tests = 1
t = 1
if tests:
	t = II()
for q in range(t):
	solve()


import math
t = int(input())
for q in range(t):
	n = int(input())
	L = [int(i) for i in input().split()]
	G = list(sorted(L))
	f = min(L)
	t = True
	for i in range(n):
		if L[i] % f != 0 and G[i] != L[i]:
			t = False
	if t:
		print('YES')
	else:
		print('NO')


t = int(input())

def solve():
	n = int(input())
	a = [int(i) for i in input().split()]
	k = min(a)
	ans = []
	comp = []
	for i in range(n):
		if a[i] % k == 0:
			comp.append(a[i])
			ans.append(-1)
		else:
			ans.append(a[i])
	comp.sort()
	i = 0
	j = 0
	while i < n:
		if ans[i] == -1:
			ans[i] = comp[j]
			j += 1
		i += 1
	a.sort()
	if ans == a:
		print('YES')
	else:
		print('NO')
while t:
	t -= 1
	solve()


import math
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	br = ar[:]
	br.sort()
	ct = min(ar)
	for i in range(n):
		if ar[i] != br[i]:
			ct = math.gcd(ct, ar[i])
	if ct == min(ar):
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout, setrecursionlimit

class Tail_Recursion_Optimization:

	def __init__(self, RECURSION_LIMIT, STACK_SIZE):
		setrecursionlimit(RECURSION_LIMIT)
		threading.stack_size(STACK_SIZE)
		return None

class SOLVE:

	def solve(self):
		R = stdin.readline
		W = stdout.write
		ans = []
		for i in range(int(R())):
			n = int(R())
			a = [int(x) for x in R().split()]
			b = a.copy()
			b.sort()
			mn = min(a)
			flg = 1
			for j in range(n):
				if a[j] != b[j]:
					if b[j] % mn:
						flg = 0
						break
			ans.append(['NO', 'YES'][flg])
		W('\n'.join(ans))
		return 0

def main():
	s = SOLVE()
	s.solve()
main()


from sys import stdin, stdout
t = int(stdin.readline())
for case in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	mnm = min(a)
	pair = [(a[i], i) for i in range(n)]
	pair.sort()
	ok = True
	for i in range(n):
		if pair[i][1] != i and pair[i][0] != a[i]:
			if pair[i][0] % mnm != 0:
				ok = False
				break
	if not ok:
		print('NO')
	else:
		print('YES')


from sys import stdin
input = lambda : stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	lst = [int(i) for i in input().split()]
	if n == 1:
		print('YES')
	else:
		min1 = min(lst)
		lst2 = []
		for i in lst:
			lst2.append(i)
		lst2.sort()
		nd = 0
		for i in range(0, n):
			if lst2[i] == lst[i]:
				pass
			elif lst2[i] % min1 == 0 and lst[i] % min1 == 0:
				pass
			else:
				nd = 1
				print('NO')
				break
		if nd == 0:
			print('YES')


t = int(input())
for tc in range(t):
	n = int(input())
	line = input().split()
	arr = []
	for i in range(n):
		arr.append(int(line[i]))
	min_val = min(arr)
	divisible = []
	divisible_indices = []
	for i in range(n):
		if arr[i] % min_val == 0:
			divisible_indices.append(i)
			divisible.append(arr[i])
	divisible.sort()
	p = 0
	for i in divisible_indices:
		arr[i] = divisible[p]
		p += 1
	if arr == sorted(arr):
		print('YES')
	else:
		print('NO')


from math import gcd
t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	cpy = arr.copy()
	cpy.sort()
	mn = cpy[0]
	val = 'YES'
	for idx in range(n):
		if cpy[idx] != arr[idx]:
			if cpy[idx] % mn or arr[idx] % mn:
				val = 'NO'
				break
	ans.append(val)
for x in ans:
	print(x)


for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	targets = sorted(nums)
	possible = True
	for (target, current) in zip(targets, nums):
		if target % targets[0] != 0:
			if current != target:
				possible = False
				break
		elif current % targets[0] != 0:
			possible = False
			break
	print('YES' if possible else 'NO')


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	m = min(arr)
	arr2 = sorted(arr)
	possible = True
	for i in range(len(arr)):
		if arr[i] != arr2[i] and arr[i] % m > 0:
			possible = False
	if possible == False:
		print('NO')
	else:
		print('YES')


import sys
readline = sys.stdin.buffer.readline

def even(n):
	return 1 if n % 2 == 0 else 0
query = int(readline())

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def func(lst):
	res = sorted(lst)
	m = min(lst)
	for i in range(len(lst)):
		if lst[i] != res[i]:
			if gcd(lst[i], m) != m:
				print('NO')
				return
	print('YES')
for _ in range(query):
	n = int(readline())
	lst1 = list(map(int, readline().split()))
	func(lst1)


for k in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = sorted(l)
	m = c[0]
	ans = True
	for i in range(n):
		if l[i] != c[i]:
			if c[i] % m != 0:
				print('NO')
				ans = False
				break
	if ans:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if l == sorted(l):
		print('YES')
	else:
		temp = sorted(l)
		mini = min(l)
		flag = 0
		for i in range(len(l)):
			if l[i] != temp[i] and l[i] % mini != 0:
				flag = 1
				break
		if flag == 1:
			print('NO')
		else:
			print('YES')


import sys
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

def input():
	return fast_reader().strip()

def print(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer('\n')
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if l == sorted(l) or 1 in l:
		print('YES')
		continue
	else:
		a = sorted(l)
		ans = 'YES'
		for i in range(n):
			if a[i] != l[i]:
				if a[i] % a[0] != 0:
					ans = 'NO'
					break
		print(ans)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	p = sorted(a)
	pos = []
	m = min(a)
	f = 0
	for i in range(len(a)):
		if a[i] % m != 0:
			pos.append(i)
	for i in pos:
		if p[i] == a[i]:
			continue
		else:
			f += 1
			break
	if f:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = sorted(l)
	m = p[0]
	f = 1
	for i in range(n):
		if l[i] != p[i]:
			if l[i] % m != 0 or p[i] % m != 0:
				f = 0
				break
	if f:
		print('YES')
	else:
		print('NO')


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def sumDigits(no):
	return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
MOD = 1000000007
PMOD = 998244353
N = 10 ** 5
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	A = a.copy()
	A.sort()
	mn = min(a)
	flag = True
	for i in range(n):
		if A[i] != a[i] and A[i] % mn != 0:
			flag = False
	if flag:
		print('YES')
	else:
		print('NO')


import bisect
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = sorted(l)
	m = s[0]
	f = 0
	for i in range(n):
		j1 = bisect.bisect_right(s, l[i])
		j2 = bisect.bisect_left(s, l[i])
		if (i > j1 - 1 or i < j2) and l[i] % m != 0:
			f = 1
			break
	if f:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = [int(j) for j in input().split()]
	l1 = l.copy()
	if n == 1:
		print('YES')
	else:
		c = 0
		for i in range(1, n):
			if l[i] >= l[i - 1]:
				c += 1
		if c == n - 1:
			print('YES')
		else:
			f = 0
			l1.sort()
			m = l1[0]
			for i in range(n):
				if l[i] != l1[i]:
					if l[i] % m != 0:
						f = 1
						break
			if f == 0:
				print('YES')
			else:
				print('NO')


import math
t = int(input())
while t:
	n = int(input())
	A = [int(x) for x in input().split(' ')]
	B = []
	for x in A:
		B.append(x)
	flag = True
	gcd = min(A)
	B.sort()
	for i in range(0, n):
		if A[i] != B[i]:
			if A[i] % gcd + B[i] % gcd != 0:
				flag = False
	if flag:
		print('YES')
	else:
		print('NO')
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mn = min(a)
	b = sorted(a)
	check = False
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % mn != 0:
				check = True
				break
	if check:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	c = sorted(arr)
	k = c[0]
	f = 0
	for i in range(n):
		if c[i] != arr[i]:
			if c[i] % k != 0 or arr[i] % k != 0:
				f = 1
				break
	if f:
		print('NO')
	else:
		print('YES')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
from collections import Counter, defaultdict, deque

def int1():
	return int(input())

def map1():
	return map(int, input().split())

def list1():
	return list(map(int, input().split()))
mod = pow(10, 9) + 7

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def solve():
	n = int1()
	l1 = list1()
	min1 = min(l1)
	x = 0
	for i in range(n):
		x = gcd(x, l1[i])
	if min1 == x:
		print('YES')
	else:
		l2 = l1.copy()
		l2.sort()
		for i in range(n):
			if l1[i] == l2[i]:
				continue
			elif l1[i] != l2[i] and l1[i] % min1 != 0:
				print('NO')
				return
		print('YES')
for _ in range(int(input())):
	solve()


import sys
read = lambda : sys.stdin.readline().strip()

def gcd(a, b):
	while a != b:
		if a > b:
			a = a - b
		else:
			b = b - a
	return a

def isValid(errs, k, min_gcd):
	for i in range(k):
		for j in range(k):
			if i == j:
				continue
			if gcd(errs[i], errs[j]) != min_gcd:
				break
		else:
			return True
	return False
for _ in range(int(read())):
	n = int(read())
	nums = list(map(int, read().split()))
	sorted_nums = sorted(nums)
	min_num = sorted_nums[0]
	errs = [0] * (n + 1)
	k = 0
	for i in range(n):
		if nums[i] != sorted_nums[i]:
			errs[k] = nums[i]
			k += 1
			if nums[i] % min_num != 0:
				print('NO')
				break
	else:
		errs[k] = 1000000000.0 + 1
		print('YES')


import sys
input = sys.stdin.readline
T = int(input())
for t in range(T):
	N = int(input())
	A = [int(_) for _ in input().split()]
	ma = min(A)
	last_seen = 0
	o = []
	done = False
	for el in A:
		if el % ma:
			if el < last_seen:
				print('NO')
				done = True
				break
			last_seen = el
		else:
			o.append(el)
	if done:
		continue
	o = sorted(o)
	i = 0
	last_seen = 0
	for el in A:
		if el % ma:
			current = el
		else:
			current = o[i]
			i += 1
		if current < last_seen:
			print('NO')
			break
		last_seen = current
	else:
		print('YES')


t = int(input())
for r in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	m = min(a)
	x = []
	y = []
	for i in range(n):
		if a[i] % m == 0:
			x.append(i)
			y.append(a[i])
	y = sorted(y)
	for i in range(len(x)):
		a[x[i]] = y[i]
	if sorted(a) == a:
		print('Yes')
	else:
		print('No')


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	d = []
	mi = min(a)
	for i in range(n):
		if a[i] != b[i]:
			d.append(a[i])
	hcf = mi
	for i in range(len(d)):
		hcf = gcd(hcf, d[i])
	if hcf != mi:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = sorted(a)
	m = b[0]
	for (i, k) in enumerate(a):
		if k % m and b[i] != k:
			print('NO')
			break
	else:
		print('YES')


t = int(input())
for asd in range(t):
	n = int(input())
	a = [None] * n
	minim = 1000000000.0 + 5
	ams = input()
	tmp = ams.split(' ')
	for i in range(n):
		a[i] = int(tmp[i])
		minim = min(minim, a[i])
	b = a.copy()
	a.sort()
	bisa = True
	for i in range(n):
		if a[i] % minim != 0:
			if a[i] != b[i]:
				bisa = False
	if bisa:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	copy = a[:]
	copy.sort()
	mini = min(a)
	if a == copy:
		print('YES')
		continue
	for i in range(n):
		if a[i] != copy[i]:
			if a[i] % mini != 0:
				print('NO')
				break
	else:
		print('YES')


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = a.copy()
	a.sort()
	l = []
	m = 0
	for i in range(n):
		if a[i] != b[i]:
			l.append(a[i])
			m += 1
	m_gcd = a[0]
	for j in range(m):
		if l[j] % m_gcd != 0:
			print('NO')
			break
	else:
		print('YES')


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
for _ in range(L()[0]):
	n = L()[0]
	A = L()
	B = sorted(A)
	notpos = 0
	for i in range(n):
		if A[i] != B[i]:
			if A[i] % B[0] != 0:
				notpos = 1
				break
	if notpos:
		print('NO')
	else:
		print('YES')
endtime = time.time()


from typing import List

def mere_array(arr: List[int]) -> str:
	minimum = min(arr)
	sorted_arr = sorted(arr)
	for i in range(len(arr)):
		if arr[i] != sorted_arr[i]:
			if arr[i] % minimum != 0 or sorted_arr[i] % minimum != 0:
				return 'NO'
	return 'YES'
t = int(input())
for _ in range(t):
	n = int(input())
	print(mere_array(list(map(int, input().split(' ')))))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	min_element = min(a)
	bckt = []
	for i in range(n):
		if a[i] % min_element == 0:
			bckt.append(a[i])
			a[i] = -1
	bckt.sort(reverse=True)
	for i in range(n):
		if a[i] == -1:
			a[i] = bckt.pop()
	ok = True
	for i in range(1, n):
		if a[i] < a[i - 1]:
			ok = False
	print('YES' if ok else 'NO')


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
from copy import deepcopy

def gift():
	for _ in range(t):
		n = int(input())
		arry = list(map(int, input().split()))
		arryS = deepcopy(arry)
		arryS.sort()
		minele = arryS[0]
		ans = 'YES'
		for i in range(n):
			if arry[i] == arryS[i] or arry[i] % minele == 0:
				continue
			else:
				ans = 'NO'
				break
		yield ans
t = int(input())
ans = gift()
print(*ans, sep='\n')


from sys import stdin, stdout

class Input:

	def __init__(self):
		self.lines = stdin.read().splitlines()
		self.idx = 0

	def line(self):
		try:
			return self.lines[self.idx]
		finally:
			self.idx += 1

	def array(self, sep=' ', cast=int):
		return list(map(cast, self.line().split(sep=sep)))

	def known_tests(self):
		(num_of_cases,) = self.array()
		for case in range(num_of_cases):
			yield self

	def unknown_tests(self):
		while self.idx < len(self.lines):
			yield self

class Problem:

	def __init__(self):
		self.oo = float('inf')

	def gcd(self, a, b):
		return a if b == 0 else self.gcd(b, a % b)

	def solve(self, n, a):
		b = sorted(a)
		c = []
		for i in range(len(a)):
			if a[i] != b[i]:
				c.append(b[i])
		ok = True
		for i in range(len(c)):
			if self.gcd(b[0], c[i]) != b[0]:
				ok = False
		print('YES' if ok else 'NO')
try:
	p = Problem()
	for tc in Input().known_tests():
		p.solve(*tc.array(), tc.array())
except Exception as e:
	import traceback
	traceback.print_exc(file=stdout)


import math

def allPermutations(string):
	listi = permutations(string)
	for i in list(listi):
		print(''.join(i))
from operator import add
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(n):
		b.append(a[i])
	b.sort()
	if b != a:
		mini = min(a)
		for i in range(n):
			if math.gcd(a[i], mini) != mini and a[i] != b[i]:
				print('NO')
				break
			if i == n - 1:
				print('YES')
	else:
		print('YES')


import math
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	a.sort()
	m = min(b)
	A = []
	c = []
	if a == b:
		print('YES')
	else:
		for i in range(len(a)):
			if a[i] != b[i]:
				A.append(a[i])
		for i in A:
			if i % m != 0:
				c.append(i)
		if len(c) == 0:
			print('YES')
		else:
			print('NO')


def run():
	n = int(input())
	nums = list(map(int, input().split()))
	nn = sorted(nums)
	Min = nn[0]
	for i in range(n):
		if nums[i] % Min != 0 and nums[i] != nn[i]:
			print('NO')
			return
	print('YES')
t = int(input())
for i in range(t):
	run()


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	t = min(a)
	d = []
	f = 1
	for i in a:
		d.append(i)
	d = sorted(d)
	for i in range(n):
		if a[i] != d[i]:
			if math.gcd(t, a[i]) != t:
				f = 0
				break
	if f == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	r = sorted(l)
	flag = 1
	for i in range(n):
		if l[i] % r[0] != 0 and l[i] != r[i]:
			print('No')
			flag = 0
			break
	if flag == 1:
		print('Yes')


import sys
import math
sys.setrecursionlimit(10000)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	min_el = min(a)
	diff = []
	for i in range(n):
		if a[i] != b[i]:
			diff.append(a[i])
	allyes = True
	for i in diff:
		if math.gcd(i, min_el) != min_el:
			allyes = False
			break
	if allyes:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = [*map(int, input().split())]
	idx = []
	ele = []
	min_ele = min(arr)
	for (i, j) in enumerate(arr):
		if j % min_ele == 0:
			idx.append(i)
			ele.append(j)
	ele.sort(reverse=True)
	for i in idx:
		arr[i] = ele[-1]
		del ele[-1]
	print('YES' if arr == sorted(arr) else 'NO')


for i in range(int(input())):
	n = int(input())
	t = list(map(int, input().split()))
	a = min(t)
	if a == 1 or t == sorted(t):
		print('YES')
	else:
		f = sorted(t)
		import math
		h = 0
		for j in range(n):
			if t[j] % a == 0 or t[j] == f[j]:
				continue
			else:
				print('NO')
				h += 1
				break
		if h == 0:
			print('YES')


from sys import stdin, stdout
from math import gcd
input = stdin.readline
print = stdout.write

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	brr = arr.copy()
	arr.sort()
	mn = arr[0]
	for i in range(n):
		if arr[i] == brr[i]:
			continue
		if arr[i] % mn == 0 and brr[i] % mn == 0:
			continue
		print('NO\n')
		return
	print('YES\n')
	return
test = 1
test = int(input())
for _ in range(test):
	main()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	x = min(arr)
	arr1 = sorted(arr)
	ans = 'YES'
	for i in range(n):
		if arr1[i] != arr[i]:
			if arr1[i] % x != 0 or arr[i] % x != 0:
				ans = 'NO'
				break
	print(ans)


from math import gcd
from collections import deque
import sys

def main():
	t = Iint()
	for _ in range(t):
		n = Iint()
		a = Ilist()
		min_a = min(a)
		b = sorted(a)
		b.sort()
		for (ai, bi) in zip(a, b):
			if ai != bi:
				if ai % min_a != 0:
					break
				elif bi % min_a != 0:
					break
		else:
			print('YES')
			continue
		print('NO')
FIO = 10

def I():
	return input()

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))
en = enumerate

def answer_debug(*args):
	print('\x1b[31m', *args, '\x1b[0m', file=sys.stderr)

def debug(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
if not FIO:
	if __name__ == '__main__':
		main()
		exit()
else:
	from io import BytesIO, IOBase
	import os
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


def gcd(a, b):
	if a % b == 0:
		return b
	return gcd(b, a % b)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		b = a.copy()
		b.sort()
		minn = min(b)
		flag = True
		for i in range(n):
			if a[i] != b[i]:
				gd = gcd(a[i], b[i])
				if gd % minn != 0:
					flag = False
					break
				elif gd == minn:
					continue
				elif b[i] % minn == 0:
					continue
				else:
					flag = False
					break
		if flag:
			print('YES')
		else:
			print('NO')
main()


from math import gcd
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	s = sorted(l)
	if 1 in l:
		print('YES')
		continue
	for i in range(n):
		if gcd(s[0], l[i]) != s[0] and s[i] != l[i]:
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if arr == sorted(arr):
		print('YES')
	else:
		minn = min(arr)
		t = []
		ind = []
		v = []
		for i in range(n):
			if arr[i] % minn == 0:
				t += [arr[i]]
				ind += [i]
			else:
				v += [[arr[i], i]]
		t.sort()
		temp = [0] * n
		for i in range(len(ind)):
			temp[ind[i]] = t[i]
		for i in v:
			temp[i[1]] = i[0]
		if temp == sorted(arr):
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	ls = list(map(int, input().split()))
	ar = []
	for i in range(n):
		ar.append(ls[i])
	ar.sort()
	if n == 1:
		print('YES')
		continue
	mn = ar[0]
	f = True
	for i in range(n):
		if ar[i] == ls[i]:
			continue
		elif ls[i] % mn == 0:
			continue
		else:
			f = False
			break
	if f:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	lr = l[:]
	lr.sort()
	small = min(l)
	f = 0
	if lr == l:
		print('YES')
	else:
		for i in range(n):
			if l[i] != lr[i]:
				if l[i] % small != 0:
					f = 1
					break
		if f == 0:
			print('YES')
		else:
			print('NO')
	t -= 1


def solve():
	n = int(input())
	arr = [int(v) for v in input().split()]
	sarr = sorted(arr)
	for i in range(n):
		if arr[i] != sarr[i] and arr[i] % sarr[0] != 0:
			print('NO')
			return
	print('YES')
t = int(input())
for _ in range(t):
	solve()


import math
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	if a == b:
		print('YES')
	else:
		m = min(a)
		flag = 0
		for i in range(len(a)):
			if a[i] != b[i] and a[i] % m != 0:
				flag = 1
				break
		if flag == 1:
			print('NO')
		else:
			print('YES')


t = int(input())
answer = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	bool = True
	mini = min(a)
	fixed = []
	for i in range(n):
		if a[i] % mini:
			fixed.append((i, a[i]))
	a.sort()
	for element in fixed:
		if a[element[0]] != element[1]:
			bool = False
			break
	if bool:
		answer.append('YES')
	else:
		answer.append('NO')
print('\n'.join(answer))


from math import gcd
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	xl = sorted(l)
	m = min(l)
	if xl == l:
		print('YES')
		continue
	(c1, c2) = (0, 0)
	for i in range(n):
		if l[i] != xl[i]:
			if l[i] % m == 0:
				continue
			else:
				print('NO')
				break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	mn = min(lst)
	temp = sorted(lst)
	ans = 'YES'
	for i in range(n):
		if lst[i] != temp[i] and lst[i] % mn != 0:
			ans = 'NO'
			break
	print(ans)


import sys
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = sorted(a)[:]
	minn = b[0]
	chk = []
	flag = 1
	for i in range(n):
		if a[i] != b[i] and a[i] % minn != 0:
			flag = 0
	print(['NO', 'YES'][flag])


import random

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	minn = min(a)
	b = []
	f = 0
	for i in a:
		b.append(i)
	b.sort()
	inds = []
	divs = []
	for i in range(n):
		if a[i] % minn == 0:
			inds.append(i)
			divs.append(a[i])
	divs.sort()
	for i in range(len(inds)):
		a[inds[i]] = divs[i]
	for i in range(n):
		if a[i] != b[i]:
			f = 1
			break
	if f:
		print('NO')
	else:
		print('YES')


import bisect
import sys
input = sys.stdin.buffer.readline
T = int(input())
for testcase in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	b = sorted(a)
	flag = [False] * n
	g = min(a)
	for i in range(n):
		if a[i] % g == 0:
			flag[i] = True
	ans = True
	for i in range(n):
		k = bisect.bisect_left(b, a[i])
		b[k] -= 1
		if a[i] % g == 0:
			if not flag[k]:
				ans = False
		elif k != i:
			ans = False
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	sorte = arr[:]
	sorte.sort()
	mino = min(arr)
	flag = 0
	derem = []
	for i in range(n):
		if arr[i] != sorte[i]:
			if arr[i] % mino != 0:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	B = []
	USE = [0] * n
	MIN = min(A)
	for i in range(n):
		if A[i] % MIN == 0:
			USE[i] = 1
			B.append(A[i])
	B.sort()
	ind = 0
	for i in range(n):
		if USE[i] == 1:
			A[i] = B[ind]
			ind += 1
	for i in range(1, n):
		if A[i] < A[i - 1]:
			print('NO')
			break
	else:
		print('YES')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)

def toord(c):
	return ord(c) - ord('a')

def lcm(a, b):
	return a * b // lcm(a, b)
mod = 998244353
INF = float('inf')
from math import factorial, sqrt, ceil, floor, gcd
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	for _ in range(N()):
		n = N()
		arr = RLL()
		sarr = sorted(arr)
		mi = sarr[0]
		for i in range(n):
			if arr[i] != sarr[i]:
				if arr[i] % mi != 0:
					print('NO')
					break
		else:
			print('YES')
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	small = b[0]
	can = True
	for (i, val) in enumerate(a):
		if b[i] != a[i]:
			if a[i] % small != 0:
				can = False
				break
	if can:
		print('YES')
	else:
		print('NO')


from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = [i for i in a]
	b.sort()
	m = min(a)
	ans = 'YES'
	for i in range(n):
		if a[i] % m != 0 and a[i] != b[i]:
			ans = 'NO'
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	mini = min(arr)
	arr2 = sorted(arr)
	st = 0
	for i in range(n):
		if arr[i] != arr2[i] and arr[i] % mini != 0:
			st = 1
			break
	if st == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	b = [0 for i in range(n)]
	x = min(l)
	m = []
	for i in range(n):
		if l[i] % x == 0:
			b[i] = 1
			m.append(l[i])
	m.sort()
	j = 0
	for i in range(n):
		if b[i]:
			l[i] = m[j]
			j += 1
	flag = 1
	for i in range(n - 1):
		if l[i] > l[i + 1]:
			print('NO')
			flag = 0
			break
	if flag:
		print('YES')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	br = ar.copy()
	br.sort()
	mi = min(ar)
	flag = True
	for i in range(n):
		if ar[i] != br[i]:
			if ar[i] % mi != 0:
				flag = False
				break
	if flag:
		print('YES')
	else:
		print('NO')


def gcd(a, b):
	if a % b == 0:
		return b
	elif b % a == 0:
		return a
	else:
		return gcd(b, a % b)
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	p = min(arr)
	q = []
	for i in range(n):
		if gcd(arr[i], p) == p:
			q.append(arr[i])
			arr[i] = -1
	q.sort()
	for i in range(n):
		if arr[i] == -1:
			arr[i] = q.pop(0)
	for i in range(1, n):
		if arr[i - 1] > arr[i]:
			print('NO')
			break
	else:
		print('YES')


from math import gcd
n = int(input())
for i in range(n):
	mm = int(input())
	a = list(map(int, input().split(' ')))
	b = sorted(a)
	m = min(a)
	flag = [0] * mm
	for j in range(mm):
		if gcd(m, a[j]) == m:
			flag[j] = 1
	my = 1
	for k in range(mm):
		if a[k] != b[k] and flag[k] == 0:
			my = 0
			break
	if my:
		print('YES')
	else:
		print('NO')


def main(x):
	flag = True
	y = sorted(x)
	m = min(y)
	for i in range(len(x)):
		if x[i] != y[i]:
			if x[i] % m != 0:
				flag = False
	return flag
t = int(input())
for i in range(2 * t):
	if i % 2 == 0:
		n = int(input())
	else:
		a = list(map(int, input().split()))
		if main(a):
			print('YES')
		else:
			print('NO')


testcases = int(input())
for testcase in range(testcases):
	n = int(input())
	temparr = input()
	temparr = temparr.split()
	arr = []
	for i in temparr:
		arr.append(int(i))
	sortedarr = sorted(arr)
	minelem = min(sortedarr)
	if n == 1:
		print('YES')
		continue
	flag = 0
	for i in range(n):
		if sortedarr[i] == arr[i]:
			continue
		if sortedarr[i] != arr[i] and arr[i] % minelem != 0:
			flag = 1
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline().strip())
	arr = list(map(int, stdin.readline().strip().split(' ')))
	narr = []
	ind = []
	t1 = min(arr)
	for i in range(n):
		if arr[i] % t1 == 0:
			narr.append(arr[i])
			ind.append(i)
	narr = sorted(narr)
	for i in range(len(ind)):
		arr[ind[i]] = narr[i]
	if arr == sorted(arr):
		stdout.write('YES\n')
	else:
		stdout.write('NO\n')


for ii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	x = b[0]
	if a == b:
		print('YES')
	else:
		c = 0
		for i in range(n):
			if a[i] != b[i]:
				if a[i] % x != 0:
					c += 1
					print('NO')
					break
		if c == 0:
			print('YES')


import math
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	min1 = min(l)
	l1 = []
	l1 = sorted(l)
	z = 0
	for j in range(n):
		if l[j] != l1[j]:
			if math.gcd(l[j], min1) != min1:
				z = 1
				break
	if z == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	arr2 = []
	if arr == sorted(arr):
		print('YES')
	else:
		mn = min(arr)
		for i in range(n):
			if arr[i] % mn == 0:
				arr2.append(arr[i])
				arr[i] = 0
		arr2.sort()
		for i in range(n):
			if arr[i] != 0:
				arr2.insert(i, arr[i])
		if arr2 == sorted(arr2):
			print('YES')
		else:
			print('NO')


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = lambda : sys.stdin.buffer.readline().rstrip()

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for _ in range(int(input())):
	q = int(input())
	w = list(map(int, input().split()))
	e = sorted(w)
	k = min(w)
	for i in range(q):
		if w[i] % k != 0:
			if w[i] != e[i]:
				print('NO')
				k = -3
				break
	if k != -3:
		print('YES')


t = int(input())
for i in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	l_ = []
	for i in range(0, n):
		l_.append(l[i])
	l_.sort()
	k = 0
	minm = l_[0]
	for i in range(0, n):
		if l[i] != l_[i] and l[i] % minm != 0:
			k = 1
	if k:
		print('NO')
	else:
		print('YES')


t = int(input())
for you in range(t):
	n = int(input())
	l = input().split()
	li = [int(i) for i in l]
	z = min(li)
	l1 = []
	hashimul = dict()
	for i in range(n):
		if li[i] % z == 0:
			l1.append(li[i])
			hashimul[i] = len(l1) - 1
	l1.sort()
	lfi = []
	for i in range(n):
		if i in hashimul:
			lfi.append(l1[hashimul[i]])
		else:
			lfi.append(li[i])
	poss = 1
	for i in range(1, n):
		if lfi[i] < lfi[i - 1]:
			poss = 0
			break
	if poss:
		print('YES')
	else:
		print('NO')


def checkKey(dict, key):
	if key in dict:
		return True
	return False

def h(arr, dummy_arr, free, n):
	for i in range(n):
		if free[i] == False:
			if arr[i] == dummy_arr[i]:
				continue
			else:
				return False
	return True

def helper(arr, n):
	for i in range(1, n):
		if arr[i] >= arr[i - 1]:
			continue
		else:
			return False
	return True
test = int(input())
for t_ in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	if helper(arr, n):
		print('YES')
		continue
	free = [False] * n
	m = min(arr)
	for i in range(n):
		if arr[i] % m == 0:
			free[i] = True
	dummy_arr = sorted(arr)
	if h(arr, dummy_arr, free, n):
		print('YES')
		continue
	else:
		print('NO')


for i in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	c = sorted(b)
	d = False
	e = min(b)
	for j in range(a):
		if b[j] == c[j]:
			pass
		elif c[j] % e != 0:
			print('NO')
			d = True
			break
	if d == False:
		print('YES')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	mini = min(a)
	ans = 'YES'
	for i in range(n):
		if a[i] == b[i]:
			pass
		elif a[i] % mini == 0:
			pass
		else:
			ans = 'NO'
			break
	print(ans)


from sys import stdin
input = stdin.readline

def mp():
	return map(int, input().split())

def it():
	return int(input())
for _ in range(it()):
	n = it()
	l = list(mp())
	k = sorted(l)
	mi = k[0]
	ans = 'YES'
	for (i, j) in zip(l, k):
		if i % mi and i != j:
			ans = 'NO'
			break
	print(ans)


T = int(input())
for t in range(1, T + 1):
	n = int(input())
	aArr = [int(inp) for inp in input().split()]
	m = min(aArr)
	multiplesOfM = []
	for a in aArr:
		if a % m == 0:
			multiplesOfM += [a]
	multiplesOfM = sorted(multiplesOfM)
	ind = 0
	ans = True
	for i in range(n):
		if aArr[i] % m == 0:
			aArr[i] = multiplesOfM[ind]
			ind += 1
		if i > 0 and aArr[i] < aArr[i - 1]:
			ans = False
			break
	print('YES' if ans else 'NO')


n = int(input())
for _ in range(n):
	t = int(input())
	ls = list(map(int, input().split()))
	mm = min(ls)
	arr = ls.copy()
	arr.sort()
	s = set()
	for (i, j) in zip(ls, arr):
		if i % mm != 0:
			if i != j:
				print('NO')
				break
	else:
		print('YES')


from collections import Counter, defaultdict, deque
import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	m = min(a)
	lock = [0 for i in range(n)]
	for i in range(n):
		if a[i] % m:
			lock[i] = 1
	b = sorted(a)
	for i in range(n):
		if lock[i] and a[i] != b[i]:
			print('no')
			return
	print('yes')
tt = int(input())
for test in range(tt):
	solve()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	t = [0] * n
	for i in range(n):
		t[i] = a[i]
	t.sort()
	m = t[0]
	flag = False
	for i in range(n):
		if a[i] != t[i]:
			if a[i] % m != 0:
				flag = True
				break
	if flag:
		print('NO')
	else:
		print('YES')


def solve(l, k, n):
	test = k[0]
	for i in range(n):
		if l[i] != k[i]:
			if l[i] % test != 0:
				return 'NO'
	return 'YES'
t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	k = sorted(l)
	print(solve(l, k, n))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().strip().split()))
	sortedL = l[:]
	sortedL.sort()
	mn = sortedL[0]
	unmovable = []
	for i in range(n):
		if l[i] % mn != 0:
			unmovable.append(i)
	can = True
	for i in unmovable:
		if sortedL[i] != l[i]:
			can = False
	if can:
		print('YES')
	else:
		print('NO')


def gcd(x, y):
	if x < y:
		(x, y) = (y, x)
	if x % y == 0:
		return y
	return gcd(y, x % y)
for i in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	copy_list = l[:]
	l.sort()
	flag = 0
	for j in range(n):
		if l[j] != copy_list[j]:
			if gcd(l[0], copy_list[j]) != l[0]:
				flag += 1
	if flag > 0:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = input()
	a = list(map(lambda x: int(x), input().split()))
	b = sorted(a)
	numbers = []
	minimum = b[0]
	for i in range(0, len(a)):
		if a[i] != b[i]:
			numbers.append(a[i])
	flag = 0
	count1 = 0
	countgt1 = 0
	if minimum == 1:
		print('YES')
	else:
		for number in numbers:
			if number % minimum != 0:
				flag = 1
				break
		if flag == 1:
			print('NO')
		else:
			print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = a.copy()
	a.sort()
	flag = 0
	for i in range(n):
		if b[i] % a[0] != 0 and b[i] != a[i]:
			print('NO')
			flag = 1
			break
	if flag == 0:
		print('YES')


from math import gcd
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	l = sorted(a)
	m = min(a)
	l1 = [0] * n
	for i in range(n):
		if a[i] % m == 0:
			l1[i] = 1
	flag = False
	for i in range(n):
		if l1[i] == 0 and a[i] != l[i]:
			flag = True
	if flag:
		print('NO')
	else:
		print('YES')


import math
t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().strip().split(' ')))
	k = []
	k[:] = lst[:]
	k.sort()
	l = []
	m = k[0]
	for j in range(n):
		if lst[j] % m == 0:
			l.append(k[j])
		else:
			l.append(lst[j])
	if l == k:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
from collections import Counter
from collections import defaultdict
from math import ceil
from bisect import bisect_left
from bisect import bisect_right
import math
ai = lambda : list(map(int, stdin.readline().split()))
ei = lambda : map(int, stdin.readline().split())
ip = lambda : int(stdin.readline().strip())
for i in range(ip()):
	n = ip()
	li = ai()
	if li == sorted(li):
		print('YES')
		continue
	dummy = sorted(li)
	MIN = min(li)
	c = 0
	for i in range(n):
		if li[i] != dummy[i]:
			if li[i] % MIN != 0:
				c += 1
				break
	if c > 0:
		print('NO')
	else:
		print('YES')


import os
import sys
from io import BytesIO, IOBase
from collections import Counter
from math import gcd

def main():
	for _ in range(Iint()):
		Iint()
		array = Ilist()
		minVal = min(array)
		newArr = []
		keyArray = []
		valArray = []
		for (i, v) in enumerate(array):
			if v % minVal == 0:
				newArr.append([i, v])
				keyArray.append(i)
				valArray.append(v)
		valArray.sort()
		for (i, v) in enumerate(keyArray):
			array[v] = valArray[i]
		if array == sorted(array):
			print('YES')
		else:
			print('NO')

def I():
	return input()

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def check(arr, arr1):
	b = min(arr)
	if len(arr) == 1:
		return 'YES'
	else:
		for i in range(len(arr)):
			if arr[i] != arr1[i] and arr[i] % b != 0:
				return 'NO'
	return 'YES'
import math
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr1 = sorted(arr)
	print(check(arr, arr1))


from sys import stdin, stdout
for query in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	a2 = sorted(a)
	b = min(a)
	mybool = True
	for x in range(n):
		if (a2[x] % b != 0 or a[x] % b != 0) and a2[x] != a[x]:
			mybool = False
	if mybool:
		stdout.write('YES\n')
	else:
		stdout.write('NO\n')


a = input()
for x in range(int(a)):
	c = input()
	d = input().split(' ')
	list1 = [int(i) for i in d]
	list2 = list1.copy()
	list2.sort()
	tag = []
	buffer = min(list1)
	for i in range(len(list1)):
		if list1[i] % buffer != 0:
			tag.append(i)
	if list1 == list2:
		print('YES')
	else:
		for i in tag:
			if list1[i] != list2[i]:
				print('NO')
				break
		else:
			print('YES')


import os
import sys
from io import BytesIO, IOBase
import threading
from collections import defaultdict, Counter
from math import gcd

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	brr = arr.copy()
	brr.sort()
	t = min(arr)
	ok = True
	for i in range(n):
		if arr[i] % t != 0 and brr[i] != arr[i]:
			ok = False
			break
	if ok:
		print('YES')
	else:
		print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(int(input())):
	main()


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	mn = b[0]
	ans = True
	for i in range(n):
		if a[i] != b[i] and a[i] % b[0] != 0:
			ans = False
			break
	if ans:
		print('YES')
	else:
		print('NO')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = LI()
		m = min(a)
		b = sorted(a)
		ok = 1
		for i in range(n):
			if a[i] != b[i] and a[i] % m != 0:
				ok = 0
				break
		rr.append(IF(ok, 'YES', 'NO'))
	return JA(rr, '\n')
print(main())


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = sorted(a)
	v = 0
	for i in range(n):
		if a[i] % c[0] != 0 and a[i] != c[i]:
			v = 1
	if v == 1:
		print('NO')
	else:
		print('YES')


def x():
	N = int(input())
	for i in range(N):
		A = int(input())
		ans = list(map(int, input().split()))
		comp = sorted(ans)
		p = 0
		for j in range(A):
			if ans[j] != comp[j] and ans[j] % comp[0] != 0:
				p = 1
				break
		if p == 1:
			print('NO')
		else:
			print('YES')
x()


def ans(a, n):
	if n == 1:
		return 'YES'
	x = min(a)
	b = sorted(a)
	for i in range(len(a)):
		if a[i] != b[i]:
			if a[i] % x != 0:
				return 'NO'
	return 'YES'
m = int(input())
for i in range(m):
	n = int(input())
	arr = input().split()
	a = []
	for i in arr:
		a.append(int(i))
	print(ans(a, n))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sarr = sorted(arr)
	m = min(arr)
	mi = arr.index(m)
	k = 0
	for i in range(n):
		if arr[i] != sarr[i] and arr[i] % m != 0:
			k = 1
			break
	if k:
		print('NO')
	else:
		print('YES')


import sys
import bisect
input = sys.stdin.readline
t = int(input())

def takeSecond(elem):
	return elem[1]

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	for i in range(n):
		l1.append(l[i])
	l1.sort()
	l2 = []
	ch = 1
	mini = min(l[:])
	for i in range(n):
		if l[i] != l1[i]:
			if l[i] % mini == 0:
				ch = 1
			else:
				ch = 0
				break
	if ch == 1:
		print('YES')
	else:
		print('NO')


try:
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		x = sorted(a)
		flag = 0
		for i in range(len(x)):
			if x[i] != a[i] and x[i] % x[0] != 0:
				flag = 1
				break
		if flag == 0:
			print('YES')
		else:
			print('NO')
except:
	pass


import math
from sys import *
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(sorted(a))
	mn = min(a)
	ch = 1
	for i in range(n):
		g = math.gcd(a[i], mn)
		if g == mn:
			continue
		if a[i] == b[i]:
			continue
		ch = 0
		break
	if ch == 1:
		stdout.write('YES\n')
	else:
		stdout.write('NO\n')


t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	j = 0
	m = min(b)
	v = [0] * n
	k = []
	while j < n:
		k.append(b[j])
		j += 1
	k.sort()
	for j in range(n):
		if k[j] % m == 0:
			v[j] = 1
	c = [0]
	j = 0
	flag = 0
	while j < n:
		if b[j] % m != 0:
			if b[j] >= c[-1]:
				c.append(b[j])
			else:
				flag = 1
				break
		elif v[j] == 0:
			flag = 1
			break
		j += 1
	if flag == 1:
		print('NO')
	else:
		print('YES')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	f = 0
	m = min(a)
	b = sorted(a)
	if a == b:
		print('YES')
	else:
		for i in range(len(a) - 1, -1, -1):
			if b[i] != a[i]:
				if b[i] % m != 0 or a[i] % m != 0:
					f = 1
		if f == 0:
			print('YES')
		else:
			print('NO')


import sys
from sys import stdin, stdout
from math import ceil, floor, sqrt, log, log2, log10
from collections import Counter
int_max = float('inf')
int_min = -float('inf')
ANS = []
input = stdin.readline

def solve():
	pass
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	m = min(arr)
	temp = arr[:]
	temp.sort()
	f = 1
	for i in range(n):
		if arr[i] % m != 0:
			if arr[i] != temp[i]:
				f = 0
				break
	ANS.append('YES' if f else 'NO')
print('\n'.join(ANS))


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	lisa = sorted(lis)
	lise = [0]
	if lisa == lis:
		print('YES')
	else:
		x = min(lis)
		for i in range(n):
			if lisa[i] != lis[i]:
				lise.append(lis[i] % x)
		if any(lise):
			print('NO')
		else:
			print('YES')


from queue import Queue

def rn():
	a = int(input())
	return a

def rl():
	a = list(map(int, input().split()))
	return a
for _ in range(rn()):
	n = rn()
	a = rl()
	ans = 'YES'
	b = a.copy()
	b.sort()
	mm = b[0]
	d = {}
	for i in range(n):
		if b[i] in d:
			d[b[i]][1] += 1
		else:
			d[b[i]] = [i, i]
	for i in range(n):
		if i < d[a[i]][0] or i > d[a[i]][1]:
			if a[i] % mm != 0:
				ans = 'NO'
				break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	new = sorted(a)
	mn = new[0]
	f = False
	for i in range(n):
		if new[i] == a[i] or (new[i] % mn == 0 and a[i] % mn == 0):
			pass
		else:
			f = True
			break
	if f:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ma = min(a)
	mod = {}
	for i in range(n):
		bb = 0 if a[i] % ma == 0 else 1
		if bb not in mod:
			mod[bb] = [i]
		else:
			mod[bb] += [i]
	for i in range(1):
		indices = mod[0]
		values = sorted([a[k] for k in indices])
		c = 0
		for i in indices:
			a[i] = values[c]
			c += 1
	print('YES' if a == sorted(a) else 'NO')


import sys
input = sys.stdin.readline
t = int(input())

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
for t1 in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	x = sorted(l)
	pos = {}
	for i in range(n):
		pos[l[i]] = []
	for i in range(n):
		pos[l[i]].append(i)
	f = 0
	z = min(l)
	for i in range(n):
		if x[i] != l[i]:
			if gcd(x[i], l[i]) % z != 0:
				f = 1
				break
			else:
				al = pos[x[i]][-1]
				pos[x[i]].pop()
				(l[i], l[al]) = (l[al], l[i])
	if f == 0:
		print('YES')
	else:
		print('NO')


import sys
from math import gcd
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print('YES')
	else:
		x = min(a)
		tmp = []
		for j in range(len(a)):
			if a[j] % x != 0:
				tmp.append((a[j], j))
		a.sort()
		ok = 1
		for el in tmp:
			if el[0] != a[el[1]]:
				ok = 0
		if ok == 1:
			print('YES')
		else:
			print('NO')


t = int(input())
for case in range(0, t):
	n = int(input())
	a = [int(x) for x in input().split()]
	m = min(a)
	flag = False
	pos = []
	arr = []
	for i in range(0, n):
		if a[i] % m == 0:
			pos.append(i)
		else:
			arr.append(a[i])
	a.sort()
	for i in range(0, n):
		if a[i] % m == 0 and i not in pos:
			flag = True
			break
	arr1 = arr[:]
	arr1.sort()
	if arr != arr1:
		flag = True
	if flag:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	x = int(input())
	s = list(map(int, input().split()))
	mn = min(s)
	v = []
	id = []
	for n in range(x):
		if s[n] % mn == 0:
			v.append(s[n])
			id.append(n)
	v.sort()
	id.sort()
	for n in range(len(v)):
		s[id[n]] = v[n]
	if s == sorted(s):
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ref = sorted(a)
	if a == ref:
		print('YES')
	else:
		mini = min(a)
		yep = False
		for i in range(n):
			if a[i] % mini != 0:
				if a[i] != ref[i]:
					yep = True
					break
		if not yep:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	(*arr,) = map(int, input().split())
	sorted_arr = sorted(arr)
	mn = sorted_arr[0]
	for i in range(n):
		if arr[i] != sorted_arr[i] and arr[i] % mn > 0:
			print('NO')
			break
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr2 = sorted(arr)
	minn = arr2[0]
	ok = True
	for i in range(n):
		if arr2[i] != arr[i] and arr2[i] % minn != 0:
			print('NO')
			ok = False
			break
	if ok:
		print('YES')


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	a = array()
	As = sorted(a)
	disturbed = []
	for i in range(n):
		if a[i] != As[i]:
			disturbed.append(a[i])
	key = min(a)
	ok = 'YES'
	for i in disturbed:
		if i % key:
			ok = 'NO'
	print(ok)


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	fa = a.copy()
	fa.sort()
	mi = min(a)
	d = []
	for i in range(n):
		if a[i] != fa[i]:
			d.append(fa[i])
	if len(d) == 0:
		print('yes')
	else:
		k = 1
		for i in d:
			if math.gcd(i, mi) != mi:
				k = 0
				break
		if k == 0:
			print('no')
		else:
			print('yes')


tc = int(input())
Y = 'YES'
N = 'NO'

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def possible(arr):
	if len(arr) == 1:
		return Y
	mi = min(arr)
	s = []
	n = len(arr)
	for i in range(n):
		if gcd(arr[i], mi) == mi:
			s.append(arr[i])
			arr[i] = -1
	s.sort()
	j = 0
	for i in range(n):
		if arr[i] == -1:
			arr[i] = s[j]
			j += 1
	return all((arr[i] >= arr[i - 1] for i in range(1, n)))
for i in range(tc):
	n = input()
	arr = input()
	arr = arr.split()
	arr = list(map(int, arr))
	print(Y if possible(arr) else N)


for _ in range(int(input())):
	n = int(input())
	s = map(int, input().split())
	s = list(s)
	so = sorted(s)
	MIN = so[0]
	ans = 'YES'
	for (i, x) in enumerate(s):
		if x != so[i]:
			if x % MIN != 0:
				ans = 'NO'
	print(ans)


def gcd(a, b):
	if b > a:
		return gcd(b, a)
	if b == 0:
		return a
	return gcd(b, a % b)

def solve(arr, n, ans):
	x = min(arr)
	good = set()
	g = 0
	for i in range(n):
		if arr[i] % x == 0:
			g = gcd(g, arr[i] // x)
			good.add(i)
	if g > 1:
		good = set()
	arr1 = arr[:]
	arr1.sort()
	for i in range(n):
		if arr1[i] != arr[i]:
			if i not in good:
				ans.append('NO')
				return
	ans.append('YES')

def main():
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		solve(arr, n, ans)
	print('\n'.join(ans))
main()


from math import gcd
t = int(input())
for i in range(t):
	n = int(input())
	ok = 1
	A = list(map(int, input().split()))
	m = min(A)
	B = sorted(A)
	C = []
	for i in range(n):
		if A[i] != B[i]:
			C.append(A[i])
	for i in range(len(C)):
		if gcd(C[i], m) != m:
			print('NO')
			ok = 0
			break
	if ok == 1:
		print('YES')


def can_sort_with_gcd(sequence):
	sequence = tuple(sequence)
	minimum = min(sequence)
	sorted_sequence = sorted(sequence)
	return all((input_x == sorted_x for (input_x, sorted_x) in zip(sequence, sorted_sequence) if input_x % minimum != 0))

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a_seq = tuple(map(int, input().split()))
		output = 'YES' if can_sort_with_gcd(a_seq) else 'NO'
		print(output)
main()


import sys
import math

def input():
	return sys.stdin.readline().rstrip('\r\n')

def List():
	return list(map(int, input().split()))

def Num():
	return int(input())
for _ in range(Num()):
	n = Num()
	a = List()
	if n == 1:
		print('YES')
	elif a == sorted(a):
		print('YES')
	else:
		x = sorted(a)[:]
		non_sorted = []
		for i in range(n):
			if a[i] != x[i]:
				non_sorted.append(a[i])
		ans = 'YES'
		minn = min(a)
		for i in range(len(non_sorted)):
			if non_sorted[i] % minn == 0:
				continue
			else:
				ans = 'NO'
		print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l1 = list(map(int, input().split()))
	x = min(l1)
	l2 = []
	for i in l1:
		l2.append(i)
	l2.sort()
	f = 0
	for i in range(n):
		if l1[i] != l2[i]:
			if l1[i] % x != 0:
				f = 1
				break
	if f == 0:
		print('YES')
	else:
		print('NO')


for _ in ' ' * int(input()):
	n = int(input())
	a = list(map(int, input().split()))
	x = list(sorted(a))
	if x == a:
		print('YES')
	else:
		mn = min(a)
		lst = []
		for i in range(n):
			if a[i] % mn == 0:
				lst.append(a[i])
		lst.sort()
		cnt = 0
		for i in range(n):
			if a[i] % mn == 0:
				a[i] = lst[cnt]
				cnt += 1
		x = list(sorted(a))
		if x == a:
			print('YES')
		else:
			print('NO')


import sys
input = sys.stdin.readline
for nt in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	flag = 0
	for i in range(n):
		if a[i] % b[0] != 0:
			if a[i] != b[i]:
				flag = 1
				break
	if flag:
		print('NO')
		continue
	print('YES')


import sys
from collections import deque
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = sorted(a)
	m = min(a)
	b = [a[i] for i in range(n) if not a[i] % m]
	b.sort()
	b = deque(b)
	for i in range(n):
		if a[i] % m == 0:
			a[i] = b.popleft()
	print('YES' if a == c else 'NO')


import sys, math
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	b = [a[i] for i in range(n)]
	b.sort()
	out = 'YES'
	for i in range(n):
		if a[i] != b[i] and a[i] % b[0] != 0:
			out = 'NO'
	print(out)


from sys import stdin, gettrace
from math import gcd
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	n = int(inputi())
	aa = [int(a) for a in inputi().split()]
	nm = min(aa)
	order = sorted(aa)
	for (a, b) in zip(aa, order):
		if a != b and a % nm != 0:
			print('NO')
			return
	print('YES')

def main():
	t = int(inputi())
	for _ in range(t):
		solve()
main()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(num) for num in input().split()]
	if n == 1:
		print('YES')
		continue
	diff_elements = []
	minimum = min(arr)
	sorted_arr = sorted(arr)
	possible_to_sort = True
	for i in range(len(arr)):
		if sorted_arr[i] != arr[i]:
			if arr[i] % minimum != 0:
				possible_to_sort = False
				break
	print('YES' if possible_to_sort else 'NO')


import sys, io, os, time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
for ii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mm = min(a)
	f = 1
	check = sorted(a)
	for i in range(n):
		if check[i] != a[i] and math.gcd(a[i], mm) != mm:
			f = 0
			break
	if f:
		print('YES')
	else:
		print('NO')


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def readTree():
	v = int(input())
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return (adj, v)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return
mod = 10 ** 9 + 7

def solve():
	n = II()
	a = L()
	if n == 1:
		P('YES')
		return
	else:
		mn = min(a)
		cpy = list(a)
		cpy.sort()
		for i in range(n):
			if cpy[i] != a[i] and a[i] % mn != 0:
				P('NO')
				return
		P('YES')
		return
t = II()
for i in range(t):
	solve()


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = mpos(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum
from math import *
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	b.sort()
	c = [0] * n
	for i in range(n):
		if a[i] != b[i]:
			c[i] = 1
	mm = b[0]
	f = 1
	for i in range(n):
		if c[i] == 1:
			if gcd(mm, a[i]) != mm:
				f = 0
				break
	if f:
		print('YES')
	else:
		print('NO')


import math
for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	li2 = li[:]
	li2.sort()
	if li2 == li:
		print('YES')
	else:
		ele = min(li)
		elements = []
		for i in range(len(li)):
			if li[i] != li2[i]:
				elements.append(li[i])
		st = ele
		f = 0
		cc = ele
		for i in range(len(elements)):
			st = math.gcd(st, elements[i])
			if st != ele:
				f = 1
				break
		if f == 1:
			print('NO')
		else:
			print('YES')


import math

def main(arr):
	s = sorted(arr)
	m = s[0]
	g = 0
	for i in range(len(s)):
		if s[i] != arr[i]:
			g = math.gcd(g, arr[i])
	if math.gcd(g, m) == m:
		return 'YES'
	return 'NO'
t = int(input())
ans = []
for i in range(t):
	n = int(input())
	ans.append(main(list(map(int, input().split()))))
for e in ans:
	print(e)


typeofresult = True

def solve(q):
	ans = 'YES'
	(n, s) = q
	il = s.copy()
	s.sort()
	m = min(il)
	for i in range(n):
		if s[i] != il[i]:
			if il[i] % m != 0:
				ans = 'NO'
	return ans

def cin():
	n = int(input())
	il = input().split()
	for _ in range(n):
		il[_] = int(il[_])
	ival = [n, il]
	return ival

def main(typeofresult):
	if typeofresult == True:
		i = int(input())
	else:
		i = 1
	for tc in range(i):
		inp = cin()
		ans = solve(inp)
		print(ans)
main(typeofresult)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr1 = arr.copy()
	arr1.sort()
	mini = min(arr)
	for i in range(n):
		if arr[i] != arr1[i]:
			if arr[i] % mini != 0:
				print('NO')
				break
	else:
		print('YES')


import math
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	u = 0
	g = 0
	min = b[0]
	ans = 0
	for i in range(n):
		if a[i] % min == 0:
			ans = 0
		elif a[i] == b[i]:
			ans = 0
		else:
			ans = 1
			break
	if ans == 0:
		print('YES')
	else:
		print('NO')


import sys
import copy
inputlines = sys.stdin.readlines()
number_of_testcases = int(inputlines[0])
for i in range(number_of_testcases):
	number_of_elements = int(inputlines[2 * i + 1])
	array_elements = list(map(int, inputlines[2 * i + 2].split(' ')))
	minimum = 1000000001
	for i in array_elements:
		if i < minimum:
			minimum = i
	array_elements_copy = copy.deepcopy(array_elements)
	array_elements.sort()
	for i in range(number_of_elements):
		if array_elements[i] != array_elements_copy[i] and array_elements_copy[i] % minimum != 0:
			print('NO')
			break
	else:
		print('YES')


t = input()
t = int(t)
for k in range(0, t):
	n = input()
	n = int(n)
	a = [int(x) for x in input().split()]
	a_sort = sorted(a)
	ans = 'YES'
	min = a_sort[0]
	min = int(min)
	for i in range(n):
		if a[i] != a_sort[i] and a[i] % min != 0:
			ans = 'NO'
			break
	print(ans)


import math
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a_sorted = sorted(a[:])
	m = a_sorted[0]
	answer = 'YES'
	if n == 1:
		print(answer)
		continue
	for j in range(n):
		if a[j] != a_sorted[j] and a[j] % m != 0:
			answer = 'NO'
			break
	print(answer)


t = int(input())

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for i in range(t):
	k = 0
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	m = min(a)
	for i in range(n):
		if a[i] != b[i] and a[i] % m > 0:
			k = 1
	if k:
		print('NO')
	else:
		print('YES')


def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def g(l):
	num1 = l[0]
	num2 = l[1]
	gcd = find_gcd(num1, num2)
	for i in range(2, len(l)):
		gcd = find_gcd(gcd, l[i])
	return gcd

def ga(l, ma):
	for i in l:
		if i % ma != 0:
			return False
	return True
for tt in range(int(input())):
	nope = False
	n = int(input())
	a = list(map(int, input().split()))
	b = list(a)
	b.sort()
	ma = min(a)
	c = []
	for j in range(n):
		if b[j] != a[j]:
			c.append(b[j])
	if len(c) == 0:
		print('YES')
		continue
	ans = ga(c, ma)
	if ans:
		print('YES')
	else:
		print('NO')


import sys

def function(arr, n):
	b = sorted(arr)
	if arr == b:
		return 'YES'
	else:
		x = min(arr)
		flag = True
		for i in range(n):
			if arr[i] != b[i] and arr[i] % x:
				flag = False
				break
		return 'YES' if flag else 'NO'
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, sys.stdin.readline().strip().split()))
	result = function(arr, n)
	print(result)


t = int(input())
while t != 0:
	t = t - 1
	n = int(input())
	arr = list(map(int, input().strip().split()))[:n]
	m = min(arr)
	pos = []
	b = []
	for i in range(0, n):
		if arr[i] % m == 0:
			b.append(arr[i])
			pos.append(i)
	b.sort()
	for i in range(0, len(pos)):
		p = pos[i]
		arr[p] = b[i]
	flag = 1
	for i in range(0, n - 1):
		if arr[i] > arr[i + 1]:
			flag = 0
			break
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	input()
	array = list(map(int, input().split()))
	sort = sorted(array)
	m = sort[0]
	for i in range(len(array)):
		if array[i] != sort[i] and array[i] % m:
			print('NO')
			break
	else:
		print('YES')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	b.sort()
	d = b[0]
	flag = 0
	for i in range(n):
		if a[i] != b[i]:
			if a[i] % d != 0:
				flag = -1
				break
	if flag == -1:
		print('NO')
	else:
		print('YES')


import math

def gcd(a, b):
	if a < b:
		(a, b) = (b, a)
	while a % b != 0:
		(a, b) = (b, a % b)
	return b
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		a[i] = [a[i], i]
	a.sort()
	for i in range(n):
		if i != a[i][1]:
			if a[i][0] % a[0][0] == 0:
				pass
			else:
				print('NO')
				break
	else:
		print('YES')


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	b.sort()
	f1 = []
	for i in range(n):
		if a[i] != b[i]:
			f1.append(0)
		else:
			f1.append(1)
	m = min(a)
	f = 0
	for i in range(n):
		if f1[i] == 0:
			if a[i] % m != 0:
				f = 1
	if f == 1:
		print('NO')
	else:
		print('YES')
	t -= 1


import sys
t = int(input().rstrip())
for _ in range(t):
	length = int(input().rstrip())
	arr = list(map(int, input().rstrip().split()))
	m = min(arr)
	sorted_arr = arr[:]
	sorted_arr.sort()
	k = False
	for i in range(length):
		if arr[i] != sorted_arr[i] and arr[i] % m > 0:
			k = True
			break
	if k:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	m = min(arr)
	nArr = arr[:]
	nArr.sort()
	need = []
	for i in range(n):
		if arr[i] != nArr[i]:
			need.append(arr[i])
	fl = True
	for i in range(len(need)):
		if need[i] % m != 0:
			fl = False
			break
	if fl:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = a[:]
	first = True
	for i in range(n):
		if first:
			min_a = a[i]
			first = False
		else:
			min_a = min(min_a, a[i])
	a.sort()
	possible = True
	for i in range(n):
		if a[i] != b[i] and a[i] % min_a != 0:
			possible = False
			break
	if possible:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	n = int(input())
	arr = [int(k) for k in input().split()]
	mn = min(arr)
	so = sorted(arr)
	flag = 0
	for i in range(n):
		if so[i] != arr[i]:
			if arr[i] % mn != 0:
				flag = 1
				break
	if flag == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	a = l.copy()
	a.sort()
	come = 0
	for i in range(n):
		if a[i] != l[i]:
			if l[i] % a[0] == 0:
				continue
			else:
				print('NO')
				come = 1
				break
		else:
			continue
	if come == 0:
		print('YES')


for i in range(int(input())):
	input()
	a = input().split(' ')
	for j in range(len(a)):
		a[j] = int(a[j])
	b = min(a)
	c = []
	d = []
	for j in range(len(a)):
		if a[j] % b == 0:
			c.append(a[j])
			d.append(j)
	c = sorted(c)
	d = enumerate(d)
	for (j, k) in d:
		a[k] = c[j]
	d = 1
	for j in range(len(a) - 1):
		d = a[j] <= a[j + 1] and d
	if d == 1:
		print('YES')
	else:
		print('NO')


def tInput():
	from sys import stdin
	input = stdin.readline
tInput()

def A():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		if n >= k:
			print(int(n % 2 != k % 2))
		else:
			print(k - n)

def B():
	for _ in range(int(input())):
		(x1, y1, z1) = map(int, input().split())
		(x2, y2, z2) = map(int, input().split())
		tot = 0
		p = min(x1, z2)
		x1 -= p
		z2 -= p
		tot += 2 * min(z1, y2)
		p = min(z1, y2)
		z1 -= p
		y2 -= p
		if z2 > 0:
			z2 -= min(z2, z1)
			tot -= 2 * min(y1, z2)
		print(tot)

def C():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		sa = sorted(a)
		m = min(a)
		w = True
		for i in range(n):
			if a[i] % m != 0:
				if a[i] != sa[i]:
					w = False
					break
		if w:
			print('YES')
		else:
			print('NO')
C()


from typing import *

def can_sort(a: List[int]) -> bool:
	a_min = min(a)
	pivots = []
	prev_el = None
	positions = []
	for (i, a_i) in enumerate(a):
		if a_i % a_min == 0:
			pivots.append(a_i)
			positions.append(i)
			prev_el = None
		elif prev_el is not None and a_i < prev_el:
			return False
		else:
			prev_el = a_i
	pivots.sort(reverse=True)
	for pos in positions:
		a[pos] = pivots.pop()
		if pos > 0:
			if a[pos] < a[pos - 1]:
				return False
		if pos < len(a) - 1:
			if a[pos + 1] % a_min != 0 and a[pos + 1] < a[pos]:
				return False
	return True
t = int(input())
for _ in range(t):
	input()
	if can_sort(list(map(int, input().split(' ')))):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	b = sorted(a)
	ans = 'YES'
	for i in range(n):
		if a[i] != b[i] and a[i] % b[0] != 0:
			ans = 'NO'
			break
	print(ans)


import math
t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	lst1 = lst.copy()
	lst1.sort()
	mi = min(lst)
	er = 0
	for j in range(n):
		if lst[j] != lst1[j] and lst[j] % mi != 0:
			er += 1
			break
	if er == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	f = min(a)
	g = sorted(a)
	ans = 'YES'
	for i in range(0, len(a)):
		if a[i] != g[i]:
			if a[i] % f != 0:
				ans = 'NO'
				break
	print(ans)


import copy
r = int(input())
for l in range(r):
	n = int(input())
	arr = [int(d) for d in input().split()]
	k = min(arr)
	b = 0
	arr1 = copy.copy(arr)
	arr1.sort()
	for i in range(n):
		if arr1[i] != arr[i] and arr[i] % k > 0:
			b = 1
	if b:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline
from math import gcd
for _ in range(int(input())):
	n = int(input())
	L = list(map(int, input().split()))
	M = sorted(L)
	if M[0] == 1:
		print('YES')
		continue
	if L == M:
		print('YES')
		continue
	MIN = M[0]
	for i in range(n):
		if L[i] != M[i]:
			if L[i] % MIN != 0:
				print('NO')
				break
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = a.copy()
	a.sort()
	mi = a[0]
	ans = True
	for i in range(n):
		if a[i] != b[i]:
			if b[i] % mi == 0:
				continue
			else:
				ans = False
				break
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	n = int(input())
	a = [int(a) for a in input().split()]
	b = sorted(a)
	m = b[0]
	ans = 'YES'
	for i in range(0, n):
		if a[i] != b[i]:
			if a[i] % m != 0:
				ans = 'NO'
				break
	print(ans)
	t -= 1


def resolve():
	loop_n = int(input())
	for _ in range(loop_n):
		N = int(input())
		A = [int(x) for x in input().split(' ')]
		if N == 1:
			print('YES')
			continue
		minA = min(A)
		sortedA = sorted(A)
		unmovable = [x % minA != 0 for x in A]
		breaked = False
		for (i, a) in enumerate(A):
			if a != sortedA[i] and unmovable[i]:
				print('NO')
				breaked = True
				break
		if not breaked:
			print('YES')
resolve()


t = int(input())
for i in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	br = sorted(ar)
	mn = br[0]
	r = 0
	for i in range(n):
		if ar[i] != br[i] and ar[i] % mn > 0:
			r = 1
			break
	if r:
		print('NO')
	else:
		print('YES')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	m = b[0]
	ans = True
	for (x, y) in zip(a, b):
		if x != y:
			if y % m:
				print('NO')
				ans = False
				break
	if ans:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	B = sorted(A)
	m = B[0]
	can_be = 1
	for i in range(n):
		if B[i] != A[i] and (B[i] % m != 0 or A[i] % m != 0):
			can_be = 0
			break
	if can_be == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = sorted(a)
	m = min(a)
	for (x, y) in zip(a, s):
		if x % m != 0 and x != y:
			print('NO')
			break
	else:
		print('YES')


for i1 in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	if a == b:
		print('YES')
		continue
	gcd = min(a)
	index = []
	for i in range(n):
		if a[i] % gcd != 0:
			index.append(i)
	flag = 0
	for i in range(len(index)):
		t = index[i]
		if a[t] != b[t]:
			flag = 1
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


import os
import sys
from io import BytesIO, IOBase
from collections import Counter
from math import gcd

def main():
	for _ in range(Iint()):
		Iint()
		array = Ilist()
		minVal = min(array)
		newArr = []
		keyArray = []
		valArray = []
		for (i, v) in enumerate(array):
			if v % minVal == 0:
				newArr.append([i, v])
				keyArray.append(i)
				valArray.append(v)
		valArray.sort()
		for (i, v) in enumerate(keyArray):
			array[v] = valArray[i]
		if array == sorted(array):
			print('YES')
		else:
			print('NO')

def I():
	return input()

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))
main()


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def solve() -> None:
	n = int(input())
	l = list(map(int, input().split()))
	m = min(l)
	copy = list(sorted(l))
	if copy == l:
		print('YES')
		return
	for i in range(n):
		if l[i] != copy[i]:
			if l[i] % m > 0:
				print('NO')
				return
	print('YES')
for _ in range(int(input())):
	solve()


from sys import stdin, stdout
import bisect
import math
from collections import deque, Counter
mod = 10 ** 9 + 7

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def DFS(dictionary, vertex, visited):
	visited[vertex] = True
	stack = [vertex]
	print(vertex)
	while stack:
		a = stack.pop()
		for i in dictionary[a]:
			if not visited[i]:
				print(i)
				visited[i] = True
				stack.append(i)

def BFS(dictionary, vertex, visited):
	visited[vertex] = True
	q = deque()
	q.append(vertex)
	while q:
		a = q.popleft()
		for i in dictionary[a]:
			if not visited[i]:
				visited[i] = True
				q.append(i)
				print(i)

def soe(limit):
	l = [1] * (limit + 1)
	l[0] = 0
	l[1] = 0
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def gcd(a, b):
	while b:
		a = a % b
		(b, a) = (a, b)
	return a

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
		a *= a
		n = n >> 1
	return r

def solve():
	n = inp()
	l = li()
	x = sorted(l)
	a = x[0]
	for i in range(n):
		if l[i] != x[i] and l[i] % a != 0:
			pr('NO')
			return
	pr('YES')
for _ in range(inp()):
	solve()


def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def solve(n, ar):
	ind = []
	for i in range(n):
		ind.append(i)
	comp = ar.copy()
	zipped_lists = zip(comp, ind)
	sorted_pairs = sorted(zipped_lists)
	tuples = zip(*sorted_pairs)
	(comp, ind) = [list(tuple) for tuple in tuples]
	mini = comp[0]
	for i in range(n):
		if ar[i] == comp[i]:
			continue
		if ar[i] % mini != 0:
			if ar[i] == comp[i]:
				continue
			else:
				print('NO')
				return
	print('YES')
t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	solve(n, ar)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	m = min(arr)
	l = []
	x = sorted(enumerate(arr), key=lambda x: x[1])
	for i in range(n):
		if x[i][0] != i:
			l.append(x[i][1])
	for i in l:
		if i % m != 0:
			print('NO')
			break
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	a = sorted(l)
	m = min(l)
	ans = True
	for i in range(len(l)):
		if l[i] != a[i] and l[i] % m != 0:
			ans = False
			break
	if ans:
		print('YES')
	else:
		print('NO')


for test in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	m = min(a)
	check = True
	for i in range(n):
		if a[i] != b[i] and a[i] % m != 0:
			check = False
			break
	if check:
		print('YES')
	else:
		print('NO')


import sys

def input():
	return sys.stdin.readline().rstrip()

def input_split():
	return [int(i) for i in input().split()]
testCases = int(input())
answers = []
for _ in range(testCases):
	n = int(input())
	arr = input_split()
	mini = min(arr)
	barr = arr.copy()
	barr.sort()
	ans = True
	for i in range(n):
		if arr[i] == barr[i]:
			continue
		elif barr[i] % mini == 0:
			continue
		else:
			ans = False
			break
	mapp = {False: 'NO', True: 'YES'}
	answers.append(mapp[ans])
print(*answers, sep='\n')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	new = sorted(a)
	mn = min(a)
	idx = []
	for i in range(n):
		if a[i] % mn == 0:
			idx.append([a[i], i])
	idx.sort(key=lambda x: x[0])
	arr = []
	for i in range(len(idx)):
		arr.append(idx[i][1])
	arr.sort()
	for i in range(len(arr)):
		a[arr[i]] = idx[i][0]
	if a == new:
		print('YES')
	else:
		print('NO')


from copy import deepcopy
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print('YES')
	else:
		t = deepcopy(a)
		t.sort()
		flag = 0
		mini = min(a)
		for i in range(n):
			if t[i] != a[i]:
				if t[i] % mini == 0:
					continue
				else:
					flag = 1
					break
		if flag == 0:
			print('YES')
		else:
			print('NO')


t = int(input())
i = 0
while i < t:
	n = int(input())
	mass = list(map(int, input().split()))
	mass2 = sorted(mass)
	minimum = mass2[0]
	numbers = []
	index = 0
	while index < n:
		if mass[index] != mass2[index]:
			numbers.append(mass[index])
		index += 1
	valid = 1
	for j in numbers:
		if j % minimum != 0:
			valid = 0
	if valid == 1:
		print('YES')
	else:
		print('NO')
	i += 1


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import Counter
import heapq
from collections import Counter
import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ls = sorted(arr)
	var = ls[0]
	f = 0
	for i in range(n):
		if arr[i] != ls[i]:
			if math.gcd(arr[i], var) != var:
				f = 1
				break
	if f == 1:
		print('NO')
	else:
		print('YES')