#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>

/* Author Information
 * Author: JokerNoCry
 * Time: 2020-08-21 22:35:02
 * QQ:1160860141
**/

#define max(a,b) ((a>b)?(a):(b))
#define min(a,b) ((a<b)?(a):(b))
#define ll long long

using namespace std;

int t,n,minnum,a[100010];
int gcd(int x, int y)
{
    while(y^=x^=y^=x%=y);
    return x;
}

void selection_sort(int arr[], int len)
{
    int i,j;

    for (i = 0 ; i < len - 1 ; i++)
    {
        int min = i;
        for (j = i + 1; j < len; j++)
            if ((arr[j] < arr[min])  && (gcd(arr[j],arr[min]) % minnum == 0 ))
                min = j;
        if(min != i)
        {
            arr[min] = arr[min] ^ arr[i];
            arr[i] = arr[min] ^ arr[i];
            arr[min] = arr[min] ^ arr[i];
        }

    }
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        minnum = 0x7fffffff;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            minnum = min(minnum,a[i]);
        }
        selection_sort(a,n);
        bool flag = true;
        for(int i=1;i<n;i++)
        {
            if(a[i]<a[i-1])
            {
                flag = false;
                break;
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>

/* Author Information
 * Author: JokerNoCry
 * Time: 2020-08-21 22:35:02
 * QQ:1160860141
**/

#define max(a,b) ((a>b)?(a):(b))
#define min(a,b) ((a<b)?(a):(b))
#define ll long long

using namespace std;

int t,n,minnum,a[100010];
int gcd(int x, int y)
{
    while(y^=x^=y^=x%=y);
    return x;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        minnum = 0x7fffffff;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            minnum = min(minnum,a[i]);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if((gcd(a[i],a[j]) == minnum) && a[i]>a[j])
                {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        bool flag = true;
        for(int i=1;i<n;i++)
        {
            if(a[i]<a[i-1])
            {
                flag = false;
                break;
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>

/* Author Information
 * Author: JokerNoCry
 * Time: 2020-08-21 22:35:02
 * QQ:1160860141
**/

#define max(a,b) ((a>b)?(a):(b))
#define min(a,b) ((a<b)?(a):(b))
#define ll long long

using namespace std;

int t,n,minnum,a[100010];
int gcd(int x, int y)
{
    while(y^=x^=y^=x%=y);
    return x;
}

void selection_sort(int arr[], int len)
{
    int i,j;

    for (i = 0 ; i < len - 1 ; i++)
    {
        if(arr[i] % minnum !=0 ) continue;
        int min = i;
        for (j = i + 1; j < len; j++)
            if ((arr[j] < arr[min])  && (gcd(arr[j],arr[min]) % minnum == 0 ))
                min = j;
        if(min != i)
        {
            arr[min] = arr[min] ^ arr[i];
            arr[i] = arr[min] ^ arr[i];
            arr[min] = arr[min] ^ arr[i];
        }

    }
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        minnum = 0x7fffffff;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            minnum = min(minnum,a[i]);
        }
        selection_sort(a,n);
        bool flag = true;
        for(int i=1;i<n;i++)
        {
            if(a[i]<a[i-1])
            {
                flag = false;
                break;
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>

/* Author Information
 * Author: JokerNoCry
 * Time: 2020-08-21 22:35:02
 * QQ:1160860141
**/

#define max(a,b) ((a>b)?(a):(b))
#define min(a,b) ((a<b)?(a):(b))
#define ll long long

using namespace std;

int t,n,minnum,a[100010];
int gcd(int x, int y)
{
    while(y^=x^=y^=x%=y);
    return x;
}

int main()
{

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        minnum = 0x7fffffff;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            minnum = min(minnum,a[i]);
        }
        for(int i=0;i<n-1;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if((gcd(a[i],a[j]) == minnum) && a[i]>a[j])
                {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        bool flag = true;
        for(int i=1;i<n;i++)
        {
            if(a[i]<a[i-1])
            {
                flag = false;
                break;
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>

/* Author Information
 * Author: JokerNoCry
 * Time: 2020-08-21 22:35:02
 * QQ:1160860141
**/

#define max(a,b) ((a>b)?(a):(b))
#define min(a,b) ((a<b)?(a):(b))
#define ll long long

using namespace std;

int t,n,minnum,a[100010];
int gcd(int x, int y)
{
    while(y^=x^=y^=x%=y);
    return x;
}

void swap(int *a,int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
void selection_sort(int arr[], int len)
{
    int i,j;

    for (i = 0 ; i < len - 1 ; i++)
    {
        int min = i;
        for (j = i + 1; j < len; j++)
            if ( (gcd(arr[j],arr[min]) == minnum ) && (arr[j] < arr[min]))
                min = j;
           swap(&arr[min], &arr[i]);
    }
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        minnum = 0x7fffffff;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            minnum = min(minnum,a[i]);
        }
        selection_sort(a,n);
        bool flag = true;
        for(int i=1;i<n;i++)
        {
            if(a[i]<a[i-1])
            {
                flag = false;
                break;
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}


#define ll long long int

#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>

using namespace std;

int main() {
    // freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll t, n, k;
    cin >> t;
    while(t--) {
        cin >> n;
        vector<ll> a(n);
        vector<ll> nd;
        for (auto &v : a) {
            cin >> v;
        }
        k = *min_element(a.begin(), a.end());
        for (auto &v : a) {
            if (v % k) {
                nd.push_back(v);
            }
        }
        string ans = "YES";
        ll m = nd.size() - 1;
        for (int i = 0; i < m; ++i) {
            if (nd[i] > nd[i + 1]) {
                ans = "NO";
                break;
            }
        }
        cout << ans << endl;
    }
}


#define ll long long int

#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>

using namespace std;

int main() {
    // freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll t, n, k;
    cin >> t;
    while(t--) {
        cin >> n;
        vector<ll> a(n), free_places(n), cnt_less(n), nd_v;
        vector<bool> nd(n);
        map<ll, ll> cnt;
        for (auto &v : a) {
            cin >> v;
        }

        auto b = vector<ll>(a.begin(), a.end());
        sort(b.begin(), b.end());
        ll sum = 0;
        for (int i = 1; i < n; ++i) {
            if (b[i] > b[i - 1]) {
                cnt[b[i - 1]] = sum;
                sum = i;
            }
        }
        if (n > 1 && b[n - 1] > b[n - 2]) {
            cnt[b[n - 1]] = sum;
        }

        k = *min_element(a.begin(), a.end());
        sum = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] % k) {
                nd[i] = 1;
                free_places[i] = i - sum;
                cnt_less[i] = cnt[a[i]] - sum;
                sum += nd[i];
                nd_v.push_back(a[i]);
            }
        }

        string ans = "YES";
        for (int i = 0; i < nd.size(); ++i) {
            if (nd[i] && free_places[i] < cnt_less[i]) {
                ans = "NO";
                break;
            }
        }
        if (ans == "YES" && nd_v.size() > 1) {
            for (int i = 0; i < nd_v.size() - 1; ++i) {
                if (nd_v[i] > nd_v[i + 1]) {
                    ans = "NO";
                }
            }
        }
        cout << ans << endl;
    }
}


#define ll long long int

#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>

using namespace std;

int main() {
    // freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll t, n;
    cin >> t;
    while(t--) {
        cin >> n;
        vector<ll> a(n);
        for (auto &v : a) {
            cin >> v;
        }

        ll m = *min_element(a.begin(), a.end());
        auto b = vector<ll>(a.begin(), a.end());
        sort(b.begin(), b.end());
        string ans = "YES";
        for (int i = 0; i < n; ++i) {
            if (a[i] != b[i] && a[i] % m) {
                ans = "NO";
                break;
            }
        }
        cout << ans << endl;
    }
}


#define ll long long int

#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>

using namespace std;

int main() {
    // freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll t, n, k;
    cin >> t;
    while(t--) {
        cin >> n;
        vector<ll> a(n);
        vector<ll> nd;
        for (auto &v : a) {
            cin >> v;
        }
        k = *min_element(a.begin(), a.end());
        for (auto &v : a) {
            if (v % k) {
                nd.push_back(v);
            }
        }
        string ans = "YES";
        ll m = nd.size() - 1;
        for (int i = 0; m >= 0 && i < m; ++i) {
            if (nd[i] > nd[i + 1]) {
                ans = "NO";
                break;
            }
        }
        cout << ans << endl;
    }
}


#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
map<int,int> ma;
int arr[100005],s[100005];
bool cnt[100005];
int main() {
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		int minn=1e9,maxn=-1,tot=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&arr[i]);
			s[i]=arr[i];
			minn=min(minn,arr[i]);
			maxn=max(maxn,arr[i]);
			if(!ma[arr[i]]) ma[arr[i]]=++tot;
		}
		sort(s+1,s+1+n);
		for(int i=1;i<=n;i++) if(arr[i]%minn==0) cnt[ma[arr[i]]]=true;
		bool flag=false;
		for(int i=1;i<=n;i++)
		{
			if(s[i]!=arr[i]&&(!(cnt[ma[s[i]]]&&cnt[ma[arr[i]]])))
			{
				puts("NO");
				flag=true;
				break;
			}
		}
		if(!flag) puts("YES");
		for(int i=1;i<=tot;i++) cnt[i]=false;
		ma.clear();
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int arr[100005],s[100005];
bool cnt[100005];
int main() {
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		int minn=1e9;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&arr[i]);
			s[i]=arr[i];
			minn=min(minn,arr[i]);
		}
		for(int i=1;i<=n;i++) if(arr[i]%minn==0) cnt[arr[i]]=true;
		sort(s+1,s+1+n);
		bool flag=false;
		for(int i=1;i<=n;i++)
		{
			if(s[i]!=arr[i]&&(!(cnt[s[i]]&&cnt[arr[i]])))
			{
				puts("NO");
				flag=true;
				break;
			}
		}
		if(!flag) puts("YES");
		memset(cnt,0,sizeof(cnt));
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
int arr[100005],s[100005];
bool cnt[100005];
int main() {
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		int num=0,minn=1e9;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&arr[i]);
			s[i]=arr[i];
			minn=min(minn,arr[i]);
		}
		for(int i=1;i<=n;i++) if(arr[i]%minn==0) cnt[arr[i]]=true;
		sort(s+1,s+1+n);
		bool flag=false;
		for(int i=1;i<=n;i++)
		{
			if(s[i]!=arr[i]&&(!(cnt[s[i]]&&cnt[arr[i]])))
			{
				puts("NO");
				flag=true;
				break;
			}
		}
		if(!flag) puts("YES");
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
int arr[100005];
inline int gcd(int a,int b)
{
	if(b==0) return a;
	else return gcd(b,a%b);
}
int main() {
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		int num=0,minn=1e9;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&arr[i]);
			if(arr[i]<minn) minn=arr[i],num=1;
			else if(arr[i]==minn) num++;
		}
		bool flag=true;
		for(int i=1;i<=num;i++)
		{
			if(arr[i]%minn!=0)  flag=false;
		}
		if(flag) puts("YES");
		else puts("NO");
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int arr[100005],s[100005];
bool cnt[1000000005];
int main() {
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		int minn=1e9,maxn=-1;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&arr[i]);
			s[i]=arr[i];
			minn=min(minn,arr[i]);
			maxn=max(maxn,arr[i]);
		}
		for(int i=1;i<=n;i++) if(arr[i]%minn==0) cnt[arr[i]]=true;
		sort(s+1,s+1+n);
		bool flag=false;
		for(int i=1;i<=n;i++)
		{
			if(s[i]!=arr[i]&&(!(cnt[s[i]]&&cnt[arr[i]])))
			{
				puts("NO");
				flag=true;
				break;
			}
		}
		if(!flag) puts("YES");
		for(int i=1;i<=maxn;i++) cnt[i]=false;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define cs const
#define fr first
#define se second
#define ls (now<<1)
#define rs (now<<1|1)
#define mid ((l+r)>>1)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define low(i) (i&(-i))
#define par pair<int,int>
#define cn(x) memset(x, 0, sizeof(x))
#define rep(i, x, y) for(int i=x; i<=y; ++i)
#define sep(i, x, y) for(int i=x; i>=y; --i)
#define fore(i, x) for(int i=fir[x]; i; i=nex[i])

cs int G = 3;
cs int ff = 2e6 + 1;
cs int inf = 1e9 + 1;
cs int base = 2333;
cs int mod = 1e9 + 7;

int n, a[ff], b[ff], mn, c[ff], tag[ff], d[ff], cnt;
int tr[ff];
void add(int x)
{
	for(int i = x; i <= n; i = i + low(i))
		tr[x] ++;
}
int qry(int x, int y = 0)
{
	for(int i = x; i >= 1; i = i - low(i))
		y += tr[x];
	return y;
}
void init()
{
	cin >> n; cnt = 0;// cn(tr);
	rep(i, 1, n) scanf("%lld", &a[i]), b[i] = a[i];
	sort(b+1, b+1+n); mn = b[1];
	int tot = unique(b+1, b+1+n) - b - 1;
	rep(i, 1, n) c[i] = lower_bound(b+1, b+1+tot, a[i]) - b, tag[i] = (a[i] % mn == 0);
	rep(i, 1, n) if(!tag[i]) d[++cnt] = a[i];
	rep(i, 1, cnt - 1) if(d[i] > d[i+1]) { puts("NO"); return; }

	int s = 0;
	rep(i, 1, n) if(tag[i]) tr[c[i]] ++;
	rep(i, 1, n) tr[i] = tr[i-1] + tr[i];//, cout << tr[i] << " "; cout << endl;
	rep(i, 1, n)
	{
		if(!tag[i])
		{
			int Q = tr[c[i] - 1];
			if(Q != s) { puts("NO"); return; }
		}
		else s ++;//, add(c[i]);
	}
	puts("YES");
	rep(i, 1, n) tr[i] = 0;
}
signed main()
{
	int Ts = 1;
	cin >> Ts;
	while(Ts--)
		init();
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define cs const
#define fr first
#define se second
#define ls (now<<1)
#define rs (now<<1|1)
#define mid ((l+r)>>1)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define low(i) (i&(-i))
#define par pair<int,int>
#define cn(x) memset(x, 0, sizeof(x))
#define rep(i, x, y) for(int i=x; i<=y; ++i)
#define sep(i, x, y) for(int i=x; i>=y; --i)
#define fore(i, x) for(int i=fir[x]; i; i=nex[i])

cs int G = 3;
cs int ff = 2e6 + 1;
cs int inf = 1e9 + 1;
cs int base = 2333;
cs int mod = 1e9 + 7;

int n, a[ff], b[ff], mn, c[ff], tag[ff], d[ff], cnt;
int tr[ff];
void init()
{
	cin >> n; cnt = 0;// cn(tr);
	rep(i, 1, n) scanf("%lld", &a[i]), b[i] = a[i];
	sort(b+1, b+1+n); mn = b[1];
	int tot = unique(b+1, b+1+n) - b - 1;
	rep(i, 1, n) c[i] = lower_bound(b+1, b+1+tot, a[i]) - b, tag[i] = (a[i] % mn == 0);
	rep(i, 1, n) if(!tag[i]) d[++cnt] = a[i];
	rep(i, 1, cnt - 1) if(d[i] > d[i+1]) { puts("NO"); return; }

	int s = 0;
	rep(i, 1, n) tr[i] = 0;
	rep(i, 1, n) if(tag[i]) tr[c[i]] ++;
	rep(i, 1, n) tr[i] = tr[i-1] + tr[i];//, cout << tr[i] << " "; cout << endl;

//	cout << endl;
//	rep(i, 1, n) cout << tr[i] << " "; cout <<endl;
//	rep(i, 1, cnt) cout << d[i] << " "; cout << mn << endl;
	rep(i, 1, n)
	{
		if(!tag[i])
		{
			int Q = tr[c[i]];
			if(Q != s) { puts("NO"); return; }
		}
		else s ++;//, add(c[i]);
	}
	puts("YES");
	rep(i, 0, n) tr[i] = tag[i] = b[i] = c[i] = 0;
	rep(i, 1, cnt) d[i] = 0;
}
signed main()
{
	int Ts = 1;
	cin >> Ts;
	while(Ts--)
		init();
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define cs const
#define fr first
#define se second
#define ls (now<<1)
#define rs (now<<1|1)
#define mid ((l+r)>>1)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define low(i) (i&(-i))
#define par pair<int,int>
#define cn(x) memset(x, 0, sizeof(x))
#define rep(i, x, y) for(int i=x; i<=y; ++i)
#define sep(i, x, y) for(int i=x; i>=y; --i)
#define fore(i, x) for(int i=fir[x]; i; i=nex[i])

cs int G = 3;
cs int ff = 2e6 + 1;
cs int inf = 1e9 + 1;
cs int base = 2333;
cs int mod = 1e9 + 7;

int n, a[ff], b[ff], mn, c[ff], tag[ff], d[ff], cnt;
int tr[ff];
void add(int x)
{
	for(int i = x; i <= n; i = i + low(i))
		tr[x] ++;
}
int qry(int x, int y = 0)
{
	for(int i = x; i >= 1; i = i - low(i))
		y += tr[x];
	return y;
}
void init()
{
	cin >> n; cnt = 0; cn(tr);
	rep(i, 1, n) scanf("%lld", &a[i]), b[i] = a[i];
	sort(b+1, b+1+n); mn = b[1];
	int tot = unique(b+1, b+1+n) - b - 1;
	rep(i, 1, n) c[i] = lower_bound(b+1, b+1+tot, a[i]) - b, tag[i] = (a[i] % mn == 0);
	rep(i, 1, n) if(!tag[i]) d[++cnt] = a[i];
	rep(i, 1, cnt - 1) if(d[i] > d[i+1]) { puts("NO"); return; }

	int s = 0;
	rep(i, 1, n) if(tag[i]) tr[c[i]] ++;
	rep(i, 1, n) tr[i] = tr[i-1] + tr[i], cout << tr[i] << " "; cout << endl;
	rep(i, 1, n)
	{
		if(!tag[i])
		{
			int Q = tr[c[i] - 1];
			if(Q != s) { puts("NO"); return; }
		}
		else s ++;//, add(c[i]);
	}
	puts("YES");
}
signed main()
{
	int Ts = 1;
	cin >> Ts;
	while(Ts--)
		init();
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define cs const
#define fr first
#define se second
#define ls (now<<1)
#define rs (now<<1|1)
#define mid ((l+r)>>1)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define low(i) (i&(-i))
#define par pair<int,int>
#define cn(x) memset(x, 0, sizeof(x))
#define rep(i, x, y) for(int i=x; i<=y; ++i)
#define sep(i, x, y) for(int i=x; i>=y; --i)
#define fore(i, x) for(int i=fir[x]; i; i=nex[i])

cs int G = 3;
cs int ff = 2e6 + 1;
cs int inf = 1e9 + 1;
cs int base = 2333;
cs int mod = 1e9 + 7;

int n, a[ff], b[ff], mn, c[ff], tag[ff], d[ff], cnt;
int tr[ff];
void add(int x)
{
	for(int i = x; i <= n; i = i + low(i))
		tr[x] ++;
}
int qry(int x, int y = 0)
{
	for(int i = x; i >= 1; i = i - low(i))
		y += tr[x];
	return y;
}
void init()
{
	cin >> n; cnt = 0; cn(tr);
	rep(i, 1, n) scanf("%lld", &a[i]), b[i] = a[i];
	sort(b+1, b+1+n); mn = b[1];
	int tot = unique(b+1, b+1+n) - b - 1;
	rep(i, 1, n) c[i] = lower_bound(b+1, b+1+tot, a[i]) - b, tag[i] = (a[i] % mn == 0);
	rep(i, 1, n) if(!tag[i]) d[++cnt] = a[i];
	rep(i, 1, cnt - 1) if(d[i] > d[i+1]) { puts("NO"); return; }

	int s = 0;
	rep(i, 1, n) if(tag[i]) tr[c[i]] ++;
	rep(i, 1, n) tr[i] = tr[i-1] + tr[i];//, cout << tr[i] << " "; cout << endl;
	rep(i, 1, n)
	{
		if(!tag[i])
		{
			int Q = tr[c[i] - 1];
			if(Q != s) { puts("NO"); return; }
		}
		else s ++;//, add(c[i]);
	}
	puts("YES");
}
signed main()
{
	int Ts = 1;
	cin >> Ts;
	while(Ts--)
		init();
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5,inf = 1e9+5;
int n,a[N],t[N],b[N];
int notp[N],p[N],tot;
int f[N][500];
//f[i][j]前i个数中，有p[j]这个质因数的最大的数
void fid() {
	for(int i=2;i<=N;i++) {
		if(!notp[i]) p[++tot] = i;
		for(int j=1;j<=tot && p[j]*i<=N;j++) {
			notp[p[j]*i] = 1;
			if(i%p[j] ==0) break;
		}
	}
}
int gcd(int x,int y){return y==0? x: gcd(y,x%y);}
int main() {
	fid();
	// cout<<p[5000]<<endl;
	int T;
	scanf("%d",&T);
	while(T--) {
		scanf("%d",&n);
		int mi = inf, c=0, cnt = 0;
		for(int i=1;i<=n;i++) {
			scanf("%d",&a[i]);
			mi=min(mi,a[i]);
		}
		for(int i=1;i<=n;i++) {
			if(a[i]%mi) t[++cnt] = a[i];
			else b[++c] = a[i]/mi;
		}
		int flag = 0;
		for(int i=1;i<cnt;i++) {
			if(t[i+1] < t[i]) {
				printf("NO\n");
				flag = 1;
				break;
			}
		}
		if(flag) continue;
		map<int,int> mp;
		for(int i=1;i<=c;i++) {
			int tmp = b[i];
			for(int j=1;p[j]<=b[i] && j<=5000;j++) {
				if(tmp%p[j]) f[i][j] = max(f[i-1][j],b[i]);
				while(tmp%p[j]==0 && tmp>1) tmp /= p[j];
			}
			for(int j=1;j<=5000;j++) f[i][j] = max(f[i][j],f[i-1][j]);
			if(tmp>1) mp[tmp] = max(mp[tmp],b[i]);
		}
		int flg = 0;
		for(int i=c;i>=1;i--) {
			int tmp = b[i];
			for(int j=1;j<=5000;j++) {
				if(b[i]%p[j]==0 && f[i-1][j] > b[i]) {
					printf("NO\n");
					flg = 1;
					break;
				}
				while(tmp%p[j]==0 && tmp>1) tmp /= p[j];
			}
			if(tmp>1 && mp[tmp] > b[i]){
				printf("NO\n");
				flg = 1;
			}
			if(flg) break;

		}
		if(flg) continue;
		printf("YES\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5,inf = 1e9+5;
int n,a[N],t[N],b[N];
int notp[N],p[N],tot;
int f[N][500];
//f[i][j]前i个数中，有p[j]这个质因数的最大的数
void fid() {
	for(int i=2;i<=N;i++) {
		if(!notp[i]) p[++tot] = i;
		for(int j=1;j<=tot && p[j]*i<=N;j++) {
			notp[p[j]*i] = 1;
			if(i%p[j] ==0) break;
		}
	}
}
int gcd(int x,int y){return y==0? x: gcd(y,x%y);}
int main() {
	fid();
	// cout<<p[5000]<<endl;
	int T;
	scanf("%d",&T);
	while(T--) {
		scanf("%d",&n);
		int mi = inf, c=0, cnt = 0;
		for(int i=1;i<=n;i++) {
			scanf("%d",&a[i]);
			mi=min(mi,a[i]);
		}
		for(int i=1;i<=n;i++) {
			if(a[i]%mi) t[++cnt] = a[i];
			else b[++c] = a[i]/mi;
		}
		int flag = 0;
		for(int i=1;i<cnt;i++) {
			if(t[i+1] < t[i]) {
				printf("NO\n");
				flag = 1;
				break;
			}
		}
		if(flag) continue;
		map<int,int> mp;
		for(int i=1;i<=c;i++) {
			int tmp = b[i];
			// cout<<tmp<<" ";
			for(int j=1;p[j]<=b[i] && j<=5000;j++) {
				if(tmp%p[j]) f[i][j] = max(f[i-1][j],b[i]);
				while(tmp%p[j]==0 && tmp>1) tmp /= p[j];
			}
			for(int j=1;j<=5000;j++) f[i][j] = max(f[i][j],f[i-1][j]);
			if(tmp) mp[tmp] = 1;
		}
		// cout<<endl;
		int flg = 0;
		for(int i=c;i>=1;i--) {
			for(int j=1;j<=5000;j++) {
				if(b[i]%p[j]==0 && f[i-1][j] > b[i]) {
					printf("NO\n");
					flg = 1;
					break;
				}
			}
			if(flg) break;
		}
		if(flg) continue;
		printf("YES\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N=1e5+10,inf=1e9;
double dp[N],x[N],b[N];
int t, n, m,pre[N],First[N], Second[N];
bool vis[N];
bool check(double mid) {
	for(int i=1;i<=n;i++) dp[i] = inf;
	for(int i=0;i<=n;i++) {
		for(int j=i+1;j<=n;j++) {
			double tmp = dp[i] + sqrt(abs(x[j]-x[i]-m))-1.0*mid*b[j];
			if(dp[j] > tmp) {
				dp[j] = tmp;
				pre[j] = i;
			}
		}
	}
	return dp[n] >= 0;
}
void prnt(int x) {
	if(x==0) return ;
	prnt(pre[x]);
	printf("%d ",x);
}
int main() {
    scanf("%d", &t);
    while(t--) {
        memset(vis, false, sizeof(vis));
        scanf("%d", &n);
        for(register int i=1; i<=n; ++i) {scanf("%d", &First[i]); Second[i]=First[i];}
        sort(Second+1, Second+1+n); int Min=Second[1];
        for(register int i=1; i<=n; ++i) {if(First[i]%Min) vis[i]=true;}
        bool f=true;
        for(register int i=1; i<=n; ++i) {
            if(vis[i]) { if(First[i]^Second[i]) {f=false; break;} }
        }
        puts(f?"YES":"NO");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5,inf = 1e9+5;
int n,a[N],t[N],b[N];
int notp[N],p[N],tot;
int f[N][500];
//f[i][j]前i个数中，有p[j]这个质因数的最大的数
void fid() {
	for(int i=2;i<=N;i++) {
		if(!notp[i]) p[++tot] = i;
		for(int j=1;j<=tot && p[j]*i<=N;j++) {
			notp[p[j]*i] = 1;
			if(i%p[j] ==0) break;
		}
	}
}
int gcd(int x,int y){return y==0? x: gcd(y,x%y);}
int main() {
	fid();
	int T;
	scanf("%d",&T);
	while(T--) {
		scanf("%d",&n);
		for(int i=1;i<=n;i++) for(int j=1;j<=5000;j++) f[i][j] = 0;
		int mi = inf, c=0, cnt = 0;
		for(int i=1;i<=n;i++) {
			scanf("%d",&a[i]);
			mi=min(mi,a[i]);
		}
		for(int i=1;i<=n;i++) {
			if(a[i]%mi) b[++c] = a[i];
			else b[++c] = a[i]/mi;
		}
		map<int,int> mp;
		for(int i=1;i<=c;i++) {
			int tmp = b[i];
			for(int j=1;p[j]<=b[i] && j<=5000;j++) {
				if(tmp%p[j]) f[i][j] = max(f[i-1][j],b[i]);
				while(tmp%p[j]==0 && tmp>1) tmp /= p[j];
			}
			for(int j=1;j<=5000;j++) f[i][j] = max(f[i][j],f[i-1][j]);
			if(tmp>1) mp[tmp] = max(mp[tmp],b[i]);
		}
		int flg = 0;
		for(int i=c;i>=1;i--) {
			int tmp = b[i];
			for(int j=1;j<=5000;j++) {
				if(b[i]%p[j]==0 && f[i-1][j] > b[i]) {
					printf("NO\n");
					flg = 1;
					break;
				}
				while(tmp%p[j]==0 && tmp>1) tmp /= p[j];
			}
			if(tmp>1 && mp[tmp] > b[i]){
				printf("NO\n");
				flg = 1;
			}
			if(flg) break;

		}
		if(flg) continue;
		printf("YES\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5,inf = 1e9+5;
int n,a[N],t[N],b[N];
int notp[N],p[N],tot;
int f[N][500];
//f[i][j]前i个数中，有p[j]这个质因数的最大的数
void fid() {
	for(int i=2;i<=N;i++) {
		if(!notp[i]) p[++tot] = i;
		for(int j=1;j<=tot && p[j]*i<=N;j++) {
			notp[p[j]*i] = 1;
			if(i%p[j] ==0) break;
		}
	}
}
int gcd(int x,int y){return y==0? x: gcd(y,x%y);}
int main() {
	fid();
	// cout<<p[5000]<<endl;
	int T;
	scanf("%d",&T);
	while(T--) {
		scanf("%d",&n);
		int mi = inf, c=0, cnt = 0;
		for(int i=1;i<=n;i++) {
			scanf("%d",&a[i]);
			mi=min(mi,a[i]);
		}
		for(int i=1;i<=n;i++) {
			if(a[i]%mi) t[++cnt] = a[i];
			else b[++c] = a[i]/mi;
		}
		int flag = 0;
		for(int i=1;i<cnt;i++) {
			if(t[i+1] < t[i]) {
				printf("NO\n");
				flag = 1;
				break;
			}
		}
		if(flag) continue;
		printf("YES\n");
	}
	return 0;
}


#include<math.h>
#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
using namespace std;
#define fo(i,n) for(int i=0;i<n;i++)
#define deg(x) cout<<#x<<" "<< x <<endl;
#define mod 1000000007
#define ll long long
#define print(x) cout<<x<<endl;
#define input(x) cin>>x
#define all(v) v.begin(),v.end()
#define fast ios_base::sync_with_stdio(false)
ll gcd(ll a, ll b)
{
    // Everything divides 0
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    // base case
    if (a == b)
        return a;

    // a is greater
    if (a > b)
        return gcd(a-b, b);
    return gcd(a, b-a);
}

int main()
{
    int t;
    cin>>t;
while(t>0)
{

int n;
cin>>n;

ll a[n];
ll b[n];
int i;
ll mn=INT16_MAX;
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
mn=min(a[i],mn);
}

sort(a,a+n);
bool p=true;
for(i=0;i<n;i++)
{
    if(gcd(b[i],mn)!=mn&&b[i]!=a[i])
    p=false;

}

if(p)
{
print("YES");
}
else
print("NO");

    t--;
}

}


#include<math.h>
#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
using namespace std;
#define fo(i,n) for(int i=0;i<n;i++)
#define deg(x) cout<<#x<<" "<< x <<endl;
#define mod 1000000007
#define ll long long
#define print(x) cout<<x<<endl;
#define input(x) cin>>x
#define all(v) v.begin(),v.end()
#define fast ios_base::sync_with_stdio(false)

int main()
{
    int t;
    cin>>t;
while(t>0)
{

int n;
cin>>n;

ll a[n];
ll b[n];
int i;
ll mn=INT16_MAX;
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
mn=min(a[i],mn);
}

sort(a,a+n);
bool p=true;
for(i=0;i<n;i++)
{
    if(b[i]%mn!=0&&b[i]!=a[i])
    p=false;
}

if(p)
{
print("YES");
}
else
print("NO");

    t--;
}

}


#include<math.h>
#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
using namespace std;
#define fo(i,n) for(int i=0;i<n;i++)
#define deg(x) cout<<#x<<" "<< x <<endl;
#define mod 1000000007
#define ll long long
#define print(x) cout<<x<<endl;
#define input(x) cin>>x
#define all(v) v.begin(),v.end()
#define fast ios_base::sync_with_stdio(false)
ll gcd(ll a, ll b)
{
    // Everything divides 0
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    // base case
    if (a == b)
        return a;

    // a is greater
    if (a > b)
        return gcd(a-b, b);
    return gcd(a, b-a);
}

int main()
{
    int t;
    cin>>t;
while(t>0)
{

int n;
cin>>n;

ll a[n];
ll b[n];
int i;
ll mn=INT16_MAX;
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
mn=min(a[i],mn);
}

sort(a,a+n);
bool p=true;
for(i=0;i<n;i++)
{
    if(gcd(b[i],mn)!=mn&&b[i]!=a[i])
    p=false;
    else if(gcd(b[i],mn)==mn)
    continue;

}

if(p)
{
print("YES");
}
else
print("NO");

    t--;
}

}


#include<math.h>
#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
using namespace std;
#define fo(i,n) for(int i=0;i<n;i++)
#define deg(x) cout<<#x<<" "<< x <<endl;
#define mod 1000000007
#define ll long long
#define print(x) cout<<x<<endl;
#define input(x) cin>>x
#define all(v) v.begin(),v.end()
#define fast ios_base::sync_with_stdio(false)

int main()
{
    int t;
    cin>>t;
while(t>0)
{

int n;
cin>>n;

ll a[n];
ll b[n];
int i;
ll mn=INT16_MAX;
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
mn=min(a[i],mn);
}

sort(a,a+n);
bool p=true;
for(i=0;i<n;i++)
{
    if(a[i]%mn>0&&b[i]!=a[i])
    p=false;
}

if(p)
{
print("YES");
}
else
print("NO");

    t--;
}

}


#include<math.h>
#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
using namespace std;
#define fo(i,n) for(int i=0;i<n;i++)
#define deg(x) cout<<#x<<" "<< x <<endl;
#define mod 1000000007
#define ll long long
#define print(x) cout<<x<<endl;
#define input(x) cin>>x
#define all(v) v.begin(),v.end()
#define fast ios_base::sync_with_stdio(false)

int main()
{
    int t;
    cin>>t;
while(t>0)
{

int n;
cin>>n;

ll a[n];
ll b[n];
int i;
ll mn=INT64_MAX;
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
mn=min(a[i],mn);
}

sort(a,a+n);
bool p=true;
for(i=0;i<n;i++)
{
    if(b[i]%mn>0&&b[i]!=a[i])
    p=false;
}

if(p)
{
print("YES");
}
else
print("NO");

    t--;
}

}


#include<math.h>
#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
using namespace std;
#define fo(i,n) for(int i=0;i<n;i++)
#define deg(x) cout<<#x<<" "<< x <<endl;
#define mod 1000000007
#define ll long long
#define print(x) cout<<x<<endl;
#define input(x) cin>>x
#define all(v) v.begin(),v.end()
#define fast ios_base::sync_with_stdio(false)

int main()
{
    int t;
    cin>>t;
while(t>0)
{

int n;
cin>>n;

int a[n];
int b[n];
int i;
int mn=INT8_MAX;
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
mn=min(a[i],mn);
}

sort(a,a+n);
bool p=true;
for(i=0;i<n;i++)
{
    if(b[i]%mn!=0&&b[i]!=a[i])
    p=false;
}

if(p)
{
print("YES");
}
else
print("NO");

    t--;
}

}


#include<math.h>
#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
using namespace std;
#define fo(i,n) for(int i=0;i<n;i++)
#define deg(x) cout<<#x<<" "<< x <<endl;
#define mod 1000000007
#define ll long long
#define print(x) cout<<x<<endl;
#define input(x) cin>>x
#define all(v) v.begin(),v.end()
#define fast ios_base::sync_with_stdio(false)

int main()
{
    int t;
    cin>>t;
while(t>0)
{

int n;
cin>>n;

ll a[n];
ll b[n];
int i;
ll mn=INT16_MAX;
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
mn=min(a[i],mn);
}

sort(a,a+n);
bool p=true;
for(i=0;i<n;i++)
{
    if(b[i]%mn>0&&b[i]!=a[i])
    p=false;
}

if(p)
{
print("YES");
}
else
print("NO");

    t--;
}

}


#include <iostream>
#include <algorithm>

using namespace std;

const int N=1e5+10;
int t,n,minn;
int a[N];

int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int work()
{
	for(int i=0;i<n;i++)
		for(int j=i;j<n;j++)
		{
			if(a[i]>a[j]&&gcd(a[i],a[j])==minn)
			{
				int temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
		}
	for(int i=1;i<n;i++)
	{
		if(a[i]<a[i-1]) return 0;
	}
	return 1;
}
int main()
{
	cin>>t;
	while(t--)
	{
		minn=1e9+1;
		cin>>n;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			minn=min(minn,a[i]);
		}
		if(work()) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int N=1e5+10;
int t,n;
int a[N],b[N];

int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			b[i]=a[i];
		}
		sort(b,b+n);
		int flag=1;
		for(int i=0;i<n;i++)
		{
			if(a[i]!=b[i]&&a[i]%b[0]!=0)
			{
				printf("NO\n");
				flag=0;
				break;
			}
		}
		if(flag) printf("YES\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int N=1e5+10;
int t,n,minn;
int a[N];

int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int work()
{
	for(int i=0;i<n;i++)
		for(int j=i;j<n;j++)
		{
			if(a[i]>a[j]&&gcd(a[i],a[j])==minn)
			{
				int temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
		}
	for(int i=1;i<n;i++)
		if(a[i]<a[i-1]) return 0;
		else return 1;
}
int work()
{

}
int main()
{
	cin>>t;
	while(t--)
	{
		minn=1e9+1;
		cin>>n;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			minn=min(minn,a[i]);
		}
		if(work()) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int N=1e5+10;
int t,n,minn;
int a[N];

int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int work()
{
	for(int i=0;i<n;i++)
		for(int j=i;j<n;j++)
		{
			if(a[i]>a[j]&&gcd(a[i],a[j])==minn)
			{
				int temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
		}
	for(int i=1;i<n;i++)
		if(a[i]<a[i-1]) return 0;
		else return 1;
}
int main()
{
	cin>>t;
	while(t--)
	{
		minn=1e9+1;
		cin>>n;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			minn=min(minn,a[i]);
		}
		if(work()) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
int main(){
	int t=0;
	cin >> t;
	while(t--){
		int n=0,m=1000000001,f=0;
		cin >> n;
		vector<int> v(n,0),s(n,0);
		for(int i=0;i<n;i++){
			cin >> v[i];
			m=min(v[i],m);
		}
		s=v;
		sort(s.begin(),s.end());
		if(s==v || n==1){
			cout << "YES\n";
		}
		else{
			for(int i=0;i<n;i++){
				if(s[i]!=v[i] && __gcd(s[i],v[i])%m!=0){
					f=1;
					break;
				}
			}
			if(f==1){

				cout << "NO\n";
			}
			else{
				cout << "YES\n";
			}
		}
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
int main(){
	int t=0;
	cin >> t;
	while(t--){
		int n=0,m=1000000001,f=0;
		cin >> n;
		vector<int> v(n,0),s(n,0);
		for(int i=0;i<n;i++){
			cin >> v[i];
			m=min(v[i],m);
		}
		s=v;
		sort(s.begin(),s.end());
		if(s==v || n==1){
			cout << "YES\n";
		}
		else{
			for(int i=0;i<n;i++){
				if(s[i]!=v[i] && __gcd(s[i],v[i])<m){
					f=1;
					break;
				}
			}
			if(f==1){
				cout << "NO\n";
			}
			else{
				cout << "YES\n";
			}
		}
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
int main(){
	int t=0;
	cin >> t;
	while(t--){
		int n=0,m=1000000001,f=0;
		cin >> n;
		vector<int> v(n,0),s(n,0);
		for(int i=0;i<n;i++){
			cin >> v[i];
			m=min(v[i],m);
		}
		s=v;
		sort(s.begin(),s.end());
		if(s==v || n==1){
			cout << "YES\n";
		}
		else{
			for(int i=0;i<n;i++){
				if(s[i]!=v[i] && __gcd(s[i],v[i])!=m){
					f=1;
					break;
				}
			}
			if(f==1){
				cout << "NO\n";
			}
			else{
				cout << "YES\n";
			}
		}
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
int main(){
	int t=0;
	cin >> t;
	while(t--){
		int n=0,m=10000000,f=0;
		cin >> n;
		vector<int> v(n,0),s(n,0);
		for(int i=0;i<n;i++){
			cin >> v[i];
			m=min(v[i],m);
		}
		s=v;
		sort(s.begin(),s.end());
		if(s==v || n==1){
			cout << "Yes\n";
		}
		else{
		for(int i=0;i<n;i++){
			if(s[i]!=v[i] && __gcd(s[i],v[i])!=m){
				f=1;
				break;
			}
		}
		if(f==1){
			cout << "No\n";
		}
		else{
			cout << "Yes\n";
		}
		}
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>

using namespace std;

template <typename T> void chkmax(T &x, T y) {x = x > y ? x : y;}
template <typename T> void chkmin(T &x, T y) {x = x > y ? y : x;}

typedef long long ll;

const int INF = 2139062143;

#define DEBUG(x) std::cerr << #x << " = " << x << std::endl

template <typename T> void read (T &x) {
	x = 0; bool f = 1; char ch;
	do {ch = getchar(); if (ch == '-') f = 0;} while (ch > '9' || ch < '0');
	do {x = x * 10 + ch - '0'; ch = getchar();} while (ch >= '0' && ch <= '9');
	x = f ? x : -x;
}

template <typename T> void write (T x) {
	if (x < 0) x = ~x + 1, putchar ('-');
	if (x > 9) write (x / 10);
	putchar (x % 10 + '0');
}

const int N = 1e5 + 7;

int T, n, mn, a[N], b[N];
bool flag;

inline int gcd(int x, int y) {
	return y == 0 ? x : gcd(y, x % y);
}

int main() {
	read(T);
	while(T--) {
		read(n); mn = INF; flag = true;
		for(int i = 0; i < n; i++) read(a[i]), b[i] = a[i];
		sort(b, b + n);
		for(int i = 0; i < n; i++) {
			if(a[i] != b[i] && gcd(a[i], b[0]) && flag == true) {
				puts("NO");
				flag = false;
				break;
			}
		}
		if(flag) puts("YES");
	}
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>

using namespace std;

template <typename T> void chkmax(T &x, T y) {x = x > y ? x : y;}
template <typename T> void chkmin(T &x, T y) {x = x > y ? y : x;}

typedef long long ll;

const int INF = 2139062143;

#define DEBUG(x) std::cerr << #x << " = " << x << std::endl

template <typename T> void read (T &x) {
	x = 0; bool f = 1; char ch;
	do {ch = getchar(); if (ch == '-') f = 0;} while (ch > '9' || ch < '0');
	do {x = x * 10 + ch - '0'; ch = getchar();} while (ch >= '0' && ch <= '9');
	x = f ? x : -x;
}

template <typename T> void write (T x) {
	if (x < 0) x = ~x + 1, putchar ('-');
	if (x > 9) write (x / 10);
	putchar (x % 10 + '0');
}

const int N = 1e5 + 7;

int T, n, mn, a[N], b[N];
bool flag;

inline int gcd(int x, int y) {
	return y == 0 ? x : gcd(y, x % y);
}

int main() {
	read(T);
	while(T--) {
		read(n); mn = INF; flag = true;
		for(int i = 0; i < n; i++) read(a[i]), b[i] = a[i];
		sort(b, b + n);
		for(int i = 0; i < n; i++) {
			if(a[i] != b[i] && a[i] % b[0] != 0 && flag == true) {
				puts("NO");
				flag = false;
				break;
			}
		}
		if(flag) puts("YES");
	}
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>

using namespace std;

template <typename T> void chkmax(T &x, T y) {x = x > y ? x : y;}
template <typename T> void chkmin(T &x, T y) {x = x > y ? y : x;}

typedef long long ll;

const int INF = 2139062143;

#define DEBUG(x) std::cerr << #x << " = " << x << std::endl

template <typename T> void read (T &x) {
	x = 0; bool f = 1; char ch;
	do {ch = getchar(); if (ch == '-') f = 0;} while (ch > '9' || ch < '0');
	do {x = x * 10 + ch - '0'; ch = getchar();} while (ch >= '0' && ch <= '9');
	x = f ? x : -x;
}

template <typename T> void write (T x) {
	if (x < 0) x = ~x + 1, putchar ('-');
	if (x > 9) write (x / 10);
	putchar (x % 10 + '0');
}

const int N = 1e5 + 7;

int T, n, a[N], b[N];
bool flag;

inline int gcd(int x, int y) {
	if(x < y) swap(x, y);
	return y == 0 ? x : gcd(y, x % y);
}

int main() {
	read(T);
	while(T--) {
		read(n); flag = true;
		for(int i = 0; i < n; i++) read(a[i]), b[i] = a[i];
		sort(b, b + n);
		for(int i = 0; i < n; i++) {
			if(a[i] != b[i] && gcd(a[i], b[0]) != b[0] && flag == true) {
				puts("NO");
				flag = false;
				break;
			}
		}
		if(flag) puts("YES");
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>

using namespace std;

template <typename T> void chkmax(T &x, T y) {x = x > y ? x : y;}
template <typename T> void chkmin(T &x, T y) {x = x > y ? y : x;}

typedef long long ll;

const int INF = 2139062143;

#define DEBUG(x) std::cerr << #x << " = " << x << std::endl

template <typename T> void read (T &x) {
	x = 0; bool f = 1; char ch;
	do {ch = getchar(); if (ch == '-') f = 0;} while (ch > '9' || ch < '0');
	do {x = x * 10 + ch - '0'; ch = getchar();} while (ch >= '0' && ch <= '9');
	x = f ? x : -x;
}

template <typename T> void write (T x) {
	if (x < 0) x = ~x + 1, putchar ('-');
	if (x > 9) write (x / 10);
	putchar (x % 10 + '0');
}

const int N = 1e5 + 7;

int T, n, mn, a[N], id[N];
bool flag;

inline bool cmp(int x, int y) {
	return a[x] < a[y];
}

inline int gcd(int x, int y) {
	return y == 0 ? x : gcd(y, x % y);
}

int main() {
	read(T);
	while(T--) {
		read(n); mn = INF; flag = true;
		for(int i = 0; i < n; i++) read(a[i]), id[i] = i, mn = min(mn, a[i]);
		sort(id, id + n, cmp);
		for(int i = 0; i < n; i++) if(id[i] != i && gcd(a[i], mn) != mn) flag = false;
		if(flag) puts("YES");
		else puts("NO");
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define mod 1000000007
#define inf 1e18
typedef vector<ll> vl;
typedef vector<int> vi;
typedef set<ll> sl;
typedef map<ll, ll> ml;
typedef map<int,int> mi;
typedef unordered_map<ll,ll> uml;
typedef unordered_map<int,int> umi;
typedef pair <ll,ll> pi;

int gcd(int a, int b){
    if(b==0)return a;
    return gcd(b, a%b);
}

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    int ad[n];
    int moti[n];
    for(int i=0; i<n; i++){
        int x;
        cin>>x;
        ad[i]=x;
        moti[i]=x;
    }
    sort(moti, moti+n);
    // for(auto i:moti)cout<<i;

    int lalla =moti[0];

    vector<int> v;
    for(int i=0; i<n; i++){
        if(ad[i]!=moti[i]) v.pb(ad[i]);
    }
    // sort(v.begin(), v.end());
    if(v.size()==0){cout<<"YES"<<endl; continue;}

    int x, flag=0;
    for(int i=0; i<v.size()-1; i++){
        // x= gcd(v[i],v[i+1]);
        // if(x!=lalla){ cout<<"NO"<<endl; flag =1; break;}
        if(v[i]%lalla!=0){ cout<<"NO"<<endl; flag =1; break;}
    }

    // cout<<x;
    // cout<<lalla;
    if(flag==0)cout<<"YES"<<endl;

}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define mod 1000000007
#define inf 1e18
typedef vector<ll> vl;
typedef vector<int> vi;
typedef set<ll> sl;
typedef map<ll, ll> ml;
typedef map<int,int> mi;
typedef unordered_map<ll,ll> uml;
typedef unordered_map<int,int> umi;
typedef pair <ll,ll> pi;

int gcd(int a, int b){
    if(b==0)return a;
    return gcd(b, a%b);
}

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    int ad[n];
    int moti[n];
    for(int i=0; i<n; i++){
        int x;
        cin>>x;
        ad[i]=x;
        moti[i]=x;
    }
    sort(moti, moti+n);
    // for(auto i:moti)cout<<i;

    int lalla =moti[0];

    vector<int> v;
    for(int i=0; i<n; i++){
        if(ad[i]!=moti[i]) v.pb(ad[i]);
    }
    // sort(v.begin(), v.end());
    if(v.size()==0){cout<<"YES"<<endl; continue;}

    int x, flag=0;
    for(int i=0; i<v.size(); i++){
        x= gcd(v[i],lalla);
        if(x!=lalla){ cout<<"NO"<<endl; flag =1; break;}
        // if(v[i]%lalla!=0){ cout<<"NO"<<endl; flag =1; break;}
    }

    // cout<<x;
    // cout<<lalla;
    if(flag==0)cout<<"YES"<<endl;

}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define mod 1000000007
#define inf 1e18
typedef vector<ll> vl;
typedef vector<int> vi;
typedef set<ll> sl;
typedef map<ll, ll> ml;
typedef map<int,int> mi;
typedef unordered_map<ll,ll> uml;
typedef unordered_map<int,int> umi;
typedef pair <ll,ll> pi;

int gcd(int a, int b){
    if(b==0)return a;
    return gcd(b, a%b);
}

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    int ad[n];
    int moti[n];
    for(int i=0; i<n; i++){
        int x;
        cin>>x;
        ad[i]=x;
        moti[i]=x;
    }
    if(n==1){cout<<"YES"<<endl; continue;}
    sort(moti, moti+n);
    // for(auto i:moti)cout<<i;

    int lalla =moti[0];

    vector<int> v;
    for(int i=0; i<n; i++){
        if(ad[i]!=moti[i]) v.pb(ad[i]);
    }
    if(v.size()==0){cout<<"YES"<<endl; continue;}

    int x=0;
    for(int i=0; i<v.size(); i++){
        x= gcd(v[i],x);
    }
    if(x!=lalla){ cout<<"NO"<<endl; continue;}
    // cout<<x;
    // cout<<lalla;
    cout<<"YES"<<endl;

}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define mod 1000000007
#define inf 1e18
typedef vector<ll> vl;
typedef vector<int> vi;
typedef set<ll> sl;
typedef map<ll, ll> ml;
typedef map<int,int> mi;
typedef unordered_map<ll,ll> uml;
typedef unordered_map<int,int> umi;
typedef pair <ll,ll> pi;

int gcd(int a, int b){
    if(b==0)return a;
    return gcd(b, a%b);
}

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    int ad[n];
    int moti[n];
    for(int i=0; i<n; i++){
        int x;
        cin>>x;
        ad[i]=x;
        moti[i]=x;
    }
    sort(moti, moti+n);
    // for(auto i:moti)cout<<i;

    int lalla =moti[0];

    vector<int> v;
    for(int i=0; i<n; i++){
        if(ad[i]!=moti[i]) v.pb(ad[i]);
    }
    sort(v.begin(), v.end());
    if(v.size()==0){cout<<"YES"<<endl; continue;}

    int x, flag=0;
    for(int i=0; i<v.size()-1; i++){
        x= gcd(v[i],v[i+1]);
        if(x!=lalla){ cout<<"NO"<<endl; flag =1; break;}
    }

    // cout<<x;
    // cout<<lalla;
    if(flag==0)cout<<"YES"<<endl;

}

    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
//#define INF 1000001
using namespace std;
//set<ll>st;
vector<vector<ll> >v;
//string s;
//map<ll,ll>mp;
//map<ll,ll>::iterator it;
//ll MAX_N=1000000002;
ll bin_search(ll val,ll i){
   ll l=i,r=v.size(),mid;
   while(l<=r){
      mid=(l+r)/2;
      if(v[mid][0]==val)return mid;
      else if(v[mid][0]<val)l=mid+1;
      else r=mid-1;
   }
}
bool cmp(vector<ll>v1,vector<ll>v2){
  return v1<v2;
}
ll gcd(ll a,ll b){
 if(!a)return b;
 gcd(b%a,a);
}
int main()
{
 ios::sync_with_stdio(0);cin.tie(0);
 ll t,n,z;
    cin>>t;
    while(t--){
      cin>>n;
      ll ar[n+2],p=1;
      for(ll i=1;i<=n;i++){
         cin>>ar[i];
         vector<ll>temp;
         temp.push_back(ar[i]);
         temp.push_back(i);
         v.push_back(temp);
      }
      sort(v.begin(),v.end(),cmp);
      for(ll i=1;i<=n;i++){
        if(ar[i]==v[i-1][0])continue;
        ll x=gcd(ar[i],ar[v[i-1][1]]);
        if(x==v[0][0]){
           swap(ar[i],ar[v[i-1][1]]);
           ll z=bin_search(ar[i],i);
           swap(v[i-1][1],v[z-1][1]);
        }
        else{
          p=0;break;
        }
      }
      if(p)cout<<"YES"<<endl;
      else cout<<"NO"<<endl;
      v.clear();
    }
 return 0;
}


#include<bits/stdc++.h>
#define ll long long int
//#define INF 1000001
using namespace std;
//set<ll>st;
vector<vector<ll> >v;
//string s;
//map<ll,ll>mp;
//map<ll,ll>::iterator it;
//ll MAX_N=1000000002;
ll bin_search(ll val,ll i){
   ll l=i,r=v.size(),mid;
   while(l<=r){
      mid=(l+r)/2;
      if(v[mid-1][0]==val)return mid;
      else if(v[mid-1][0]<val)l=mid+1;
      else r=mid-1;
   }
}
bool cmp(vector<ll>v1,vector<ll>v2){
  return v1<v2;
}
ll gcd(ll a,ll b){
 if(!a)return b;
 return gcd(b%a,a);
}
int main()
{
 ios::sync_with_stdio(0);cin.tie(0);
 ll t,n,z;
    cin>>t;
    while(t--){
      cin>>n;
      ll ar[n+5],p=1;
      for(ll i=1;i<=n;i++){
         cin>>ar[i];
         vector<ll>temp;
         temp.push_back(ar[i]);
         temp.push_back(i);
         v.push_back(temp);
      }
      sort(v.begin(),v.end(),cmp);
      for(ll i=1;i<=n;i++){
        if(ar[i]==v[i-1][0])continue;
        ll x=gcd(ar[i],ar[v[i-1][1]]);
        if(x==v[0][0]){
           swap(ar[i],ar[v[i-1][1]]);
           ll z=bin_search(ar[i],i);
           swap(v[i-1][1],v[z-1][1]);
        }
        else{
          p=0;break;
        }
      }
      if(p)cout<<"YES"<<endl;
      else cout<<"NO"<<endl;
      v.clear();
    }
 return 0;
}


#include<bits/stdc++.h>
#define ll long long int
//#define INF 1000001
using namespace std;
//set<ll>st;
vector<vector<ll> >v;
//string s;
//map<ll,ll>mp;
//map<ll,ll>::iterator it;
//ll MAX_N=1000000002;
ll bin_search(ll val,ll i){
   ll l=i,r=v.size(),mid;
   while(l<=r){
      mid=(l+r)/2;
      if(v[mid][0]==val)return mid;
      else if(v[mid][0]<val)l=mid+1;
      else r=mid-1;
   }
}
bool cmp(vector<ll>v1,vector<ll>v2){
  return v1<v2;
}
ll gcd(ll a,ll b){
 if(!a)return b;
 gcd(b%a,a);
}
int main()
{
 ios::sync_with_stdio(0);cin.tie(0);
 ll t,n,z;
    cin>>t;
    while(t--){
      cin>>n;
      ll ar[n+5],p=1;
      for(ll i=1;i<=n;i++){
         cin>>ar[i];
         vector<ll>temp;
         temp.push_back(ar[i]);
         temp.push_back(i);
         v.push_back(temp);
      }
      sort(v.begin(),v.end(),cmp);
      for(ll i=1;i<=n;i++){
        if(ar[i]==v[i-1][0])continue;
        ll x=gcd(ar[i],ar[v[i-1][1]]);
        if(x==v[0][0]){
           swap(ar[i],ar[v[i-1][1]]);
           ll z=bin_search(ar[i],i);
           swap(v[i-1][1],v[z-1][1]);
        }
        else{
          p=0;break;
        }
      }
      if(p)cout<<"YES"<<endl;
      else cout<<"NO"<<endl;
      v.clear();
    }
 return 0;
}


#include<bits/stdc++.h>
#define ll long long int
//#define INF 1000001
using namespace std;
//set<ll>st;
//vector<vector<ll> >v;
//string s;
//map<ll,ll>mp;
//map<ll,ll>::iterator it;
//ll MAX_N=1000000002;
/*ll bin_search(ll val,ll i){
   ll l=i-1,r=v.size()-1,mid;
   while(l<=r){
      mid=(l+r)/2;
      if(v[mid][0]==val)r=mid-1;
      else if(v[mid][0]<val)l=mid+1;
      else r=mid-1;
   }
}*/
/*bool cmp(vector<ll>v1,vector<ll>v2){
 return v1<v2;
}*/
/*ll gcd(ll a,ll b){
 if(!a)return b;
 return gcd(b%a,a);
}*/
int main()
{
 ios::sync_with_stdio(0);cin.tie(0);
 ll t,n;
    cin>>t;
    while(t--){
      cin>>n;
      ll cnt=0;
      vector<ll>ar(n+1),a(n+1);
      for(ll i=1;i<=n;i++){
         cin>>ar[i];
         a[i]=ar[i];
      }
      sort(a.begin()+1,a.end());
      for(ll i=1;i<=n;i++){
        if(ar[i]%a[1]&&a[i]!=ar[i])continue;
        cnt++;
      }
      if(cnt==n)cout<<"YES\n";
      else cout<<"NO\n";
    }
 return 0;
}


#include<bits/stdc++.h>
#define ll long long int
//#define INF 1000001
using namespace std;
//set<ll>st;
vector<vector<ll> >v;
//string s;
//map<ll,ll>mp;
//map<ll,ll>::iterator it;
//ll MAX_N=1000000002;
ll bin_search(ll val,ll i){
   ll l=i,r=v.size(),mid;
   while(l<=r){
      mid=(l+r)/2;
      if(v[mid][0]==val)return mid;
      else if(v[mid][0]<val)l=mid+1;
      else r=mid-1;
   }
}
bool cmp(vector<ll>v1,vector<ll>v2){
  return v1<v2;
}
ll gcd(ll a,ll b){
 if(!a)return b;
 return gcd(b%a,a);
}
int main()
{
 ios::sync_with_stdio(0);cin.tie(0);
 ll t,n,z;
    cin>>t;
    while(t--){
      cin>>n;
      ll ar[n+5],p=1;
      for(ll i=1;i<=n;i++){
         cin>>ar[i];
         vector<ll>temp;
         temp.push_back(ar[i]);
         temp.push_back(i);
         v.push_back(temp);
      }
      sort(v.begin(),v.end(),cmp);
      for(ll i=1;i<=n;i++){
        if(ar[i]==v[i-1][0])continue;
        ll x=gcd(ar[i],ar[v[i-1][1]]);
        if(x==v[0][0]){
           swap(ar[i],ar[v[i-1][1]]);
           ll z=bin_search(ar[i],i);
           swap(v[i-1][1],v[z-1][1]);
        }
        else{
          p=0;break;
        }
      }
      if(p)cout<<"YES"<<endl;
      else cout<<"NO"<<endl;
      v.clear();
    }
 return 0;
}


#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
	if(b==0) return a;
	return gcd(b, a%b);
}
bool ma(int arr[], int n, int m, priority_queue<int> pq) {
	if(n<=1) return true;
	if(arr[n-1]==pq.top()) {
		pq.pop();
		return ma(arr,n-1,m,pq);
	} else {
		if(gcd(pq.top(), arr[n-1])!=m) return false;
		else {
			pq.pop();
			return ma(arr,n-1,m,pq);
		}
	}

}
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int arr[n];
		for(int i=0; i<n; i++) cin>>arr[i];
		int m = INT_MAX;
		for(int i=0; i<n; i++) m = min(m,arr[i]);
		priority_queue<int> pq(arr,arr+n);
		if(ma(arr,n,m,pq)) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
//	cout<<gcd(1,5)<<" "<<gcd(15,45)<<" "<<gcd(51,27);
}


#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
	if(b==0) return a;
	return gcd(b, a%b);
}
bool ma(int arr[], int n, int m) {
	int temp[n];
	for(int i=0; i<n; i++) temp[i]=arr[i];
	sort(temp, temp+n);
	for(int i=n-1; i>=0; i--) {
		if(arr[i]==temp[i]) continue;
		else {
			if(gcd(arr[i],temp[i])!=m) return false;
		}
	}
	return true;
}
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int arr[n];
		for(int i=0; i<n; i++) cin>>arr[i];
		int m = INT_MAX;
		for(int i=0; i<n; i++) {
			m = min(m, arr[i]);
		}
		if(ma(arr,n,m)) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
	if(b==0) return a;
	return gcd(b, a%b);
}
//bool ma(int arr[], int n, priority_queue<int> pq, int m, unordered_map<int, vector<int>> mp, bool visited[]) {
//	if(n==1) return true;
//	if(pq.top()==arr[n-1]) {
//		pq.pop();
//		visited[n-1] = true;
//		return ma(arr,n-1,pq,m,mp,visited);
//	} else {
//		if(gcd(pq.top(),arr[n-1]) != m) return false;
//		else {
//			int idx;
//			for(int k=0; k<mp[pq.top()].size(); k++) {
//				if(!visited[mp[pq.top()][k]]) {
//					idx = mp[pq.top()][k];
//					visited[idx] = true;
//					break;
//				}
//			}
//			swap(arr[idx], arr[n-1]);
//			return ma(arr,n-1,pq,m,mp,visited);
//		}
//	}
//}
bool ma(int arr[], int n, int m) {
	int temp[n];
	for(int i=0; i<n; i++) temp[i]=arr[i];
	sort(temp, temp+n);
	for(int i=n-1; i>=0; i--) {
		if(arr[i-1]==temp[i-1]) continue;
		else {
			if(gcd(arr[i-1],temp[i-1])!=m) return false;
		}
	}
	return true;
}
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int arr[n];
		for(int i=0; i<n; i++) cin>>arr[i];

//		priority_queue<int> pq(arr, arr+n);
//		stack<int> s;
//		s.push(arr[0]);
//		for(int i=1; i<n; i++) {
//			if(arr[i] < s.top()) s.push(arr[i]);
//			else s.push(s.top());
//		}
//		unordered_map<int, vector<int>> mp;
//		bool visited[n] = {0};
		int m = INT_MAX;
		for(int i=0; i<n; i++) {
			m = min(m, arr[i]);
//			mp[arr[i]].push_back(i);
		}

//		if(ma(arr,n,pq,m,mp,visited)) cout<<"YES"<<endl;
		if(ma(arr,n,m)) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int arr[n];
		int m = INT_MAX;
		for(int i=0; i<n; i++) {
			cin>>arr[i];
			m = min(m, arr[i]);
		}
		vector<int> idx,div;
		for(int i=0; i<n; i++) {
			if(arr[i]%m==0) {
				idx.push_back(i);
				div.push_back(arr[i]);
			}
		}
//		cout<<"reached"<<endl;
		sort(div.begin(), div.end());
		for(int i=0; i<idx.size(); i++) {
			arr[idx[i]] = div[i];
		}
		bool sorted = true;
		for(int i=1; i<n; i++) {
			if(arr[i-1] > arr[i]) {
				sorted = false;
				break;
			}
		}
		if(sorted) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int arr[n];
		int m = INT_MAX;
		for(int i=0; i<n; i++) {
			cin>>arr[i];
			m = min(m, arr[i]);
		}
		priority_queue<int> pq(arr, arr+n);
		bool sorted = true;

		for(int i=n-1; i>=0; i--) {
			if(arr[i]==pq.top() || arr[i]%m==0) {
				pq.pop();
			} else {
				sorted = false;
				break;
			}
		}

		if(sorted) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
	if(b==0) return a;
	return gcd(b, a%b);
}
bool ma(int arr[], int n, int m) {
	int temp[n];
	for(int i=0; i<n; i++) temp[i]=arr[i];
	sort(temp, temp+n);
	for(int i=n-1; i>=0; i--) {
		if(arr[i]==temp[i]) continue;
		else {
			if(gcd(arr[i-1],temp[i-1])!=m) return false;
		}
	}
	return true;
}
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int arr[n];
		for(int i=0; i<n; i++) cin>>arr[i];
		int m = INT_MAX;
		for(int i=0; i<n; i++) {
			m = min(m, arr[i]);
		}
		if(ma(arr,n,m)) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const ll N = 9e5 + 9;
priority_queue<ll> pq;
ll a[N];
ll b[N];
ll head[N];
bool vis[N];
void solve() {
    int n;
    ll mi = 0xfffffff;
    memset(vis, 0, sizeof vis);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mi = min(mi, a[i]);
        b[i] = a[i];
    }
    sort(b + 1, b + 1 + n);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (a[i] != b[i]) {
            head[++cnt] = i;
        }
    }
    int sum = 0;
    for (int i = 1; i <= cnt; i++) {
        cout << a[head[i]] << " ";
        if (a[head[i]] % mi == 0) {
            sum++;
        }
    }
    cout << endl;
    if (sum == cnt) {
        cout << "YES\n";
    } else
        cout << "NO\n";
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}


#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const ll N = 9e5 + 9;
priority_queue<ll> pq;
ll a[N];
ll b[N];
ll head[N];
bool vis[N];
void solve() {
    int n;
    ll mi = INT_MAX;
    memset(vis, 0, sizeof vis);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mi = min(mi, a[i]);
        b[i] = a[i];
    }
    sort(b + 1, b + 1 + n);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (a[i] != b[i]) {
            head[++cnt] = i;
        }
    }
    int sum = 0;
    for (int i = 1; i <= cnt; i++) {
       // cout << a[head[i]] << " ";
        if (a[head[i]] % mi == 0) {
            sum++;
        }
    }
    //cout << endl;
    if (sum == cnt) {
        cout << "YES\n";
    } else
        cout << "NO\n";
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}


#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const ll N = 9e5 + 9;
priority_queue<ll> pq;
ll a[N];
ll b[N];
ll head[N];
bool vis[N];
void solve() {
    int n;
    ll mi = 0xfffffff;
    memset(vis, 0, sizeof vis);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mi = min(mi, a[i]);
        b[i] = a[i];
    }
    sort(b + 1, b + 1 + n);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (a[i] != b[i]) {
            head[++cnt] = i;
        }
    }
    int sum = 0;
    for (int i = 1; i <= cnt; i++) {
       // cout << a[head[i]] << " ";
        if (a[head[i]] % mi == 0) {
            sum++;
        }
    }
    //cout << endl;
    if (sum == cnt) {
        cout << "YES\n";
    } else
        cout << "NO\n";
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}


#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const ll N = 1e5 + 9;
priority_queue<ll> pq;
ll a[N];
ll b[N];
ll head[N];
bool vis[N];
void solve() {
    int n;
    ll mi = 0xfffffff;
    memset(vis, 0, sizeof vis);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mi = min(mi, a[i]);
        b[i] = a[i];
    }
    sort(b + 1, b + 1 + n);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (a[i] != b[i]) {
            head[++cnt] = i;
        }
    }
    int sum = 0;
    for (int i = 1; i <= cnt; i++) {
        if (a[head[i]] % mi == 0) {
            sum++;
        }
    }
    if (sum == cnt) {
        cout << "YES\n";
    } else
        cout << "NO\n";
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}


#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const ll N = 1e5 + 9;
priority_queue<ll>pq;
ll a[N];
ll b[N];
ll head[N];
bool vis[N];
void solve() {
    int n;
    ll mi = 0xfffffff;
    memset(vis, 0, sizeof vis);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mi = min(mi, a[i]);
        b[i] = a[i];
    }
    sort(b + 1, b + 1 + n);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (a[i] != b[i]) {
            vis[i] = 1;
            head[++cnt] = i;
        }
    }
    if (!cnt) {
        cout << "YES\n";
        return;
    }
    int sum = 0;
    for (int i = 1; i <= cnt; i++) {

        if (a[head[i]] % mi == 0) {
            sum++;
        }
    }
    // for (int i = 1; i <= n; i++) {
    //     int sum = 0;
    //     for (int j = 1; j <= cnt; j++) {
    //         if (a[head[j]] == a[i])continue;
    //         if (gcd(a[i], a[head[j]]) == mi) {
    //             sum++;
    //         }
    //     }
    //     if (sum == cnt-1) {
    //         cout << "YES\n";
    //         return;
    //     }
    //     if (sum == 0 && vis[i]) {
    //         cout << "NO\n";
    //         return;
    //     }
    // }
    if (sum == cnt) {
        cout << "YES\n";
    }else
    cout << "NO\n";
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}


#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const ll N = 1e5 + 9;
priority_queue<ll>pq;
ll a[N];
ll b[N];
ll head[N];
bool vis[N];
void solve() {
    int n;
    ll mi = 0xfffffff;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mi = min(mi, a[i]);
        b[i] = a[i];
    }
    sort(b + 1, b + 1 + n);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (a[i] != b[i]) {
            vis[i] = 1;
            head[++cnt] = i;
        }
    }
    if (!cnt) {
        cout << "YES\n";
        return;
    }

    // for (int i = 1; i <= cnt; i++) {
    //     cout << head[i] << endl;
    // }
    for (int i = 1; i <= cnt; i++) {
        int sum = 0;
        for (int j = 1; j <= cnt; j++) {
            if (j == i)continue;
           // cout << mi << "?";

            if (gcd(a[head[i]], a[head[j]]) == mi) {
                sum++;
                //cout << "------\n";
            }
        }
        if (sum == cnt-1) {
            cout << "YES\n";
            return;
        }
    }
    cout << "NO\n";
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}


#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const ll N = 1e5 + 9;
priority_queue<ll>pq;
ll a[N];
ll b[N];
ll head[N];
bool vis[N];
void solve() {
    int n;
    ll mi = 0xfffffff;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mi = min(mi, a[i]);
        b[i] = a[i];
    }
    sort(b + 1, b + 1 + n);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (a[i] != b[i]) {
            vis[i] = 1;
            head[++cnt] = i;
        }
    }
    if (!cnt) {
        cout << "YES\n";
        return;
    }
    for (int i = 1; i <= cnt; i++) {
        int sum = 0;
        for (int j = 1; j <= cnt; j++) {
            if (j == i)continue;
            if (gcd(a[head[i]], a[head[j]]) == mi) {
                sum++;
            }
        }
        if (sum == cnt-1) {
            cout << "YES\n";
            return;
        }
        if (sum == 0) {
            cout << "NO\n";
            return;
        }
    }
    cout << "NO\n";
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}


#include <iostream>
#include <algorithm>
#define ll          long long
using namespace std;

int a[100005], x[100005], y[100005];

int main(){
	int t;
	cin >> t;
	while(t){
		t--;

		int n;
		cin >> n;
		int mn=INT_MAX;
		bool sorted;
		int prev;
		for(int i =0;i<n;i++){
			int temp;
			cin >> temp;
			a[i]=temp;
			mn = min(temp,mn);
			prev=temp;
		}
		int k = 0;
		for(int i=0;i<n;i++){
			if(a[i]%mn==0){
				x[k]=i;
				y[k]=a[i];
			k++;
			}
		}
		sort(y,y+k);
		for(int i =0;i<k;i++){
			a[x[i]]=y[i];

			}
		sorted = true;
		for(int i =1;i<n;i++){
			if(a[i]<a[i-1]){
				sorted = false;
			}
		}
		if(sorted){
			cout << "YES\n" ;

		}
		else{
			cout << "NO\n";
		}

	}
}


#include <iostream>
#include <algorithm>
#define ll          long long
using namespace std;

int a[100005], x[100005], y[100005];

int main(){
	int t;
	cin >> t;
	while(t){
		t--;

		int n;
		cin >> n;
		int mn=INT_MAX;
		bool sorted;
		int prev;
		for(int i =0;i<n;i++){
			int temp;
			cin >> temp;
			a[i]=temp;
			mn = min(temp,mn);
			if(prev && prev>temp){
				sorted = false;
			}
			prev=temp;
		}
		int k = 0;
		for(int i=0;i<n;i++){
			if(a[i]%mn==0){
				x[a[i]]=i;
				y[k]=a[i];
			k++;
			}
		}
		sort(y,y+k);
		for(int i =0;i<k;i++){
			a[x[y[k]]]=y[k];
		}
		sorted = true;

		for(int i =1;i<k;i++){
			if(a[i]<a[i-1]){
				sorted = false;
			}
		}
		if(sorted){
			cout << "YES\n" ;

		}
		else{
			cout << "NO\n";
		}

	}
}


#include <iostream>
#include <algorithm>
#define ll          long long
using namespace std;

int a[100005], x[100005], y[100005];

int main(){
	int t;
	cin >> t;
	while(t){
		t--;

		int n;
		cin >> n;
		int mn=INT_MAX;
		bool sorted;
		int prev;
		for(int i =0;i<n;i++){
			int temp;
			cin >> temp;
			a[i]=temp;
			mn = min(temp,mn);
			if(prev && prev>temp){
				sorted = false;
			}
			prev=temp;
		}
		if(sorted){
			cout << "YES";
		}
		int k = 0;
		for(int i=0;i<n;i++){
			if(a[i]%mn==0){
				x[a[i]]=i;
				y[k]=a[i];
			k++;
			}
		}
		sort(y,y+k);
		for(int i =0;i<k;i++){
			a[x[y[k]]]=y[k];
		}
		sorted = true;

		for(int i =1;i<k;i++){
			if(a[i]<a[i-1]){
				sorted = false;
			}
		}
		if(sorted){
			cout << "YES\n" ;

		}
		else{
			cout << "NO\n";
		}

	}
}


#include <iostream>
#include <algorithm>
#define ll          long long
using namespace std;

int x[1 << 9];
int y[1 << 9];
int a[1<<9];

int main(){
	int t;
	cin >> t;
	while(t){
		t--;

		int n;
		cin >> n;
		int mn=INT_MAX;
		bool sorted;
		int prev;
		for(int i =0;i<n;i++){
			int temp;
			cin >> temp;
			a[i]=temp;
			mn = min(temp,mn);
			if(prev && prev>temp){
				sorted = false;
			}
			prev=temp;
		}
		if(sorted){
			cout << "YES";
		}
		int k = 0;
		for(int i=0;i<n;i++){
			if(a[i]%mn==0){
				x[a[i]]=i;
				y[k]=a[i];
			k++;
			}
		}
		sort(y,y+k);
		for(int i =0;i<k;i++){
			a[x[y[k]]]=y[k];
		}
		sorted = true;

		for(int i =1;i<k;i++){
			if(a[i]<a[i-1]){
				sorted = false;
			}
		}
		if(sorted){
			cout << "YES\n" ;
		}
		else{
			cout << "NO\n";
		}

	}
}


#include<bits/stdc++.h>
using namespace std;
int T,n,las,a[110000],minn;
int flag;
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>n;
		minn=0x3f3f3f3f;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]),minn=min(minn,a[i]);
			if(a[i]<a[i-1]) flag=1;
		}
		if(!flag)
		{
			cout<<"YES"<<endl;
			continue;
		}
		flag=0;
		for(int i=2;i<=sqrt(minn)+1&&i!=minn;i++)
		{
			if(minn%i==0) flag=1;
		}
		if(flag==1)
		{
			cout<<"NO"<<endl;
			continue;
		}
		las=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]%minn)
			{
				if(a[i]<las)
				{
					cout<<"NO"<<endl;
					break;
				}
				las=a[i];
			}
			if(i==n) cout<<"YES"<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int T,n,las,a[110000],minn,b[110000],cnt[110000],id,tot;
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>n;
		minn=0x3f3f3f3f;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]),minn=min(minn,a[i]),b[i]=a[i];
		}
		sort(b+1,b+n+1);
		for(int i=1;i<=n;i++)
		{
			if(b[i]!=b[i-1]) cnt[b[i]]=i;
		}
		las=0,tot=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]==a[i-1]) tot++;
			else tot=1;
			if(a[i]%minn)
			{
				if(a[i]<las||i!=cnt[a[i]]+tot-1)
				{
					cout<<"NO"<<endl;
					break;
				}
				las=a[i];
			}
			if(i==n) cout<<"YES"<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int T,n,las,a[110000],minn,cnt[110000],tot;
struct node
{
	int id;
	int x;
}b[110000];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>n;
		minn=0x3f3f3f3f;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]),minn=min(minn,a[i]),b[i].x =a[i],b[i].id=i;
		}
		sort(b+1,b+n+1,cmp);
		for(int i=1;i<=n;i++)
		{
			if(b[i].x!=b[i-1].x) cnt[b[i].id]=i;
			else cnt[b[i].id]=cnt[b[i-1].id];
		}
		las=0,tot=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]==a[i-1]) tot++;
			else tot=1;
			if(a[i]%minn)
			{
				if(a[i]<las||i!=cnt[i]+tot-1)
				{
					cout<<"NO"<<endl;
					break;
				}
				las=a[i];
			}
			if(i==n) cout<<"YES"<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int T,n,las,a[110000],minn;
int flag;
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>n;
		minn=0x3f3f3f3f;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]),minn=min(minn,a[i]);
			if(a[i]<a[i-1]) flag=1;
		}
		if(!flag)
		{
			cout<<"YES"<<endl;
			continue;
		}
		flag=0;
		for(int i=1;i<=sqrt(minn)+1;i++)
		{
			if(minn%i==0) flag=1;
		}
		if(flag==1)
		{
			cout<<"NO"<<endl;
			continue;
		}
		las=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]%minn)
			{
				if(a[i]<las)
				{
					cout<<"NO"<<endl;
					break;
				}
				las=a[i];
			}
			if(i==n) cout<<"YES"<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int T,n,las,a[110000],minn,cnt[110000],tot;
struct node
{
	int id;
	int x;
}b[110000];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>n;
		minn=0x3f3f3f3f;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]),minn=min(minn,a[i]),b[i].x =a[i],b[i].id=i;
		}
		sort(b+1,b+n+1,cmp);
		for(int i=1;i<=n;i++)
		{
			if(b[i].x!=b[i-1].x) cnt[b[i].id]=i;
		}
		las=0,tot=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]==a[i-1]) tot++;
			else tot=1;
			if(a[i]%minn)
			{
				if(a[i]<las||i!=cnt[i]+tot-1)
				{
					cout<<"NO"<<endl;
					break;
				}
				las=a[i];
			}
			if(i==n) cout<<"YES"<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int T,n,las,a[110000],minn,b[110000],cnt[110000],id;
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>n;
		minn=0x3f3f3f3f;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]),minn=min(minn,a[i]),b[i]=a[i];
		}
		sort(b+1,b+n+1);
		for(int i=1;i<=n;i++)
		{
			if(b[i]!=b[i-1]) cnt[b[i]]=i;
		}
		las=0,id=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]%minn)
			{
				if(a[i]<las||cnt[a[i]]-cnt[a[id]]!=i-id)
				{
					cout<<"NO"<<endl;
					break;
				}
				las=a[i],id=i;
			}
			if(i==n) cout<<"YES"<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int T,n,las,a[110000],minn;
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>n;
		minn=0x3f3f3f3f;
		for(int i=1;i<=n;i++) scanf("%d",&a[i]),minn=min(minn,a[i]);
		las=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]%minn)
			{
				if(a[i]<las)
				{
					cout<<"NO"<<endl;
					break;
				}
				las=a[i];
			}
			if(i==n) cout<<"YES"<<endl;
		}
	}
}


/* =============================================================================
#
 * @Description:
 * @Author: 梁云钦
 * @Date: 2020-08-26 16:45:21
 * @LastEditTime: 2020-08-26 17:11:12
 * @LastEditors: 梁云钦
#
============================================================================= */
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
const int N=1e5+5;
int num[N];
int fa[N];
pair<int,int> aim[N];

int gcd(int a,int b){
    if(b>a) swap(a,b);
    return b==0?a:gcd(b,a%b);
}
int find(int t){
    return fa[t]==t?t:find(fa[t]);
}
void father(int dad,int son){
    fa[find(son)]=find(dad);
}

int main(){
    int t;
    cin>>t;
    while(t--){
        map<int,int> mp;
        set<int> st;
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>num[i];
            st.insert(num[i]);
            fa[i]=i;
            aim[i]=pair<int,int>(num[i],i);
        }
        sort(aim,aim+n);
        for(int i=0;i<n;i++)
            mp.insert(pair<int,int>(aim[i].second,i));
        int flag=1;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if(find(i)==find(j)) continue;
                if(st.find(gcd(num[i],num[j]))!=st.end()){
                    father(i,j);
                }
            }
        }
        for(int i=0;i<n;i++){
            if(find(i)!=find(mp[i])) {
                flag=0;
                break;
            }
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


/* =============================================================================
#
 * @Description:
 * @Author: 梁云钦
 * @Date: 2020-08-26 16:45:21
 * @LastEditTime: 2020-08-26 17:11:12
 * @LastEditors: 梁云钦
#
============================================================================= */
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
const int N=1e5+5;
int num[N];
int fa[N];
pair<int,int> aim[N];

int gcd(int a,int b){
    if(b>a) swap(a,b);
    return b==0?a:gcd(b,a%b);
}

void father(int dad,int son){
    fa[son]=fa[dad];
}

int find(int t){
    return fa[t]==t?t:find(fa[t]);
}

int main(){
    int t;
    cin>>t;
    while(t--){
        map<int,int> mp;
        set<int> st;
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>num[i];
            st.insert(num[i]);
            fa[i]=i;
            aim[i]=pair<int,int>(num[i],i);
        }
        sort(aim,aim+n);
        for(int i=0;i<n;i++)
            mp.insert(pair<int,int>(aim[i].second,i));
        int flag=1;
        for(int i=0;i<n;i++){
            if(i==mp[i]) continue;
            if(find(i)==find(mp[i])) continue;
            for(int j=0;j<n;j++){
                if(i==j) continue;
                if(find(i)==find(j)) continue;
                if(st.find(gcd(num[i],num[j]))!=st.end()){
                    father(i,j);
                }
            }
            if(find(i)==find(mp[i])) continue;
            flag=0;
            break;
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


/* =============================================================================
#
 * @Description:
 * @Author: 梁云钦
 * @Date: 2020-08-26 16:45:21
 * @LastEditTime: 2020-08-26 17:11:12
 * @LastEditors: 梁云钦
#
============================================================================= */
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
const int N=1e5+5;
int num[N];
int fa[N];
pair<int,int> aim[N];

int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}

void father(int dad,int son){
    fa[son]=fa[dad];
}

int find(int t){
    return fa[t]==t?t:find(fa[t]);
}

int main(){
    int t;
    cin>>t;
    while(t--){
        map<int,int> mp;
        set<int> st;
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>num[i];
            st.insert(num[i]);
            fa[i]=i;
            aim[i]=pair<int,int>(num[i],i);
        }
        sort(aim,aim+n);
        for(int i=0;i<n;i++)
            mp.insert(pair<int,int>(aim[i].second,i));
        int flag=1;
        for(int i=0;i<n;i++){
            if(i==mp[i]) continue;
            if(find(i)==find(mp[i])) continue;
            for(int j=0;j<n;j++){
                if(i==j) continue;
                if(find(i)==find(j)) continue;
                if(st.find(gcd(num[i],num[j]))!=st.end()){
                    father(i,j);
                }
            }
            if(find(i)==find(mp[i])) continue;
            flag=0;
            break;
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


/* =============================================================================
#
 * @Description:
 * @Author: 梁云钦
 * @Date: 2020-08-26 16:45:21
 * @LastEditTime: 2020-08-26 17:11:12
 * @LastEditors: 梁云钦
#
============================================================================= */
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
const int N=1e5+5;
int num[N];
int fa[N];
pair<int,int> aim[N];

int gcd(int a,int b){
    if(b>a) swap(a,b);
    return b==0?a:gcd(b,a%b);
}

void father(int dad,int son){
    fa[fa[son]]=fa[dad];
}

int find(int t){
    return fa[t]==t?t:find(fa[t]);
}

int main(){
    int t;
    cin>>t;
    while(t--){
        map<int,int> mp;
        set<int> st;
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>num[i];
            st.insert(num[i]);
            fa[i]=i;
            aim[i]=pair<int,int>(num[i],i);
        }
        sort(aim,aim+n);
        for(int i=0;i<n;i++)
            mp.insert(pair<int,int>(aim[i].second,i));
        int flag=1;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if(find(i)==find(j)) continue;
                if(st.find(gcd(num[i],num[j]))!=st.end()){
                    father(i,j);
                }
            }
        }
        for(int i=0;i<n;i++){
            if(find(i)!=find(mp[i])) {
                flag=0;
                break;
            }
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


/* =============================================================================
#
 * @Description:
 * @Author: 梁云钦
 * @Date: 2020-08-26 16:45:21
 * @LastEditTime: 2020-08-26 17:11:12
 * @LastEditors: 梁云钦
#
============================================================================= */
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
const int N=1e5+5;
int num[N];
int fa[N];
pair<int,int> aim[N];

int gcd(int a,int b){
    if(b>a) swap(a,b);
    return b==0?a:gcd(b,a%b);
}
int find(int t){
    return fa[t]==t?t:find(fa[t]);
}
void father(int dad,int son){
    fa[find(son)]=fa[dad];
}

int main(){
    int t;
    cin>>t;
    while(t--){
        map<int,int> mp;
        set<int> st;
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>num[i];
            st.insert(num[i]);
            fa[i]=i;
            aim[i]=pair<int,int>(num[i],i);
        }
        sort(aim,aim+n);
        for(int i=0;i<n;i++)
            mp.insert(pair<int,int>(aim[i].second,i));
        int flag=1;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if(find(i)==find(j)) continue;
                if(st.find(gcd(num[i],num[j]))!=st.end()){
                    father(i,j);
                }
            }
        }
        for(int i=0;i<n;i++){
            if(find(i)!=find(mp[i])) {
                flag=0;
                break;
            }
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


/* =============================================================================
#
 * @Description:
 * @Author: 梁云钦
 * @Date: 2020-08-26 16:45:21
 * @LastEditTime: 2020-08-26 17:11:12
 * @LastEditors: 梁云钦
#
============================================================================= */
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
const int N=1e5+5;
int num[N];
int fa[N];
pair<int,int> aim[N];

int gcd(int a,int b){
    if(b>a) swap(a,b);
    return b==0?a:gcd(b,a%b);
}

void father(int dad,int son){
    fa[find(son)]=fa[dad];
}

int find(int t){
    return fa[t]==t?t:find(fa[t]);
}

int main(){
    int t;
    cin>>t;
    while(t--){
        map<int,int> mp;
        set<int> st;
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>num[i];
            st.insert(num[i]);
            fa[i]=i;
            aim[i]=pair<int,int>(num[i],i);
        }
        sort(aim,aim+n);
        for(int i=0;i<n;i++)
            mp.insert(pair<int,int>(aim[i].second,i));
        int flag=1;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if(find(i)==find(j)) continue;
                if(st.find(gcd(num[i],num[j]))!=st.end()){
                    father(i,j);
                }
            }
        }
        for(int i=0;i<n;i++){
            if(find(i)!=find(mp[i])) {
                flag=0;
                break;
            }
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define L long
#define endl "\n"
#define mset(x, val) memset(x, val, sizeof(x))
#define For(i, init, n) for (ll int i = init; i < n; i++)
#define ___ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const double PI = acos(-1), E = exp(1.0), EPS = DBL_EPSILON;
const ll int MOD = (ll int)1e9 + 7, N = (int)1e5;
int a[N+1];

ll int GCD(ll int a, ll int b)
{
    a = abs(a);
    b = abs(b);
    ll int temp;
    while (b != 0)
    {
        temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int BS(int idx)
{
    int l = 0, r = idx - 1, res = -1;
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (a[mid] <= a[idx])
        {
            res = mid;
            l = mid + 1;
        }
        else
        {
            r = mid - 1;
        }
    }
    return res;
}

int main()
{
#ifdef MY_PREPROCESSOR
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ___

    int T;
    // scanf("%d", &T);
    cin >> T;
    for (int tc = 1; tc <= T; tc++)
    {
        int n, m = (int)2e9;
        cin >> n;
        vector<int> v;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            m = min(m, a[i]);
            if (i > 0 && a[i] < a[i-1])
            {
                v.push_back(i);
            }
        }
        bool flag = true;
        for (int i = v.size() - 1; i >= 0; i--)
        {
            int j = v[i], k = BS(j) + 1;
            int g = GCD(a[j], a[k]);
            if (g != m)
            {
                flag = false;
                break;
            }
            else
            {
                swap(a[i], a[j]);
            }
        }
        if (flag)
            cout<< "YES\n";
        else
            cout<< "NO\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define L long
#define endl "\n"
#define mset(x, val) memset(x, val, sizeof(x))
#define For(i, init, n) for (ll int i = init; i < n; i++)
#define ___ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const double PI = acos(-1), E = exp(1.0), EPS = DBL_EPSILON;
const ll int MOD = (ll int)1e9 + 7, N = (int)1e5;
int a[N+1];

ll int GCD(ll int a, ll int b)
{
    a = abs(a);
    b = abs(b);
    ll int temp;
    while (b != 0)
    {
        temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int BS(int idx)
{
    int l = 0, r = idx - 1, res = -1;
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (a[mid] > a[idx])
        {
            res = mid;
            r = mid - 1;
        }
        else
        {
            l = mid + 1;
        }
    }
    return res;
}

int main()
{
#ifdef MY_PREPROCESSOR
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ___

    int T;
    // scanf("%d", &T);
    cin >> T;
    for (int tc = 1; tc <= T; tc++)
    {
        int n, m = (int)2e9, p = 0;
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            m = min(m, a[i]);
        }
        bool flag = true;
        for (int i = n - 1; i >= 0; i--)
        {
            int j = BS(i);
            if (j != -1)
            {
                if (GCD(a[i], a[j]) == m)
                {
                    swap(a[i], a[j]);
                    i++;
                }
                else
                {
                    flag = false;
                    break;
                }
            }
        }

        if (flag)
            cout<< "YES\n";
        else
            cout<< "NO\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define L long
#define endl "\n"
#define mset(x, val) memset(x, val, sizeof(x))
#define For(i, init, n) for (ll int i = init; i < n; i++)
#define ___ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const double PI = acos(-1), E = exp(1.0), EPS = DBL_EPSILON;
const ll int MOD = (ll int)1e9 + 7, N = (int)1e5;
int a[N+1], b[N+1];

int main()
{
#ifdef MY_PREPROCESSOR
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ___

    int T;
    // scanf("%d", &T);
    cin >> T;
    for (int tc = 1; tc <= T; tc++)
    {
        int n, m = (int)2e9;
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            b[i] = a[i];
            m = min(m, a[i]);
        }
        sort(b, b + n);
        bool p = true;
        for (int i = 0; i < n; i++)
        {
            if (a[i] != b[i] && a[i] % m)
            {
                p = false;
                break;
            }
        }
        if (p)
            cout<< "YES\n";
        else
            cout<< "NO\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define L long
#define endl "\n"
#define mset(x, val) memset(x, val, sizeof(x))
#define For(i, init, n) for (ll int i = init; i < n; i++)
#define ___ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const double PI = acos(-1), E = exp(1.0), EPS = DBL_EPSILON;
const ll int MOD = (ll int)1e9 + 7, N = (int)1e5;
int a[N+1];

ll int GCD(ll int a, ll int b)
{
    a = abs(a);
    b = abs(b);
    ll int temp;
    while (b != 0)
    {
        temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int BS(int idx)
{
    int l = 0, r = idx - 1, res = -1;
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (a[mid] <= a[idx])
        {
            res = mid;
            l = mid + 1;
        }
        else
        {
            r = mid - 1;
        }
    }
    return res;
}

int main()
{
#ifdef MY_PREPROCESSOR
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ___

    int T;
    // scanf("%d", &T);
    cin >> T;
    for (int tc = 1; tc <= T; tc++)
    {
        int n, m = (int)2e9;
        cin >> n;
        vector<int> v;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            m = min(m, a[i]);
            if (i > 0 && a[i] < a[i-1])
            {
                v.push_back(i);
            }
        }
        bool flag = true;
        for (int i = v.size() - 1; i >= 0; i--)
        {
            int j = v[i], k = BS(j) + 1;
            int g = GCD(a[j], a[k]);
            if (j != k && g != m)
            {
                flag = false;
                break;
            }
            else
            {
                swap(a[j], a[k]);
            }
        }
        m = (int)2e9;
        vector<int> u;
        for (int i = 0; i < n; i++)
        {
            m = min(m, a[i]);
            if (i > 0 && a[i] < a[i-1])
            {
                u.push_back(i);
            }
        }
        flag = true;
        for (int i = u.size() - 1; i >= 0; i--)
        {
            int j = u[i], k = BS(j) + 1;
            int g = GCD(a[j], a[k]);
            if (j != k && g != m)
            {
                flag = false;
                break;
            }
            else
            {
                swap(a[j], a[k]);
            }
        }

        if (flag)
            cout<< "YES\n";
        else
            cout<< "NO\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define L long
#define endl "\n"
#define mset(x, val) memset(x, val, sizeof(x))
#define For(i, init, n) for (ll int i = init; i < n; i++)
#define ___ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const double PI = acos(-1), E = exp(1.0), EPS = DBL_EPSILON;
const ll int MOD = (ll int)1e9 + 7, N = (int)1e5;
int a[N+1];

ll int GCD(ll int a, ll int b)
{
    a = abs(a);
    b = abs(b);
    ll int temp;
    while (b != 0)
    {
        temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int BS(int idx)
{
    int l = 0, r = idx - 1, res = -1;
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (a[mid] <= a[idx])
        {
            res = mid;
            l = mid + 1;
        }
        else
        {
            r = mid - 1;
        }
    }
    return res;
}

int main()
{
#ifdef MY_PREPROCESSOR
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ___

    int T;
    // scanf("%d", &T);
    cin >> T;
    for (int tc = 1; tc <= T; tc++)
    {
        int n, m = (int)2e9, p = 0;
        cin >> n;
        vector<int> v;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            m = min(m, a[i]);
            if (i > 0)
            {
                if (a[i] < a[i-1])
                {
                    v.push_back(i);
                }
                else if (a[i] < p)
                {
                    v.push_back(i);
                }
                else
                {
                    p = a[i];
                }
            }
            else
                p = a[i];
        }
        bool flag = true;
        for (int i = v.size() - 1; i >= 0; i--)
        {
            int j = v[i], k = BS(j) + 1;
            int g = GCD(a[j], a[k]);
            if (j != k && g != m)
            {
                flag = false;
                break;
            }
            else
            {
                swap(a[j], a[k]);
            }
        }

        if (flag)
            cout<< "YES\n";
        else
            cout<< "NO\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define L long
#define endl "\n"
#define mset(x, val) memset(x, val, sizeof(x))
#define For(i, init, n) for (ll int i = init; i < n; i++)
#define ___ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const double PI = acos(-1), E = exp(1.0), EPS = DBL_EPSILON;
const ll int MOD = (ll int)1e9 + 7, N = (int)1e5;
int a[N+1], b[N+1];

ll int GCD(ll int a, ll int b)
{
    a = abs(a);
    b = abs(b);
    ll int temp;
    while (b != 0)
    {
        temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int main()
{
#ifdef MY_PREPROCESSOR
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ___

    int T;
    // scanf("%d", &T);
    cin >> T;
    for (int tc = 1; tc <= T; tc++)
    {
        int n, m = (int)2e9;
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            b[i] = a[i];
            m = min(m, a[i]);
        }
        sort(b, b + n);
        bool p = true;
        for (int i = 0; i < n; i++)
        {
            if (a[i] != b[i])
            {
                if (a[i] % m != 0)
                {
                    p = false;
                    break;
                }
            }
        }
        if (p)
            cout<< "YES\n";
        else
            cout<< "NO\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define L long
#define endl "\n"
#define mset(x, val) memset(x, val, sizeof(x))
#define For(i, init, n) for (ll int i = init; i < n; i++)
#define ___ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const double PI = acos(-1), E = exp(1.0), EPS = DBL_EPSILON;
const ll int MOD = (ll int)1e9 + 7, N = (int)1e5;
int a[N+1];

ll int GCD(ll int a, ll int b)
{
    a = abs(a);
    b = abs(b);
    ll int temp;
    while (b != 0)
    {
        temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int BS(int idx)
{
    int l = 0, r = idx - 1, res = -1;
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (a[mid] <= a[idx])
        {
            res = mid;
            l = mid + 1;
        }
        else
        {
            r = mid - 1;
        }
    }
    return res;
}

int main()
{
#ifdef MY_PREPROCESSOR
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ___

    int T;
    // scanf("%d", &T);
    cin >> T;
    for (int tc = 1; tc <= T; tc++)
    {
        int n, m = (int)2e9;
        cin >> n;
        vector<int> v;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            m = min(m, a[i]);
            if (i > 0 && a[i] < a[i-1])
            {
                v.push_back(i);
            }
        }
        bool flag = true;
        for (int i = v.size() - 1; i >= 0; i--)
        {
            int j = v[i], k = BS(j) + 1;
            int g = GCD(a[j], a[k]);
            if (j != k && g != m)
            {
                flag = false;
                break;
            }
            else
            {
                swap(a[j], a[k]);
            }
        }
        if (flag)
            cout<< "YES\n";
        else
            cout<< "NO\n";
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll int gcd(ll int a,ll int b)
{if(b==0)
  return a;
  else
    return gcd(b,a%b);

}
bool findMinSwap(vector<ll int> &arr ,ll int n)
{ ll int mini=arr[0];
  for(auto i: arr)
    mini=min(i,mini);
      //cout<<mini<<" mini\n";

    vector<pair<ll int,ll int>> vec(n);

    for(ll int i=0;i<n;i++)
    {
        vec[i].first=arr[i];
        vec[i].second=i;
    }

    sort(vec.begin(),vec.end());

    ll int ans=0,c=0,j;
    bool mans=true;
    for(int i=0;i<n;i++)
    {

        if(vec[i].second==i)
            continue;
        else
        {
       ll   int f=vec[i].first;
        ll  int e=vec[vec[i].second].first;
     ll  int d=gcd(vec[i].first,vec[vec[i].second].first);

            swap(vec[i].first,vec[vec[i].second].first);
            swap(vec[i].second,vec[vec[i].second].second);
      if(d!=mini&&e!=f)
        return false;

        }

        if(i!=vec[i].second)
            --i;

        ans++;
    }

    return true;
}
int main()
{
    #ifndef ONLINE_JUDGE
     freopen("inh.txt",  "r",  stdin);
   freopen("ouhh.txt", "w", stdout);
    #endif
 int t;
 cin>>t;
 while(t--)
 {

ll int n;
cin>>n;
vector<ll int> v;
for(int i=0;i<n;i++)
{ll int d;
  cin>>d;
  v.push_back(d);

}

bool c=findMinSwap(v,n);
if(c)
  cout<<"YES\n";
else
  cout<<"NO\n";

}

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll int gcd(ll int a,ll int b)
{if(b==0)
  return a;
  else
    return gcd(b,a%b);

}
bool findMinSwap(vector<ll int> &arr ,ll int n)
{ ll int mini=arr[0];
  for(auto i: arr)
    mini=min(i,mini);
      //cout<<mini<<" mini\n";

    vector<pair<ll int,ll int>> vec(n);

    for(ll int i=0;i<n;i++)
    {
        vec[i].first=arr[i];
        vec[i].second=i;
    }

    sort(vec.begin(),vec.end());

    ll int ans=0,c=0,j;
    bool mans=true;
    for(int i=0;i<n;i++)
    {

        if(vec[i].second==i)
            continue;
        else
        {
       ll   int f=vec[i].first;
        ll  int e=vec[vec[i].second].first;
     ll  int d=gcd(vec[i].first,vec[vec[i].second].first);
      if(d!=mini&&e!=f)
        return false;

            swap(vec[i].first,vec[vec[i].second].first);
            swap(vec[i].second,vec[vec[i].second].second);
        }

        if(i!=vec[i].second)
            --i;

        ans++;
    }

    return true;
}
int main()
{
    #ifndef ONLINE_JUDGE
     freopen("inh.txt",  "r",  stdin);
   freopen("ouhh.txt", "w", stdout);
    #endif
 int t;
 cin>>t;
 while(t--)
 {

ll int n;
cin>>n;
vector<ll int> v;
for(int i=0;i<n;i++)
{ll int d;
  cin>>d;
  v.push_back(d);

}

bool c=findMinSwap(v,n);
if(c)
  cout<<"YES\n";
else
  cout<<"NO\n";

}

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
 ll int gcd(ll int a,ll int b)
 {
if(b==0)
  return a;
else
  return gcd(b,a%b);

 }
ll int findgcd(vector<ll int> &v)
{
if(v.size()==1)
  return v[0];
ll int e=gcd(v[0],v[1]);
for(ll int i=2;i<v.size();i++)
{
  e=gcd(e,v[i]);
}
return e;

}

int main()
{
    #ifndef ONLINE_JUDGE
     freopen("inh.txt",  "r",  stdin);
   freopen("ouhh.txt", "w", stdout);
    #endif
 int t;
 cin>>t;
 while(t--)
 {

ll int n;
cin>>n;
vector<ll int> v1;
vector<ll int> v2;
for(int i=0;i<n;i++)
{ll int d;
  cin>>d;
  v1.push_back(d);
v2.push_back(d);

}
sort(v2.begin(),v2.end());
ll int mini=v1[0];
for(auto i: v1)
mini=min(i,mini);
vector<ll int> v3;
for(int i=0;i<v1.size();i++)
{if(v1[i]!=v2[i])
  v3.push_back(v1[i]);
}
if(v3.empty())
{
  cout<<"YES\n";
}
else
{
ll int d=findgcd(v3);
if(d==mini)
  cout<<"YES\n";
else
  cout<<"NO\n";

}

}

}


#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <ctime>
#include <queue>
#include <sstream>
#include <utility>
using namespace std;
int n,m,k;
map<int,int>mp;
int main()
{
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)
	{
		int t;
		cin>>t;
		mp[t]++;
	}
	for(int i=1;i<=m;i++)
	{
		int t;
		cin>>t;
		mp[t]--;
	}
	for(int i=1;i<=k;i++)
	{
		if(mp[i]>0)
		{
			cout<<"YES";
			return 0;
		}
	}
	cout<<"NO";
	return 0;
}


#include<iostream>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<deque>
#include<set>
#include<map>
using namespace std;
#define PB push_back
#define MP make_pair
#define L first
#define R second
typedef pair<int,int> pie;
map<int,int> m;
vector<int> v;
vector<int> q;
int main()
{
	ios_base::sync_with_stdio(false);
	int n,mm,k;
	cin>>n>>mm>>k;
	for(int i=1;i<=n;i++)
	{
		int l;
		cin>>l;
		m[l]++;
	}
	for(int i=1;i<=mm;i++)
	{
		int l;
		cin>>l;
		if(m.find(l)!=m.end())
			m[l]--;
		else
			q.PB(l);
	}
	for(map<int,int>::iterator it=m.begin();it!=m.end();++it)
	{
		int kk = it->L;
		int o = it->R;
		for(int i=0;i<o;i++)
			v.PB(kk);
	}
	sort(v.begin(),v.end());
	sort(q.begin(),q.end());
	if(v.size()==0)
	{
		cout<<"NO";
		return 0;
	}
	if(q.size()==0)
	{
		cout<<"YES";
		return 0;
	}
	int maxx = v[v.size()-1];
	int moxx = q[q.size()-1];
	if(maxx>moxx)
	{
		cout<<"YES";
		return 0;
	}
	int ans = 0;
	int ons = 0;
	if(v.size()>q.size())
	{
		cout<<"YES";
		return 0;
	}
	ans = q.size()-v.size();
	for(int i=0;i<v.size();i++)
		if(v[i]>q[i])
			ons++;
	if(ons>ans)
	{
		cout<<"YES";
		return 0;
	}
	else
	{
		cout<<"NO";
		return 0;
	}
}