#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

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

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

ll a[maxn];
ll b[maxn];

bool solve() {
    vector<ll> v;
    int n = readint();
    ll Min = 1e18;
    for (int i = 0; i < n; i++) a[i] = readll(),b[i] = a[i], Min = min(Min, a[i]);
    sort(b, b + n);
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            if (a[i] % Min != 0) return false;
            v.push_back(a[i] / Min);
        }
    }
    if (!v.size()) return true;
    ll g = v[0];
    for (int i = 1; i < v.size(); i++) g = gcd(g, v[i]);
    return g == 1ll;
}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}


#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

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

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

ll a[200005];
ll b[200005];

bool solve() {
    vector<ll> v;
    int n = readint();
    ll Min = 1e18;
    for (int i = 0; i < n; i++) a[i] = readll(),b[i] = a[i], Min = min(Min, a[i]);
    sort(b, b + n);
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            if (a[i] % Min != 0) return false;
            v.push_back(a[i] / Min);
        }
    }
    if (!v.size()) return true;
    ll g = v[0];
    for (int i = 1; i < v.size(); i++) g = gcd(g, v[i]);
    return g == 1ll;
}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}


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

int main() {
	// your code goes here
	int t;
	cin >> t;
	while(t--){
	    long long n;
	    cin >> n;
	    int f = 1;
	    long long a[1000000], b[1000000];
	    long long minni = INT_MAX;
	    for(int i = 0; i < n; i++){
	        cin >> a[i];
	        b[i] = a[i];
	        if(minni > a[i])
	            minni = a[i];
	    }
	    sort(a, a + n);
	    for(int i = 0; i < n; i++){
	        if(a[i] != b[i] && b[i] % minni != 0){
	            f = 0;
	        }
	    }
	    if(f == 1)
	        cout << "YES" << endl;
	    else
	        cout << "NO" << endl;
	}
	return 0;
}


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

int main() {
	// your code goes here
	int t;
	cin >> t;
	while(t--){
	    long long n;
	    cin >> n;
	    int f = 1;
	    long long a[1000000], b[1000000] = {0};
	    long long minni = INT_MAX;
	    for(int i = 0; i < n; i++){
	        cin >> a[i];
	        b[i] = a[i];
	        if(minni > a[i])
	            minni = a[i];
	    }
	    sort(a, a + n);
	    for(int i = 0; i < n; i++){
	        if(a[i] != b[i] && b[i] % minni != 0){
	            f = 0;
	        }
	    }
	    if(f == 1)
	        cout << "YES" << endl;
	    else
	        cout << "NO" << endl;
	}
	return 0;
}


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

int main() {
	// your code goes here
	int t;
	cin >> t;
	while(t--){
	    long long n;
	    cin >> n;
	    int f = 1;
	    long long a[100000], b[1000000] = {0};
	    long long minni = INT_MAX;
	    for(int i = 0; i < n; i++){
	        cin >> a[i];
	        b[i] = a[i];
	        if(minni > a[i])
	            minni = a[i];
	    }
	    sort(a, a + n);
	    for(int i = 0; i < n; i++){
	        if(a[i] != b[i] && b[i] % minni != 0){
	            f = 0;
	        }
	    }
	    if(f == 1)
	        cout << "YES" << endl;
	    else
	        cout << "NO" << endl;
	}
	return 0;
}


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

int main() {
	// your code goes here
	int test;
	cin >> test;
	while(test--){
	    long long n;
	    cin >> n;
	    vector<long long> a(n);
	    vector<long long> b(n);
	    long long minn = INT_MAX;
	    for(int i = 0; i < n; i++){
	        cin >> a[i];
	        b[i] = a[i];
	        if(minn > a[i])
	            minn = a[i];
	    }
	    sort(a.begin(), a.end());
	    int flag = 1;
	    for(int i = 0; i < n; i++){
	        if(a[i] != b[i] && b[i] % minn != 0){
	            //cout << a[i] << " " << b[i] << " " << minn << endl;
	            flag = 0;
	        }
	    }
	    if(flag == 1)
	        cout << "YES" << endl;
	    else
	        cout << "NO" << endl;
	}
	return 0;
}


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

int main() {
	// your code goes here
	int t;
	cin >> t;
	while(t--){
	    long long n;
	    cin >> n;
	    int f = 1;
	    long long a[n], b[n];
	    long long minni = INT_MAX;
	    for(int i = 0; i < n; i++){
	        cin >> a[i];
	        b[i] = a[i];
	        if(minni > a[i])
	            minni = a[i];
	    }
	    sort(a, a + n);
	    for(int i = 0; i < n; i++){
	        if(a[i] != b[i] && b[i] % minni != 0){
	            f = 0;
	        }
	    }
	    if(f == 1)
	        cout << "YES" << endl;
	    else
	        cout << "NO" << endl;
	}
	return 0;
}


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

int main() {
	// your code goes here
	int t;
	cin >> t;
	while(t--){
	    long long n;
	    cin >> n;
	    int f = 1;
	    long long a[n], b[1000000] = {0};
	    long long minni = INT_MAX;
	    for(int i = 0; i < n; i++){
	        cin >> a[i];
	        b[i] = a[i];
	        if(minni > a[i])
	            minni = a[i];
	    }
	    sort(a, a + n);
	    for(int i = 0; i < n; i++){
	        if(a[i] != b[i] && b[i] % minni != 0){
	            f = 0;
	        }
	    }
	    if(f == 1)
	        cout << "YES" << endl;
	    else
	        cout << "NO" << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mx 500000
ll brr[mx];
ll   arr[mx];
map<int,int>mm;
map<ll,ll>mmm;
vector<int>v5;
vector<ll>vv;
int   main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
ll   t;
cin>>t;
while(t--){
     ll   n;
     vector<ll>v,v2;
     cin>>n;
     ll   k=9999999999999;
     for(ll   i=0;i<n;i++){
        cin>>arr[i];
        v.pb(arr[i]);
        k=min(k,arr[i]);
     }
     sort(v.begin(),v.end());
     ll   f=0;
     for(ll   i=0;i<n;i++){
        if(v[i]!=arr[i]){
            v2.pb(v[i]);
            f=1;
        }
     }

     ll   gc=0;
     for(ll   i=0;i<v2.size();i++){
        gc=__gcd(gc,v2[i]);
     }
     if(gc==k  ||f==0)
        cout<<"YES"<<endl;
     else
        cout<<"NO"<<endl;
        v.clear();v2.clear();
}

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mx 500000
ll brr[mx];
int arr[mx];
map<int,int>mm;
map<ll,ll>mmm;
vector<int>v5;
vector<ll>vv;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int t;
cin>>t;
while(t--){
     int n;
     vector<int>v,v2;
     cin>>n;
     int k=99999999;
     for(int i=0;i<n;i++){
        cin>>arr[i];
        v.pb(arr[i]);
        k=min(k,arr[i]);
     }
     sort(v.begin(),v.end());
     int f=0;
     for(int i=0;i<n;i++){
        if(v[i]!=arr[i]){
            if(__gcd(k,arr[i])!=k)
            f=1;
        }
     }

     if(f==0)
        cout<<"YES"<<endl;
     else
        cout<<"NO"<<endl;
        v.clear();v2.clear();
}

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mx 500000
ll brr[mx];
int arr[mx];
map<int,int>mm;
map<ll,ll>mmm;
vector<int>v5;
vector<ll>vv;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int t;
cin>>t;
while(t--){
     int n;
     vector<int>v,v2;
     cin>>n;
     int k=INT_MAX;
     for(int i=0;i<n;i++){
        cin>>arr[i];
        v.pb(arr[i]);
        k=min(k,arr[i]);
     }
     sort(v.begin(),v.end());
     for(int i=0;i<n;i++){
        if(v[i]!=arr[i])
            v2.pb(v[i]);
     }
     if(v2.size()==0)
     {
         cout<<"YES"<<endl;
         continue;
     }
     int gc=0;
     for(int i=0;i<v2.size();i++){
        gc=__gcd(gc,v2[i]);
     }
     if(gc==k)
        cout<<"YES"<<endl;
     else
        cout<<"NO"<<endl;
}

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mx 500000
ll brr[mx];
int arr[mx];
map<int,int>mm;
map<ll,ll>mmm;
vector<int>v5;
vector<ll>vv;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int t;
cin>>t;
while(t--){
     int n;
     vector<int>v,v2;
     cin>>n;
     int k=99999999;
     for(int i=0;i<n;i++){
        cin>>arr[i];
        v.pb(arr[i]);
        k=min(k,arr[i]);
     }
     sort(v.begin(),v.end());
     int f=0;
     for(int i=0;i<n;i++){
        if(v[i]!=arr[i]){
            v2.pb(v[i]);
            f=1;
        }
     }

     int gc=0;
     for(int i=0;i<v2.size();i++){
        gc=__gcd(gc,v2[i]);
     }
     if(gc==k  ||f==0)
        cout<<"YES"<<endl;
     else
        cout<<"NO"<<endl;
        v.clear();v2.clear();
}

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mx 500000
ll brr[mx];
ll  arr[mx];
map<int,int>mm;
map<ll,ll>mmm;
vector<int>v5;
vector<ll>vv;
int   main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
ll  t;
cin>>t;
while(t--){
     ll  n;
     vector<int>v,v2;
     cin>>n;
     ll  k=999999999999999;
     for(ll  i=0;i<n;i++){
        cin>>arr[i];
        v.pb(arr[i]);
        k=min(k,arr[i]);
     }
     sort(v.begin(),v.end());
     ll  f=0;
     for(ll  i=0;i<n;i++){
        if(v[i]!=arr[i]){
            if(__gcd(k,arr[i])!=k)
            f=1;
        }
     }

     if(f==0)
        cout<<"YES"<<endl;
     else
        cout<<"NO"<<endl;
        v.clear();v2.clear();
}

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mx 500000
ll brr[mx];
int arr[mx];
map<int,int>mm;
map<ll,ll>mmm;
vector<int>v5;
vector<ll>vv;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int t;
cin>>t;
while(t--){
     int n;
     vector<int>v,v2;
     cin>>n;
     int k=99999999;
     for(int i=0;i<n;i++){
        cin>>arr[i];
        v.pb(arr[i]);
        k=min(k,arr[i]);
     }
     sort(v.begin(),v.end());
     for(int i=0;i<n;i++){
        if(v[i]!=arr[i])
            v2.pb(v[i]);
     }
     if(v2.size()==0)
     {
         cout<<"YES"<<endl;
         continue;
     }
     int gc=v2[0];
     for(int i=1;i<v2.size();i++){
        gc=__gcd(gc,v2[i]);
     }
     if(gc==k)
        cout<<"YES"<<endl;
     else
        cout<<"NO"<<endl;
}

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mx 500000
ll brr[mx];
ll   arr[mx];
map<int,int>mm;
map<ll,ll>mmm;
vector<int>v5;
vector<ll>vv;
int   main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
ll   t;
cin>>t;
while(t--){
     ll   n;
     vector<int>v,v2;
     cin>>n;
     ll   k=9999999999999;
     for(ll   i=0;i<n;i++){
        cin>>arr[i];
        v.pb(arr[i]);
        k=min(k,arr[i]);
     }
     sort(v.begin(),v.end());
     ll   f=0;
     for(ll   i=0;i<n;i++){
        if(v[i]!=arr[i]){
            v2.pb(v[i]);
            f=1;
        }
     }

     ll   gc=0;
     for(ll   i=0;i<v2.size();i++){
        gc=__gcd(gc,v2[i]);
     }
     if(gc==k  ||f==0)
        cout<<"YES"<<endl;
     else
        cout<<"NO"<<endl;
        v.clear();v2.clear();
}

}


#pragma GCC optimize("Ofast")
//#include<bits/stdc++.h>
#include <iostream>
#include<vector>
#include<set>
#include<iterator>
#include<string>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include<numeric>
#define baap_coder17 ios_base::sync_with_stdio(false); cin.tie(NULL);
#define   lli long long int
#define ld long double
#define  endl "\n"
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ui unsigned int
#define mt make_tuple
#define t_(t)[(i)]  get<(i)>((t))
#define vsort(v) sort((v).begin(), (v).end())
#define vsortr(v) sort((v).rbegin(), (v).rend())
#define all(v)  (v).begin(), (v).end()
#define rall(v)  (v).rbegin(), (v).rend()
#define vdp(n,x) vector<vector<ll>>dp(n+1,vector<ll>(x+1));
const ld EPSILON=0.000000001;
const lli mod=1e9+7;
const lli AM=2e5+5;
const ld pi=3.14159265358979323846;
lli max(lli a,lli b){if(a>b)return a;else return b;}
using namespace std;
lli fact(lli n) {lli ans=1; for(lli i=1;i<=n;i++) ans*=i; return ans;}
lli powm(lli a, lli b, lli n){lli x=1, y=a; while (b > 0) {if (b%2 == 1) {x = (x*y) % n; } y = (y*y) % n; b /= 2;}return x % n;}
lli pow(lli x, lli y) { lli res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; }
lli min(lli a,lli b) {if(a>b)return b;else return a;}
bool cmp(ld x,ld y){if(abs(x-y)<=EPSILON)return 1;return 0;}
 bool checkprime(lli n){ for(lli i=2;i*i<=n;i++) if(n%i==0)return false;   return true;}
lli gcd(lli a, lli b){return b ? gcd (b, a % b) : a;}
lli lcm(lli a, lli b) { return (a * b) / gcd(a, b); }
lli modInverse(lli a, lli m) { lli g = gcd(a, m); return powm(a, m - 2, m); }

int main()
{
    int t; cin>>t;
    l1:while(t--)
    {
        int n; cin>>n;
        vector<lli> v(n);
        multiset<pair<lli,int>> st;
        lli mini=LLONG_MAX;
        int idx = -1;
        for(int i=0;i<n;i++)
        {
            cin>>v[i];
            if(v[i]<=mini)
            {
                mini=v[i];
                idx  = i;
            }
            st.insert({v[i],i});
        }
        for(int i=0;i<n-1;i++)
        {
            if(v[i]!=mini)
            {
                if(v[i]%mini!=0)
                {
                    cout<<"NO"<<endl;
                    goto l1;
                }
                auto it1 = st.find({mini,idx});
                auto  it2 = st.find({v[i],i});
                st.erase(it1);
                st.erase(it2);
                st.insert({v[i]*mini,idx});
                //swap(v[i] ,v[idx]);
                v[idx] = v[i]*mini;
                v[i] = mini;
                mini = st.begin()->X;
                idx  = st.begin()->Y;
            } else
            {
                st.erase(st.begin());
                mini = st.begin()->X;
                idx  = st.begin()->Y;
            }
        }
        cout<<"YES"<<endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast")
//#include<bits/stdc++.h>
#include <iostream>
#include<vector>
#include<set>
#include<iterator>
#include<string>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include<numeric>
#define baap_coder17 ios_base::sync_with_stdio(false); cin.tie(NULL);
#define   lli long long int
#define ld long double
#define  endl "\n"
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ui unsigned int
#define mt make_tuple
#define t_(t)[(i)]  get<(i)>((t))
#define vsort(v) sort((v).begin(), (v).end())
#define vsortr(v) sort((v).rbegin(), (v).rend())
#define all(v)  (v).begin(), (v).end()
#define rall(v)  (v).rbegin(), (v).rend()
#define vdp(n,x) vector<vector<ll>>dp(n+1,vector<ll>(x+1));
const ld EPSILON=0.000000001;
const lli mod=1e9+7;
const lli AM=2e5+5;
const ld pi=3.14159265358979323846;
lli max(lli a,lli b){if(a>b)return a;else return b;}
using namespace std;
lli fact(lli n) {lli ans=1; for(lli i=1;i<=n;i++) ans*=i; return ans;}
lli powm(lli a, lli b, lli n){lli x=1, y=a; while (b > 0) {if (b%2 == 1) {x = (x*y) % n; } y = (y*y) % n; b /= 2;}return x % n;}
lli pow(lli x, lli y) { lli res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; }
lli min(lli a,lli b) {if(a>b)return b;else return a;}
bool cmp(ld x,ld y){if(abs(x-y)<=EPSILON)return 1;return 0;}
 bool checkprime(lli n){ for(lli i=2;i*i<=n;i++) if(n%i==0)return false;   return true;}
lli gcd(lli a, lli b){return b ? gcd (b, a % b) : a;}
lli lcm(lli a, lli b) { return (a * b) / gcd(a, b); }
lli modInverse(lli a, lli m) { lli g = gcd(a, m); return powm(a, m - 2, m); }

int main()
{
    int t; cin>>t;
    l1:while(t--)
    {
        int n; cin>>n;
        vector<lli> v(n);
        multiset<pair<lli,int>> st;
        lli mini=LLONG_MAX;
        int idx = -1;
        for(int i=0;i<n;i++)
        {
            cin>>v[i];
            if(v[i]<=mini)
            {
                mini=v[i];
                idx  = i;
            }
            st.insert({v[i],i});
        }
        for(int i=0;i<n;i++)
        {
            if(v[i]!=mini)
            {
                if(v[i]%mini!=0)
                {
                    cout<<"NO"<<endl;
                    goto l1;
                }
                auto it1 = st.find({mini,idx});
                auto  it2 = st.find({v[i],i});
                st.erase(it1);
                st.erase(it2);
                st.insert({v[i]*mini,idx});
                //swap(v[i] ,v[idx]);
                v[idx] = v[i]*mini;
                v[i] = mini;
                mini = st.begin()->X;
                idx  = st.begin()->Y;
            } else
            {
                st.erase(st.find({v[i],i}));
                mini = st.begin()->X;
                idx  = st.begin()->Y;
            }
        }
        cout<<"YES"<<endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast")
//#include<bits/stdc++.h>
#include <iostream>
#include<vector>
#include<set>
#include<iterator>
#include<string>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include<numeric>
#define baap_coder17 ios_base::sync_with_stdio(false); cin.tie(NULL);
#define   lli long long int
#define ld long double
#define  endl "\n"
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ui unsigned int
#define mt make_tuple
#define t_(t)[(i)]  get<(i)>((t))
#define vsort(v) sort((v).begin(), (v).end())
#define vsortr(v) sort((v).rbegin(), (v).rend())
#define all(v)  (v).begin(), (v).end()
#define rall(v)  (v).rbegin(), (v).rend()
#define vdp(n,x) vector<vector<ll>>dp(n+1,vector<ll>(x+1));
const ld EPSILON=0.000000001;
const lli mod=1e9+7;
const lli AM=2e5+5;
const ld pi=3.14159265358979323846;
lli max(lli a,lli b){if(a>b)return a;else return b;}
using namespace std;
lli fact(lli n) {lli ans=1; for(lli i=1;i<=n;i++) ans*=i; return ans;}
lli powm(lli a, lli b, lli n){lli x=1, y=a; while (b > 0) {if (b%2 == 1) {x = (x*y) % n; } y = (y*y) % n; b /= 2;}return x % n;}
lli pow(lli x, lli y) { lli res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; }
lli min(lli a,lli b) {if(a>b)return b;else return a;}
bool cmp(ld x,ld y){if(abs(x-y)<=EPSILON)return 1;return 0;}
 bool checkprime(lli n){ for(lli i=2;i*i<=n;i++) if(n%i==0)return false;   return true;}
lli gcd(lli a, lli b){return b ? gcd (b, a % b) : a;}
lli lcm(lli a, lli b) { return (a * b) / gcd(a, b); }
lli modInverse(lli a, lli m) { lli g = gcd(a, m); return powm(a, m - 2, m); }

int main()
{
    int t; cin>>t;
    l1:while(t--)
    {
        int n; cin>>n;
        vector<lli> v(n);
        multiset<pair<lli,int>> st;
        lli mini=LLONG_MAX;
        for(int i=0;i<n;i++)
        {
            cin>>v[i];
            if(v[i]<=mini)
            {
                mini=v[i];
            }
            st.insert({v[i],i});
        }
        for(int i=0;i<n;i++)
        {
            if(v[i]!=st.begin()->X)
            {
                if(st.begin()->X%mini!=0 or v[i]%mini!=0)
                {
                    cout<<"NO"<<endl;
                    goto l1;
                }
                st.erase(st.find({v[i],i}));
                v[st.begin()->Y]=v[i];
                v[i] = st.begin()->X;
                st.insert({v[st.begin()->Y],st.begin()->Y});
                st.erase(st.begin());
            } else
            {
                st.erase(st.find({v[i],i}));
            }
        }
        cout<<"YES"<<endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast")
//#include<bits/stdc++.h>
#include <iostream>
#include<vector>
#include<set>
#include<iterator>
#include<string>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include<numeric>
#define baap_coder17 ios_base::sync_with_stdio(false); cin.tie(NULL);
#define   lli long long int
#define ld long double
#define  endl "\n"
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ui unsigned int
#define mt make_tuple
#define t_(t)[(i)]  get<(i)>((t))
#define vsort(v) sort((v).begin(), (v).end())
#define vsortr(v) sort((v).rbegin(), (v).rend())
#define all(v)  (v).begin(), (v).end()
#define rall(v)  (v).rbegin(), (v).rend()
#define vdp(n,x) vector<vector<ll>>dp(n+1,vector<ll>(x+1));
const ld EPSILON=0.000000001;
const lli mod=1e9+7;
const lli AM=2e5+5;
const ld pi=3.14159265358979323846;
lli max(lli a,lli b){if(a>b)return a;else return b;}
using namespace std;
lli fact(lli n) {lli ans=1; for(lli i=1;i<=n;i++) ans*=i; return ans;}
lli powm(lli a, lli b, lli n){lli x=1, y=a; while (b > 0) {if (b%2 == 1) {x = (x*y) % n; } y = (y*y) % n; b /= 2;}return x % n;}
lli pow(lli x, lli y) { lli res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; }
lli min(lli a,lli b) {if(a>b)return b;else return a;}
bool cmp(ld x,ld y){if(abs(x-y)<=EPSILON)return 1;return 0;}
 bool checkprime(lli n){ for(lli i=2;i*i<=n;i++) if(n%i==0)return false;   return true;}
lli gcd(lli a, lli b){return b ? gcd (b, a % b) : a;}
lli lcm(lli a, lli b) { return (a * b) / gcd(a, b); }
lli modInverse(lli a, lli m) { lli g = gcd(a, m); return powm(a, m - 2, m); }

int main()
{
    int t; cin>>t;
    l1:while(t--)
    {
        int n; cin>>n;
        vector<lli> v(n);
        multiset<pair<lli,int>> st;
        lli mini=LLONG_MAX;
        int idx = -1;
        for(int i=0;i<n;i++)
        {
            cin>>v[i];
            if(v[i]<=mini)
            {
                mini=v[i];
                idx  = i;
            }
            st.insert({v[i],i});
        }
        for(int i=0;i<n;i++)
        {
            if(v[i]>v[i+1])
            {
                if(v[i]%mini!=0)
                {
                    cout<<"NO"<<endl;
                    goto l1;
                }
                auto it1 = st.find({mini,idx});
                auto  it2 = st.find({v[i],i});
                st.erase(it1);
                //st.erase(it2);
                st.insert({v[i]*mini,idx});
                swap(v[i] ,v[idx]);
                v[idx] *=mini;
                mini = st.begin()->X;
                idx  = st.begin()->Y;
            }
        }
        cout<<"YES"<<endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast")
//#include<bits/stdc++.h>
#include <iostream>
#include<vector>
#include<set>
#include<iterator>
#include<string>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include<numeric>
#define baap_coder17 ios_base::sync_with_stdio(false); cin.tie(NULL);
#define   lli long long int
#define ld long double
#define  endl "\n"
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ui unsigned int
#define mt make_tuple
#define t_(t)[(i)]  get<(i)>((t))
#define vsort(v) sort((v).begin(), (v).end())
#define vsortr(v) sort((v).rbegin(), (v).rend())
#define all(v)  (v).begin(), (v).end()
#define rall(v)  (v).rbegin(), (v).rend()
#define vdp(n,x) vector<vector<ll>>dp(n+1,vector<ll>(x+1));
const ld EPSILON=0.000000001;
const lli mod=1e9+7;
const lli AM=2e5+5;
const ld pi=3.14159265358979323846;
lli max(lli a,lli b){if(a>b)return a;else return b;}
using namespace std;
lli fact(lli n) {lli ans=1; for(lli i=1;i<=n;i++) ans*=i; return ans;}
lli powm(lli a, lli b, lli n){lli x=1, y=a; while (b > 0) {if (b%2 == 1) {x = (x*y) % n; } y = (y*y) % n; b /= 2;}return x % n;}
lli pow(lli x, lli y) { lli res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; }
lli min(lli a,lli b) {if(a>b)return b;else return a;}
bool cmp(ld x,ld y){if(abs(x-y)<=EPSILON)return 1;return 0;}
 bool checkprime(lli n){ for(lli i=2;i*i<=n;i++) if(n%i==0)return false;   return true;}
lli gcd(lli a, lli b){return b ? gcd (b, a % b) : a;}
lli lcm(lli a, lli b) { return (a * b) / gcd(a, b); }
lli modInverse(lli a, lli m) { lli g = gcd(a, m); return powm(a, m - 2, m); }

int main()
{
    int t; cin>>t;
    l1:while(t--)
    {
        int n; cin>>n;
        vector<lli> v(n);
        multiset<pair<lli,int>> st;
        lli mini=LLONG_MAX;
        int idx = -1;
        for(int i=0;i<n;i++)
        {
            cin>>v[i];
            if(v[i]<=mini)
            {
                mini=v[i];
                idx  = i;
            }
            st.insert({v[i],i});
        }
        for(int i=0;i<n;i++)
        {
            if(v[i]!=st.begin()->X)
            {
                if(gcd(v[i],st.begin()->X)!=mini)
                {
                    cout<<"NO"<<endl;
                    goto l1;
                }
                st.erase(st.find({v[i],i}));
                v[st.begin()->Y]=v[i];
                v[i] = st.begin()->X;
                st.insert({v[st.begin()->Y],st.begin()->Y});
                st.erase(st.begin());
            } else
            {
                st.erase(st.find({v[i],i}));
            }
        }
        cout<<"YES"<<endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast")
//#include<bits/stdc++.h>
#include <iostream>
#include<vector>
#include<set>
#include<iterator>
#include<string>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include<numeric>
#define baap_coder17 ios_base::sync_with_stdio(false); cin.tie(NULL);
#define   lli long long int
#define ld long double
#define  endl "\n"
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ui unsigned int
#define mt make_tuple
#define t_(t)[(i)]  get<(i)>((t))
#define vsort(v) sort((v).begin(), (v).end())
#define vsortr(v) sort((v).rbegin(), (v).rend())
#define all(v)  (v).begin(), (v).end()
#define rall(v)  (v).rbegin(), (v).rend()
#define vdp(n,x) vector<vector<ll>>dp(n+1,vector<ll>(x+1));
const ld EPSILON=0.000000001;
const lli mod=1e9+7;
const lli AM=2e5+5;
const ld pi=3.14159265358979323846;
lli max(lli a,lli b){if(a>b)return a;else return b;}
using namespace std;
lli fact(lli n) {lli ans=1; for(lli i=1;i<=n;i++) ans*=i; return ans;}
lli powm(lli a, lli b, lli n){lli x=1, y=a; while (b > 0) {if (b%2 == 1) {x = (x*y) % n; } y = (y*y) % n; b /= 2;}return x % n;}
lli pow(lli x, lli y) { lli res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; }
lli min(lli a,lli b) {if(a>b)return b;else return a;}
bool cmp(ld x,ld y){if(abs(x-y)<=EPSILON)return 1;return 0;}
 bool checkprime(lli n){ for(lli i=2;i*i<=n;i++) if(n%i==0)return false;   return true;}
lli gcd(lli a, lli b){return b ? gcd (b, a % b) : a;}
lli lcm(lli a, lli b) { return (a * b) / gcd(a, b); }
lli modInverse(lli a, lli m) { lli g = gcd(a, m); return powm(a, m - 2, m); }

int main()
{
    int t; cin>>t;
    l1:while(t--)
    {
        int n; cin>>n;
        vector<lli> v(n);
        multiset<lli> st;
        lli mini=LLONG_MAX;
        for(int i=0;i<n;i++)
        {
            cin>>v[i];
            mini = min(v[i], mini);
            st.insert(v[i]);
        }
        for(int i=0;i<n-1;i++)
        {
            if(v[i]>v[i+1]) {
                if (v[i] % mini != 0) {
                    cout << "NO" << endl;
                    goto l1;
                }
                st.insert(v[i] * mini);
                st.erase(st.find(v[i]));
                st.erase(st.begin());
                mini = *st.begin();
            }
        }
        cout<<"YES"<<endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast")
//#include<bits/stdc++.h>
#include <iostream>
#include<vector>
#include<set>
#include<iterator>
#include<string>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include<numeric>
#define baap_coder17 ios_base::sync_with_stdio(false); cin.tie(NULL);
#define   lli long long int
#define ld long double
#define  endl "\n"
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ui unsigned int
#define mt make_tuple
#define t_(t)[(i)]  get<(i)>((t))
#define vsort(v) sort((v).begin(), (v).end())
#define vsortr(v) sort((v).rbegin(), (v).rend())
#define all(v)  (v).begin(), (v).end()
#define rall(v)  (v).rbegin(), (v).rend()
#define vdp(n,x) vector<vector<ll>>dp(n+1,vector<ll>(x+1));
const ld EPSILON=0.000000001;
const lli mod=1e9+7;
const lli AM=2e5+5;
const ld pi=3.14159265358979323846;
lli max(lli a,lli b){if(a>b)return a;else return b;}
using namespace std;
lli fact(lli n) {lli ans=1; for(lli i=1;i<=n;i++) ans*=i; return ans;}
lli powm(lli a, lli b, lli n){lli x=1, y=a; while (b > 0) {if (b%2 == 1) {x = (x*y) % n; } y = (y*y) % n; b /= 2;}return x % n;}
lli pow(lli x, lli y) { lli res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; }
lli min(lli a,lli b) {if(a>b)return b;else return a;}
bool cmp(ld x,ld y){if(abs(x-y)<=EPSILON)return 1;return 0;}
 bool checkprime(lli n){ for(lli i=2;i*i<=n;i++) if(n%i==0)return false;   return true;}
lli gcd(lli a, lli b){return b ? gcd (b, a % b) : a;}
lli lcm(lli a, lli b) { return (a * b) / gcd(a, b); }
lli modInverse(lli a, lli m) { lli g = gcd(a, m); return powm(a, m - 2, m); }

int main()
{
    int t; cin>>t;
    l1:while(t--)
    {
        int n; cin>>n;
        vector<lli> v(n);
        multiset<pair<lli,int>> st;
        lli mini=LLONG_MAX;
        int idx = -1;
        for(int i=0;i<n;i++)
        {
            cin>>v[i];
            if(v[i]<=mini)
            {
                mini=v[i];
                idx  = i;
            }
            st.insert({v[i],i});
        }
        for(int i=0;i<n-1;i++)
        {
            if(v[i]>v[i+1])
            {
                if(v[i]%mini!=0)
                {
                    cout<<"NO"<<endl;
                    goto l1;
                }
                auto it1 = st.find({mini,idx});
                auto  it2 = st.find({v[i],i});
                st.erase(it1);
                st.erase(it2);
                st.insert({v[i]*mini,idx});
                //swap(v[i] ,v[idx]);
                v[idx] = v[i]*mini;
                v[i] = mini;
                mini = st.begin()->X;
                idx  = st.begin()->Y;
            }
        }
        cout<<"YES"<<endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast")
//#include<bits/stdc++.h>
#include <iostream>
#include<vector>
#include<set>
#include<iterator>
#include<string>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include<numeric>
#define baap_coder17 ios_base::sync_with_stdio(false); cin.tie(NULL);
#define   lli long long int
#define ld long double
#define  endl "\n"
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ui unsigned int
#define mt make_tuple
#define t_(t)[(i)]  get<(i)>((t))
#define vsort(v) sort((v).begin(), (v).end())
#define vsortr(v) sort((v).rbegin(), (v).rend())
#define all(v)  (v).begin(), (v).end()
#define rall(v)  (v).rbegin(), (v).rend()
#define vdp(n,x) vector<vector<ll>>dp(n+1,vector<ll>(x+1));
const ld EPSILON=0.000000001;
const lli mod=1e9+7;
const lli AM=2e5+5;
const ld pi=3.14159265358979323846;
lli max(lli a,lli b){if(a>b)return a;else return b;}
using namespace std;
lli fact(lli n) {lli ans=1; for(lli i=1;i<=n;i++) ans*=i; return ans;}
lli powm(lli a, lli b, lli n){lli x=1, y=a; while (b > 0) {if (b%2 == 1) {x = (x*y) % n; } y = (y*y) % n; b /= 2;}return x % n;}
lli pow(lli x, lli y) { lli res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; }
lli min(lli a,lli b) {if(a>b)return b;else return a;}
bool cmp(ld x,ld y){if(abs(x-y)<=EPSILON)return 1;return 0;}
 bool checkprime(lli n){ for(lli i=2;i*i<=n;i++) if(n%i==0)return false;   return true;}
lli gcd(lli a, lli b){return b ? gcd (b, a % b) : a;}
lli lcm(lli a, lli b) { return (a * b) / gcd(a, b); }
lli modInverse(lli a, lli m) { lli g = gcd(a, m); return powm(a, m - 2, m); }

int main()
{
    int t; cin>>t;
    l1:while(t--)
    {
        int n; cin>>n;
        vector<lli> v(n);
        multiset<pair<lli,int>> st;
        lli mini=LLONG_MAX;
        int idx = -1;
        for(int i=0;i<n;i++)
        {
            cin>>v[i];
            if(v[i]<=mini)
            {
                mini=v[i];
                idx  = i;
            }
            st.insert({v[i],i});
        }
        for(int i=0;i<n;i++)
        {
            if(v[i]!=st.begin()->X)
            {
                if(gcd(v[i],st.begin()->X)!=mini)
                {
                    cout<<"NO"<<endl;
                    goto l1;
                }
                st.erase(st.find({v[i],i}));
                v[st.begin()->Y]=v[i]*mini;
                v[i] = st.begin()->X;
                st.insert({v[st.begin()->Y],st.begin()->Y});
                st.erase(st.begin());
            } else
            {
                st.erase(st.find({v[i],i}));
            }
        }
        cout<<"YES"<<endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast")
//#include<bits/stdc++.h>
#include <iostream>
#include<vector>
#include<set>
#include<iterator>
#include<string>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include<numeric>
#define baap_coder17 ios_base::sync_with_stdio(false); cin.tie(NULL);
#define   lli long long int
#define ld long double
#define  endl "\n"
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ui unsigned int
#define mt make_tuple
#define t_(t)[(i)]  get<(i)>((t))
#define vsort(v) sort((v).begin(), (v).end())
#define vsortr(v) sort((v).rbegin(), (v).rend())
#define all(v)  (v).begin(), (v).end()
#define rall(v)  (v).rbegin(), (v).rend()
#define vdp(n,x) vector<vector<ll>>dp(n+1,vector<ll>(x+1));
const ld EPSILON=0.000000001;
const lli mod=1e9+7;
const lli AM=2e5+5;
const ld pi=3.14159265358979323846;
lli max(lli a,lli b){if(a>b)return a;else return b;}
using namespace std;
lli fact(lli n) {lli ans=1; for(lli i=1;i<=n;i++) ans*=i; return ans;}
lli powm(lli a, lli b, lli n){lli x=1, y=a; while (b > 0) {if (b%2 == 1) {x = (x*y) % n; } y = (y*y) % n; b /= 2;}return x % n;}
lli pow(lli x, lli y) { lli res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; }
lli min(lli a,lli b) {if(a>b)return b;else return a;}
bool cmp(ld x,ld y){if(abs(x-y)<=EPSILON)return 1;return 0;}
 bool checkprime(lli n){ for(lli i=2;i*i<=n;i++) if(n%i==0)return false;   return true;}
lli gcd(lli a, lli b){return b ? gcd (b, a % b) : a;}
lli lcm(lli a, lli b) { return (a * b) / gcd(a, b); }
lli modInverse(lli a, lli m) { lli g = gcd(a, m); return powm(a, m - 2, m); }

int main()
{
    int t; cin>>t;
    l1:while(t--)
    {
        int n; cin>>n;
        vector<lli> v(n);
        multiset<lli> st;
        lli mini=LLONG_MAX;
        for(int i=0;i<n;i++)
        {
            cin>>v[i];
            mini = min(v[i], mini);
            st.insert(v[i]);
        }
        for(int i=0;i<n-1;i++)
        {
            if(v[i]>v[i+1])
            {
                lli tmp=-1;
                if(v[i]%mini!=0)
                    goto l2;
                for(auto it:st)
                {
                    if(it%mini==0)
                    {
                        tmp=it;
                        break;
                    }
                }
                l2:if(tmp==-1)
                {
                    cout<<"NO"<<endl;
                    goto l1;
                }
            }
        }
        cout<<"YES"<<endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast")
//#include<bits/stdc++.h>
#include <iostream>
#include<vector>
#include<set>
#include<iterator>
#include<string>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include<numeric>
#define baap_coder17 ios_base::sync_with_stdio(false); cin.tie(NULL);
#define   lli long long int
#define ld long double
#define  endl "\n"
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ui unsigned int
#define mt make_tuple
#define t_(t)[(i)]  get<(i)>((t))
#define vsort(v) sort((v).begin(), (v).end())
#define vsortr(v) sort((v).rbegin(), (v).rend())
#define all(v)  (v).begin(), (v).end()
#define rall(v)  (v).rbegin(), (v).rend()
#define vdp(n,x) vector<vector<ll>>dp(n+1,vector<ll>(x+1));
const ld EPSILON=0.000000001;
const lli mod=1e9+7;
const lli AM=2e5+5;
const ld pi=3.14159265358979323846;
lli max(lli a,lli b){if(a>b)return a;else return b;}
using namespace std;
lli fact(lli n) {lli ans=1; for(lli i=1;i<=n;i++) ans*=i; return ans;}
lli powm(lli a, lli b, lli n){lli x=1, y=a; while (b > 0) {if (b%2 == 1) {x = (x*y) % n; } y = (y*y) % n; b /= 2;}return x % n;}
lli pow(lli x, lli y) { lli res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; }
lli min(lli a,lli b) {if(a>b)return b;else return a;}
bool cmp(ld x,ld y){if(abs(x-y)<=EPSILON)return 1;return 0;}
 bool checkprime(lli n){ for(lli i=2;i*i<=n;i++) if(n%i==0)return false;   return true;}
lli gcd(lli a, lli b){return b ? gcd (b, a % b) : a;}
lli lcm(lli a, lli b) { return (a * b) / gcd(a, b); }
lli modInverse(lli a, lli m) { lli g = gcd(a, m); return powm(a, m - 2, m); }

int main()
{
    int t; cin>>t;
    l1:while(t--)
    {
        int n; cin>>n;
        vector<lli> v(n);
        multiset<pair<lli,int>> st;
        lli mini=LLONG_MAX;
        for(int i=0;i<n;i++)
        {
            cin>>v[i];
            if(v[i]<=mini)
            {
                mini=v[i];
            }
            st.insert({v[i],i});
        }
        for(int i=0;i<n;i++)
        {
            if(v[i]!=st.begin()->X)
            {
                if(gcd(v[i],st.begin()->X)!=mini)
                {
                    cout<<"NO"<<endl;
                    goto l1;
                }
                st.erase(st.find({v[i],i}));
                v[st.begin()->Y]=v[i];
                v[i] = st.begin()->X;
                st.insert({v[st.begin()->Y],st.begin()->Y});
                st.erase(st.begin());
            } else
            {
                st.erase(st.find({v[i],i}));
            }
        }
        cout<<"YES"<<endl;
    }
    return 0;
}


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

#define int long long int
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}

/*
Mistakes

- while using pairs make sure you comparator datatype it's int by default change it as per the need
- Modify comparator if you want to
*/
#define f first
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll64 int64_t
#define pb push_back
#define pll pair<long long int,long long int>
#define pi pair<int,int>
#define mi map<int,int>
#define mll map<long long int,long long int>
#define umi unordered_map<int,int>
#define umll unordered_map<long long int,long long int>
#define vpi vector<pair<int,int>>
#define vpll vector<pair<long long int,long long int>>
#define mod 1000000007
#define fp(i,x,n) for(int i=x;i<n;i++)
#define fn(i,x,n) for(int i=x;i>n;i--)
#define fpm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define asort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysec)
#define asort_vp_first(vect) sort(vect.begin(), vect.end())
#define dsort_vp_first(vect) sort(vect.begin(), vect.end(), sortinrev)
#define dsort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysecdesc)
#define vi vector<int>
#define vll vector<long long int>
#define c(x) cout<<x<<endl;
#define M 1000000007
using namespace std;

signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a1[n];

        fp(i,0,n)
        cin>>a1[i];

        int mn = *min_element(a1,a1+n);

        int b[n];
        memcpy(b, a1, n*sizeof(int));
        sort(b,b+n);

        vector<int> v;

        fp(i,0,n)
        {
            if(a1[i]!=b[i])
            v.pb(a1[i]);
        }
        int a[100001];
        memset(a,0,sizeof(a));
        fp(i,0,v.size())
        {
            for(int j=1;j<=sqrt(v[i]);j++)
            {
                if(v[i]%j==0)
                {
                    if(v[i]/j==j)
                    {
                        a[j]++;
                    }else
                    {
                        a[j]++;
                        a[v[i]/j]++;
                    }
                }
            }
        }
        int c=0;
        for(int i=1;i<100001;i++)
        {
            if(i>=mn && a[i]==v.size())
            {
                c++;
            }
        }
        if(v.size()==0)
        cout<<"YES"<<endl;
        else if(c==1 && a[mn]==v.size())
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}


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

#define int long long int
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}

/*
Mistakes

- while using pairs make sure you comparator datatype it's int by default change it as per the need
- Modify comparator if you want to
*/
#define f first
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll64 int64_t
#define pb push_back
#define pll pair<long long int,long long int>
#define pi pair<int,int>
#define mi map<int,int>
#define mll map<long long int,long long int>
#define umi unordered_map<int,int>
#define umll unordered_map<long long int,long long int>
#define vpi vector<pair<int,int>>
#define vpll vector<pair<long long int,long long int>>
#define mod 1000000007
#define fp(i,x,n) for(int i=x;i<n;i++)
#define fn(i,x,n) for(int i=x;i>n;i--)
#define fpm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define asort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysec)
#define asort_vp_first(vect) sort(vect.begin(), vect.end())
#define dsort_vp_first(vect) sort(vect.begin(), vect.end(), sortinrev)
#define dsort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysecdesc)
#define vi vector<int>
#define vll vector<long long int>
#define c(x) cout<<x<<endl;
#define M 1000000007
using namespace std;

signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a1[n];

        fp(i,0,n)
        cin>>a1[i];

        int mn = *min_element(a1,a1+n);

        int b[n];
        memcpy(b, a1, n*sizeof(int));
        sort(b,b+n);

        vector<int> v;

        fp(i,0,n)
        {
            if(a1[i]!=b[i])
            v.pb(a1[i]);
        }
        int a[100001];
        memset(a,0,sizeof(a));
        fp(i,0,v.size())
        {
            for(int j=1;j<=sqrt(v[i]);j++)
            {
                if(v[i]%j==0)
                {
                    if(v[i]/j==j)
                    {
                        a[j]++;
                    }else
                    {
                        a[j]++;
                        a[v[i]/j]++;
                    }
                }
            }
        }
        if(v.size()==0)
        cout<<"YES"<<endl;
        else if(a[mn]==v.size())
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}


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

#define int long long int
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}

/*
Mistakes

- while using pairs make sure you comparator datatype it's int by default change it as per the need
- Modify comparator if you want to
*/
#define f first
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll64 int64_t
#define pb push_back
#define pll pair<long long int,long long int>
#define pi pair<int,int>
#define mi map<int,int>
#define mll map<long long int,long long int>
#define umi unordered_map<int,int>
#define umll unordered_map<long long int,long long int>
#define vpi vector<pair<int,int>>
#define vpll vector<pair<long long int,long long int>>
#define mod 1000000007
#define fp(i,x,n) for(int i=x;i<n;i++)
#define fn(i,x,n) for(int i=x;i>n;i--)
#define fpm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define asort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysec)
#define asort_vp_first(vect) sort(vect.begin(), vect.end())
#define dsort_vp_first(vect) sort(vect.begin(), vect.end(), sortinrev)
#define dsort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysecdesc)
#define vi vector<int>
#define vll vector<long long int>
#define c(x) cout<<x<<endl;
#define M 1000000007
using namespace std;

signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];

        fp(i,0,n)
        cin>>a[i];

        int mn = *min_element(a,a+n);

        int b[n];
        memcpy(b, a, n*sizeof(int));
        sort(b,b+n);

        vector<int> v;

        fp(i,0,n)
        {
            if(a[i]!=b[i])
            v.pb(a[i]);
        }
        sort(v.begin(),v.end());
        int f=0;
        fp(i,0,(int)v.size()-1)
        {

            if(__gcd(v[i],v[i+1])!=mn)
            {
                f=1;
                break;
            }
        }
        if(f==1)
        cout<<"NO"<<endl;
        else
        cout<<"YES"<<endl;
    }
}


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

#define int long long int
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}

/*
Mistakes

- while using pairs make sure you comparator datatype it's int by default change it as per the need
- Modify comparator if you want to
*/
#define f first
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll64 int64_t
#define pb push_back
#define pll pair<long long int,long long int>
#define pi pair<int,int>
#define mi map<int,int>
#define mll map<long long int,long long int>
#define umi unordered_map<int,int>
#define umll unordered_map<long long int,long long int>
#define vpi vector<pair<int,int>>
#define vpll vector<pair<long long int,long long int>>
#define mod 1000000007
#define fp(i,x,n) for(int i=x;i<n;i++)
#define fn(i,x,n) for(int i=x;i>n;i--)
#define fpm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define asort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysec)
#define asort_vp_first(vect) sort(vect.begin(), vect.end())
#define dsort_vp_first(vect) sort(vect.begin(), vect.end(), sortinrev)
#define dsort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysecdesc)
#define vi vector<int>
#define vll vector<long long int>
#define c(x) cout<<x<<endl;
#define M 1000000007
using namespace std;
int x[100001],y[100001];
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;

        int a1[n+1];

        fp(i,1,n+1)
        cin>>a1[i];

        int mn = *min_element(a1+1,a1+n+1);

        int k=0;
        fp(i,1,n+1)
        {
            if(a1[i]%mn==0)
            {
               x[k] = i;
               y[k] = a1[i];
               k++;
            }
        }
        sort(y,y+k);

        fp(i,0,k)
        a1[x[i]]=y[i];

        int f=0;
        fp(i,1,n)
        {
            if(a1[i]>a1[i+1])
            {
                f=1;
            }
        }
        if(f==1)
        cout<<"NO"<<endl;
        else
        cout<<"YES"<<endl;
    }
}


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

#define int long long int
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}

/*
Mistakes

- while using pairs make sure you comparator datatype it's int by default change it as per the need
- Modify comparator if you want to
*/
#define f first
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll64 int64_t
#define pb push_back
#define pll pair<long long int,long long int>
#define pi pair<int,int>
#define mi map<int,int>
#define mll map<long long int,long long int>
#define umi unordered_map<int,int>
#define umll unordered_map<long long int,long long int>
#define vpi vector<pair<int,int>>
#define vpll vector<pair<long long int,long long int>>
#define mod 1000000007
#define fp(i,x,n) for(int i=x;i<n;i++)
#define fn(i,x,n) for(int i=x;i>n;i--)
#define fpm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define asort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysec)
#define asort_vp_first(vect) sort(vect.begin(), vect.end())
#define dsort_vp_first(vect) sort(vect.begin(), vect.end(), sortinrev)
#define dsort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysecdesc)
#define vi vector<int>
#define vll vector<long long int>
#define c(x) cout<<x<<endl;
#define M 1000000007
using namespace std;

signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];

        fp(i,0,n)
        cin>>a[i];

        int mn = *min_element(a,a+n);

        int b[n];
        memcpy(b, a, n*sizeof(int));
        sort(b,b+n);

        vector<int> v;

        fp(i,0,n)
        {
            if(a[i]!=b[i])
            v.pb(a[i]);
        }
        sort(v.begin(),v.end());
        int f=0;
        if(v.size()>0){
        int g = v[0];
        fp(i,1,(int)v.size())
        {
            g = __gcd(g,v[i]);
        }
        if(g==mn)
        f=0;
        else
        f=1;
        }
        if(f==1)
        cout<<"NO"<<endl;
        else
        cout<<"YES"<<endl;
    }
}


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

#define int long long int
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}

/*
Mistakes

- while using pairs make sure you comparator datatype it's int by default change it as per the need
- Modify comparator if you want to
*/
#define f first
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll64 int64_t
#define pb push_back
#define pll pair<long long int,long long int>
#define pi pair<int,int>
#define mi map<int,int>
#define mll map<long long int,long long int>
#define umi unordered_map<int,int>
#define umll unordered_map<long long int,long long int>
#define vpi vector<pair<int,int>>
#define vpll vector<pair<long long int,long long int>>
#define mod 1000000007
#define fp(i,x,n) for(int i=x;i<n;i++)
#define fn(i,x,n) for(int i=x;i>n;i--)
#define fpm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define asort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysec)
#define asort_vp_first(vect) sort(vect.begin(), vect.end())
#define dsort_vp_first(vect) sort(vect.begin(), vect.end(), sortinrev)
#define dsort_vp_second(vect) sort(vect.begin(), vect.end(), sortbysecdesc)
#define vi vector<int>
#define vll vector<long long int>
#define c(x) cout<<x<<endl;
#define M 1000000007
using namespace std;
int x[100001],y[100001];
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a1[n];

        fp(i,0,n)
        cin>>a1[i];

        int mn = *min_element(a1,a1+n);
        int k=0;
        fp(i,0,n)
        {
            if(a1[i]%mn==0)
            {
               x[k] = i;
               y[k] = a1[i];
               k++;
            }
        }
        sort(y,y+k);
        fp(i,0,k)
        a1[x[i]]=y[i];

        int f=0;
        fp(i,0,n)
        {
            if(a1[i]>a1[i+1])
            {
                f=1;
            }
        }
        if(f==1)
        cout<<"NO"<<endl;
        else
        cout<<"YES"<<endl;
    }
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int usg(int a,int b){
  if(b==0){
    return a;
  }else{
    return usg(b,a%b);
  }
}
int f(int n,int k,int a[]){
  int b;
  for(int i=0;i<n;i++){
if(k==a[i]){
  b=i;
  break;
}
  }
  return b;
}
void q(int n,int a[]){
  vector<int> v;
  int min,b=0;
  for(int i=0;i<n;i++){
    v.push_back(a[i]);
  }
  sort(a,a+n);
min=a[0];
for(int i=0;i<n;i++){
  if(a[i]!=v[i]&&usg(a[i],v[i])==min){
swap(v[i],v[f(n,v[i],a)]);
  }
}
for(int i=0;i<n;i++){
  if(a[i]!=v[i]){
    b=1;
    break;
  }
}
if(b==1){
  cout<<"NO"<<endl;
}else{
  cout<<"YES"<<endl;
}
}
int main() {
int t,n,a[100001];
cin>>t;
while(t--){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  q(n,a);
}
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define ll long long
int usg(int a,int b){
  if(b==0){
    return a;
  }else{
    return usg(b,a%b);
  }
}
void q(int n,int a[]){
  vector<int> v;
  int b=0;
  vector<int> k;
  for(int i=0;i<n;i++){
    v.push_back(a[i]);
  }
  sort(a,a+n);
  for(int i=0;i<n;i++){
    if(a[i]!=v[i]){
      k.push_back(a[i]);
    }
  }
  int c=a[0];
  if(k.size()>=1){
for(int i=0;i<k.size()-1;i++){
if(usg(k[i],k[i+1])==c){
  b+=1;
}
}
}
if(b==k.size()-1||k.size()==0){
  cout<<"YES"<<endl;
}else{
  cout<<"NO"<<endl;
}
}
int main() {
int t,n,a[100001];
cin>>t;
while(t--){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  q(n,a);
}
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define ll long long
int usg(int a,int b){
  if(b==0){
    return a;
  }else{
    return usg(b,a%b);
  }
}
void q(int n,int a[]){
  vector<int> v;
  for(int i=0;i<n;i++){
    v.push_back(a[i]);
  }
sort(a,a+n);

int b=0,min=a[0],c=0;
for(int i=0;i<n;i++){

  if(v[i]!=a[i]&&usg(v[i],a[i])==min){
c+=1;

}else{
  if(v[i]!=a[i]){
    b+=1;
  }
}

}
if(c>=b){
  cout<<"YES"<<endl;
}else{
  cout<<"NO"<<endl;
}
}
int main() {
int t,n,a[100001];
cin>>t;
while(t--){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  q(n,a);
}
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int usg(int a,int b){
  if(b==0){
    return a;
  }else{
    return usg(b,a%b);
  }
}
void q(int n,int a[]){
  vector<int> v;
  int min,b=0;
  for(int i=0;i<n;i++){
    v.push_back(a[i]);
  }
  sort(a,a+n);
min=a[0];
for(int i=0;i<n;i++){
  if(a[i]!=v[i]&&usg(a[i],v[i])!=min){
b=1;
break;
  }
}
if(b==0){
  cout<<"YES"<<endl;
}else{
  cout<<"NO"<<endl;
}
}
int main() {
int t,n,a[100001];
cin>>t;
while(t--){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  q(n,a);
}
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
void q(int n,int a[]){
vector<int> v;
int b,c=0;
for(int i=0;i<n;i++){
	v.push_back(a[i]);
}
sort(a,a+n);
b=a[0];
for(int i=0;i<n;i++){
	if(a[i]!=v[i]&&a[i]%b!=0){
		c=1;
		break;
	}
}
if(c==0){
	cout<<"YES"<<endl;
}else{
	cout<<"NO"<<endl;
}
}
int main (){
	int t,n,a[100001];
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		q(n,a);
	}
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define ll long long
int usg(int a,int b){
  if(b==0){
    return a;
  }else{
    return usg(b,a%b);
  }
}
void q(int n,int a[]){
  vector<int> v;
  for(int i=0;i<n;i++){
    v.push_back(a[i]);
  }
sort(a,a+n);

int b=0,min=a[0],c=0;
for(int i=0;i<n;i++){

  if(v[i]!=a[i]&&usg(v[i],a[i])==min){
c+=1;

}else{
  if(v[i]!=a[i]){
    b+=1;
  }
}

}
if(c>=(b+1)/2){
  cout<<"YES"<<endl;
}else{
  cout<<"NO"<<endl;
}
}
int main() {
int t,n,a[100001];
cin>>t;
while(t--){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  q(n,a);
}
}


#include<bits/stdc++.h>
using namespace std;
                       ///****   Hasebul Hassan Chowdhury ***////
#define ms(a,v)        memset(a,v,sizeof a)
#define lll            long long
#define FOR(i,a,b)     for(int i=a;i<=b;i++)
#define fast           ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Read           freopen("input.txt", "r", stdin)
#define Write          freopen("output.txt", "w", stdout)
#define INF            100000

int main()
{
    //Read;
    //Write
    fast
    int t;
    cin>>t;
    while(t--)
    {
        int n,x;
        cin>>n;
        int Min=1000000000;
        vector<int>arr,party;
        typedef pair<int,int>P;
        vector<P>brr;
        for(int i=0;i<n;i++){ cin>>x; arr.push_back(x); brr.push_back(P(x,i)); Min=min(Min,x); }
        party=arr;
        sort(brr.begin(),brr.end());
        sort(party.begin(),party.end());
        for(int i=0;i<n;i++)
        {
            if(arr[i]!=brr[i].first)
            {
                int GCD=__gcd(arr[i],brr[i].first);
                if(GCD!=Min) { cout<<"NO"<<endl; return 0;}
                swap(arr[i],arr[brr[i].second]);
                int num=lower_bound(party.begin(),party.end(),arr[i])-party.begin();
                //cout<<num<<" "<<party[num]<<endl;
                swap(brr[num].second,brr[i].second);
            }
        }
        cout<<"YES"<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
                       ///****   Hasebul Hassan Chowdhury ***////
#define ms(a,v)        memset(a,v,sizeof a)
#define lll            long long
#define FOR(i,a,b)     for(int i=a;i<=b;i++)
#define fast           ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Read           freopen("input.txt", "r", stdin)
#define Write          freopen("output.txt", "w", stdout)
#define INF            100000

int main()
{
    //Read;
    //Write
    fast
    int t;
    cin>>t;
    while(t--)
    {
        int n,x;
        cin>>n;
        int Min=1000000000;
        vector<int>arr,brr;
        for(int i=0;i<n;i++){ cin>>x; arr.push_back(x);  Min=min(Min,x); }
        brr=arr;
        sort(brr.begin(),brr.end());
        for(int i=0;i<n;i++)
        {
            if((arr[i]!=brr[i])&&(arr[i]%Min!=0)) { cout<<"NO"<<endl; return 0; }
        }
        cout<<"YES"<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
                       ///****   Hasebul Hassan Chowdhury ***////
#define ms(a,v)        memset(a,v,sizeof a)
#define lll            long long
#define FOR(i,a,b)     for(int i=a;i<=b;i++)
#define fast           ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Read           freopen("input.txt", "r", stdin)
#define Write          freopen("output.txt", "w", stdout)
#define INF            100000

int main()
{
    //Read;
    //Write
    fast
    int t;
    cin>>t;
    while(t--)
    {
        int n,x,temp=0;
        cin>>n;
        int Min=1000000000;
        vector<int>arr,brr;
        for(int i=0;i<n;i++){ cin>>x; arr.push_back(x);  Min=min(Min,x); }
        brr=arr;
        sort(brr.begin(),brr.end());
        for(int i=0;i<n;i++)
        {
            if((arr[i]!=brr[i])&&(arr[i]%Min!=0)) { cout<<"NO"<<endl; temp=1; break; }
        }
        if(temp==0) cout<<"YES"<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
                       ///****   Hasebul Hassan Chowdhury ***////
#define ms(a,v)        memset(a,v,sizeof a)
#define lll            long long
#define FOR(i,a,b)     for(int i=a;i<=b;i++)
#define fast           ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Read           freopen("input.txt", "r", stdin)
#define Write          freopen("output.txt", "w", stdout)
#define INF            1000000000

void solve()
{
   int n;
   cin>>n;
   int arr[n+3];
   vector<int>v;
   int Min=INT_MAX;
   for(int i=1;i<=n;i++) { cin>>arr[i]; Min=min(Min,arr[i]); v.push_back(arr[i]); }
   sort(v.begin(),v.end());
   for(int i=1;i<=n;i++)
   {
       if(arr[i]!=v[i-1])
       {
           int GCD=__gcd(v[i-1],arr[i]);
           if(GCD!=Min) { cout<<"NO"<<endl; return; }
       }
       //int GCD=__gcd(v[i-1],arr[i]);
   }
   cout<<"YES"<<endl;

}

int main()
{
    //Read;
    //Write
    fast
    int t; cin>>t; while(t--) solve();
}


#include<bits/stdc++.h>
using namespace std;
                       ///****   Hasebul Hassan Chowdhury ***////
#define ms(a,v)        memset(a,v,sizeof a)
#define lll            long long
#define FOR(i,a,b)     for(int i=a;i<=b;i++)
#define fast           ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Read           freopen("input.txt", "r", stdin)
#define Write          freopen("output.txt", "w", stdout)
#define INF            1000000000000

void solve()
{
   int n;
   cin>>n;
   long long arr[n+3];
   typedef pair<long long,int>P;
   vector<P>v;
   lll Min=INF;
   for(int i=1;i<=n;i++) { cin>>arr[i]; Min=min(Min,arr[i]); v.push_back(P(arr[i],i)); }
   sort(v.begin(),v.end());
   for(int i=1;i<=n;i++)
   {
       if(arr[i]!=v[i-1].first)
       {
           int GCD=__gcd(v[i-1].first,arr[i]);
           if(GCD!=Min) { cout<<"NO"<<endl; return; }
           swap(arr[i],arr[v[i-1].second]);
       }
       //int GCD=__gcd(v[i-1],arr[i]);
   }
   cout<<"YES"<<endl;

}

int main()
{
    //Read;
    //Write
    fast
    int t; cin>>t; while(t--) solve();
}


#include<bits/stdc++.h>
using namespace std;
                       ///****   Hasebul Hassan Chowdhury ***////
#define ms(a,v)        memset(a,v,sizeof a)
#define lll            long long
#define FOR(i,a,b)     for(int i=a;i<=b;i++)
#define fast           ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Read           freopen("input.txt", "r", stdin)
#define Write          freopen("output.txt", "w", stdout)
#define INF            1000000000000

lll gcd(lll a, lll 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);
}

void solve()
{
   int n;
   cin>>n;
   long long arr[n+3];
   typedef pair<long long,int>P;
   vector<P>v;
   lll Min=INF;
   for(int i=1;i<=n;i++) { cin>>arr[i]; Min=min(Min,arr[i]); v.push_back(P(arr[i],i)); }
   sort(v.begin(),v.end());
   for(int i=1;i<=n;i++)
   {
       if(arr[i]!=v[i-1].first)
       {
           long long GCD=gcd(v[i-1].first,arr[i]);
           if(GCD!=Min) { cout<<"NO"<<endl; return; }
           swap(arr[i],arr[v[i-1].second]);
       }
       //int GCD=__gcd(v[i-1],arr[i]);
   }
   cout<<"YES"<<endl;

}

int main()
{
    //Read;
    //Write
    fast
    int t; cin>>t; while(t--) solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int a[N];
int b[N];
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		priority_queue <int,vector<int>,greater<int> > q;
		int cur=1e9+10;
		int cnt=0;

		for(int i=0;i<n;i++) {
			cin>>a[i];
			cur = min(cur,a[i]);
			q.push(a[i]);
			b[a[i]] = i;
		}
		int f=1;
		for(int i=0;i<n;i++) {
			int t = q.top();
			q.pop();
			if(a[i]!=t) {
				if(__gcd(a[i],t)!=cur) {
					f=0;
					break;
				}
				else {
					a[b[t]] = a[i];
				}
			}
	}

				if(f==0) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
		}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int a[N];
int b[N];
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		priority_queue <int,vector<int>,greater<int> > q;
		int cur=1e9+10;
		int cnt=0;
		map<int,int>mp;
		for(int i=0;i<n;i++) {
			cin>>a[i];
			cur = min(cur,a[i]);
			q.push(a[i]);
			mp[a[i]] = i;
		}
		int f=1;
		for(int i=0;i<n;i++) {
			int t = q.top();
			q.pop();
			if(a[i]!=t) {
				b[cnt++] = a[i];
				if(__gcd(a[i],t)!=cur) {
					f=0;
					break;
				}
				else {
					a[mp[t]] = a[i];
				}
			}
	}
			int f1=1;
		for(int i=0;i<cnt-1;i++) {
			if(__gcd(b[i],b[i+1])!=cur) {
				f1=0;
				break;
			}
		}
				if(f==0||f1==0) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
		}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int a[N];
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		priority_queue <int,vector<int>,greater<int> > q;
		int cur=1e9+10;
		int cnt=0;
		map<int,int>mp;
		for(int i=0;i<n;i++) {
			cin>>a[i];
			cur = min(cur,a[i]);
			q.push(a[i]);
			mp[a[i]] = i;
		}
		int f=1;
		for(int i=0;i<n;i++) {
			int t = q.top();
			q.pop();
			if(a[i]!=t) {
				if(__gcd(a[i],t)!=cur) {
					f=0;
					break;
				}
				else {
					a[mp[t]] = a[i];
				}
			}
	}

				if(f==0) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
		}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N =1e5+10;
int a[N];
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int mi = 0x3f3f3f3f;
		vector<pair<int,int> >v;
		for(int i=0;i<n;i++) {
			cin>>a[i];
			mi = min (mi,a[i]);
			v.push_back({a[i],i});
		}
		int mx=-1;
		int f = 1;
		sort(v.begin(),v.end());
		for(int i=0;i<v.size();i++) {
			if(i!=v[i].second) {
				if(v[i].first%mi!=0 || a[i]%mi!=0) {
					f=0;
				}
			}
		}
		if(f==0) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int a[N];
int b[N];
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int cur=1e9+10;
		int cnt=0;
		map<int,int>mp;
		mp.clear();
		map<int,int>mp1;
			mp1.clear();
		for(int i=0;i<n;i++) {
			cin>>a[i];
			cur = min(cur,a[i]);
			mp[a[i]]=i+1;
			b[i+1] = a[i];

		}
		sort(a,a+n);
		for(int i=0;i<n;i++) {
			mp1[a[i]] = i+1;
		}
		int f=1;
		map<int,int>::iterator it;
		map<int,int>::iterator it1;
		for(it=mp.begin(),it1=mp1.begin();it!=mp.end()&&it1!=mp1.end();it++,it1++)
		 {

			if(it->second!=it1->second) {
				if(__gcd(it->first,b[it1->second])!=cur) {
					f=0;
					break;
				}
				else {
					mp[b[it1->second]] = it->second;
				}
			}
	}

		if(f==0) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;

	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int a[N];
int b[N];
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int cur=1e9+10;
		int cnt=0;
		map<int,int>mp;
		map<int,int>mp1;
		for(int i=0;i<n;i++) {
			cin>>a[i];
			cur = min(cur,a[i]);
			mp[a[i]]=i+1;
		}
		sort(a,a+n);
		for(int i=0;i<n;i++) {
			mp1[a[i]] = i+1;
		}
		map<int,int>::iterator it;
		map<int,int>::iterator it1;
		for(it=mp.begin(),it1=mp1.begin();it!=mp.end()&&it1!=mp1.end();it++,it1++) {
			if(it->second!=it1->second) {
				b[cnt++] = it->first;
			}
	}

		int f=1;
		for(int i=0;i<cnt-1;i++) {
			if(__gcd(b[i],b[i+1])!=cur) {
				f=0;
				break;
			}
		}
		if(f==0) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;

	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int a[N];
int b[N];
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		priority_queue <int,vector<int>,greater<int> > q;
		int cur=1e9+10;
		int cnt=0;

		for(int i=0;i<n;i++) {
			cin>>a[i];
			cur = min(cur,a[i]);
			q.push(a[i]);
		}
		int f=1;
		for(int i=0;i<n;i++) {
			int t = q.top();
			q.pop();
			if(a[i]!=t) {
				if(__gcd(a[i],t)!=cur) {
					f=0;
					break;
				}
			}
	}

				if(f==0) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
		}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int a[N];
int b[N];
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int cur=1e9+10;
		int cnt=0;
		map<int,int>mp;
		mp.clear();
		map<int,int>mp1;
				mp1.clear();
		for(int i=0;i<n;i++) {
			cin>>a[i];
			cur = min(cur,a[i]);
			mp[a[i]]=i+1;
		}
		sort(a,a+n);
		for(int i=0;i<n;i++) {
			mp1[a[i]] = i+1;
		}
		map<int,int>::iterator it;
		map<int,int>::iterator it1;
		for(it=mp.begin(),it1=mp1.begin();it!=mp.end()&&it1!=mp1.end();it++,it1++) {
			if(it->second!=it1->second) {
				b[cnt++] = it->first;
			}
	}

		int f=1;
		sort(b,b+cnt);
		for(int i=0;i<cnt-1;i++) {
			if(__gcd(b[i],b[i+1])!=cur) {
				f=0;
				break;
			}
		}
		if(f==0) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;

	}
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/*
  Contest 1401C

  The array is non-decreasing only if only there is an element that
  is not a power of minimum number of the array and it is in the
  same place after ordering the array in a non-decreasing way.
*/

int main () {
  int t, n, i;

  cin >> t;
  while (t--) {
    cin >> n;
    bool non_deccreasing = 0;
    int u, u_min = 9999999;
    vector <int> p (n), c (n);

    for (i = 0; i < n; i++)
    {
      cin >> u;
      p[i] = u;
      c[i] = u;
      if (u < u_min)
        u_min = u;
    }

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

    for (i = 0; i < n; i++)
      if (p[i] != c[i] && c[i] % u_min != 0)
        non_deccreasing = 1;

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

  return 0;
}


#include <iostream>
#include <vector>

using namespace std;

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

int main () {
  int t, n, i, j;

  cin >> t;
  while (t--) {
    cin >> n;
    int u, u_min = 9999999;
    vector <int> p (n);
    vector <bool> p_bool (n, 0);

    for (i = 0; i < n; i++)
    {
      cin >> u;
      p[i] = u;
      if (u < u_min)
        u_min = u;
    }

    for (i = 0; i < n; i++)
      for (j = i+1; j < n; j++)
      {
        if (p[i] > p[j] && gcd (p[i], p[j]) == u_min) {
          u = p[i];
          p[i] = p[j];
          p[j] = u;
        }
      }

    u = p[0];
    for (i = 1; i < n && u <= p[i]; i++)
      u = p[i];

    if (i < n && n != 1) cout << "NO" << endl;
    else cout << "YES" << endl;
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/*
  Contest 1401C

  The array is non-decreasing only if only there is an element that
  is not a power of minimum number of the array and it is in the
  same place after ordering the array in a non-decreasing way.
*/

int main () {
  int t, n, i;

  cin >> t;
  while (t--) {
    cin >> n;
    bool non_deccreasing = 0;
    int u, u_min = 1000000000;
    vector <int> p (n), c (n);

    for (i = 0; i < n; i++)
    {
      cin >> u;
      p[i] = u;
      c[i] = u;
      if (u < u_min)
        u_min = u;
    }

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

    for (i = 0; i < n; i++)
      if (p[i] != c[i] && c[i] % u_min != 0)
        non_deccreasing = 1;

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

  return 0;
}


#include <iostream>
#include <vector>

using namespace std;

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

int main () {
  int t, n, i, j;

  cin >> t;
  while (t--) {
    cin >> n;
    int u, u_min = 9999999;
    vector <int> p (n);
    vector <bool> p_bool (n, 0);

    for (i = 0; i < n; i++)
    {
      cin >> u;
      p[i] = u;
      if (u < u_min)
        u_min = u;
    }

    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++)
      {
        if (p[i] > p[j] && gcd (p[i], p[j]) == u_min && p_bool[i] != 1 && p_bool[j] != 1) {
          u = p[i];
          p[i] = p[j];
          p[j] = u;
          p_bool[i] = 1;
        }
      }

    u = p[0];
    for (i = 1; i < n && u <= p[i]; i++)
      u = p[i];

    if (i < n && n != 1) cout << "NO" << endl;
    else cout << "YES" << endl;
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/*
  Contest 1401C

  The array is non-decreasing only if only there is an element that
  is not a power of minimum number of the array and it is in the
  same place after ordering the array in a non-decreasing way.
*/

int main () {
  int t, n, i;

  cin >> t;
  while (t--) {
    cin >> n;
    bool non_deccreasing = 0;
    int u, u_min = 9999999;
    vector <int> p (n), c (n);

    for (i = 0; i < n; i++)
    {
      cin >> u;
      p[i] = u;
      c[i] = u;
      if (u < u_min)
        u_min = u;
    }

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

    for (i = 0; i < n; i++)
      if (p[i] != c[i] && c[i] % u_min > 0)
        non_deccreasing = 1;

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

  return 0;
}


#include <iostream>
#include <vector>

using namespace std;

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

int main () {
  int t, n, i, j;

  cin >> t;
  while (t--) {
    cin >> n;
    int u, u_min = 9999999;
    vector <int> p (n);
    vector <bool> p_bool (n, 0);

    for (i = 0; i < n; i++)
    {
      cin >> u;
      p[i] = u;
      if (u < u_min)
        u_min = u;
    }

    cout << "u_min: " << u_min << endl;

    for (i = 0; i < n; i++)
      for (j = i+1; j < n; j++)
      {
        if (p[i] > p[j] && gcd (p[i], p[j]) == u_min) {
          cout << "p[i]: " << p[i] << " - p[j]: " << p[j] << " - gcd(): " << gcd (p[i], p[j]) << endl;
          u = p[i];
          p[i] = p[j];
          p[j] = u;
        }
      }

    u = p[0];
    for (i = 1; i < n && u <= p[i]; i++)
      u = p[i];

    if (i < n && n != 1) cout << "NO" << endl;
    else cout << "YES" << endl;
  }

  return 0;
}


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

#define ll long long

ll m = INT_MAX;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        ll a[n];
        ll b[n];
        ll flag =0;
        for(ll i=0;i<n;i++){
            cin>>a[i];
            b[i] = a[i];
            m = min(m,a[i]);
        }
        sort(b,b+n);
        for(ll i=0;i<n;i++){
            if(a[i] != b[i] && a[i]%m > 0){
                flag = 1;
            }
        }
        if(flag){
            cout<<"NO"<<endl;
        }
        else cout<<"YES"<<endl;
    }
}


#import<bits/stdc++.h>

#define endl '\n'

using namespace std;

int a[100005], b[100005];
main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    for(;t--;)
    {
	int k = 0, m = 1000000000, n;

	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);

	for(int i = 0; i < n; i++)
	    if(a[i] != b[i] && a[i] % m > 0)
		k = 1;

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


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

#define ll long long

ll m = 1e9+1;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;

        ll a[n];
        ll b[n];
        ll flag =0;
        for(ll i=0;i<n;i++){
            cin>>a[i];
            b[i] = a[i];
            m = min(m,a[i]);
        }
        //cout<<m<<endl;
        sort(b,b+n);
        for(ll i=0;i<n;i++){
            if(a[i] != b[i] && a[i]%m > 0){
                flag = 1;
            }
        }
        if(flag){
            cout<<"NO"<<endl;
        }
        else cout<<"YES"<<endl;
    }
}


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

#define ll long long

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        ll m = 1e9+1;
        ll a[n];
        ll b[n];
        ll flag =0;
        for(ll i=0;i<n;i++){
            cin>>a[i];
            b[i] = a[i];
            m = min(m,a[i]);
        }
        //cout<<m<<endl;
        sort(b,b+n);
        for(ll i=0;i<n;i++){
            if(a[i] != b[i] && a[i]%m > 0){
                flag = 1;
            }
        }
        if(flag){
            cout<<"NO"<<endl;
        }
        else cout<<"YES"<<endl;
    }
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define rep(i,a,n) for(int i=a;i<n;i++)
#define rop(i,a,n) for(int i=a;i<=n;i++)
#define in insert
#define bs binary_search
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(v) v.begin(),v.end()
#define PI 3.1415926535
#define sz(s) s.size()
#define N "\n"
#define ub(v,a) upper_bound(all(v),a)
#define lb(v,a) lower_bound(all(v),a)
#define F first
#define S second
#define vi vector<int>
#define vpi vector<pair<int,int> >
#define mi map<int,int>
#define pi pair<int,int>
using namespace std;
signed main()
{
    faster
    int t=1; cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; rep(i,0,n) cin>>a[i]; vpi v; bool f=false;
        rep(i,0,n){
            v.pb({a[i],i});
        }
        sort(all(v)); int ans=v[0].F;
        rep(i,0,n){
            if(a[i]!=v[i].F){
                if(v[i].S > i){
                if(__gcd(a[i],a[v[i].S])==ans) swap(a[i],a[v[i].S]); //v[i].S=v[a[i]].S; v[a[i]].S=v[i].S; }
                else { f=true; break;}
                }
            }
        }
        //rep(i,0,n) cout<<a[i]<<" ";
        if(f) cout<<"NO"<<N;
        else cout<<"YES"<<N;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define rep(i,a,n) for(int i=a;i<n;i++)
#define rop(i,a,n) for(int i=a;i<=n;i++)
#define in insert
#define bs binary_search
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(v) v.begin(),v.end()
#define PI 3.1415926535
#define sz(s) s.size()
#define N "\n"
#define ub(v,a) upper_bound(all(v),a)
#define lb(v,a) lower_bound(all(v),a)
#define F first
#define S second
#define vi vector<int>
#define vpi vector<pair<int,int> >
#define mi map<int,int>
#define pi pair<int,int>
using namespace std;
signed main()
{
    faster
    int t=1; cin>>t;
    while(t--)
    {
        int n; cin>>n; vi v(n),a(n); rep(i,0,n) { cin>>a[i]; v[i]=a[i];}
        sort(all(v)); bool f=false; int x=v[0];
        rep(i,0,n){
            if(a[i]!=v[i] && a[i]%x !=0){
                f=true; break;
            }
        }
        if(f) cout<<"NO"<<N; else cout<<"YES"<<N;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define rep(i,a,n) for(int i=a;i<n;i++)
#define rop(i,a,n) for(int i=a;i<=n;i++)
#define in insert
#define bs binary_search
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(v) v.begin(),v.end()
#define PI 3.1415926535
#define sz(s) s.size()
#define N "\n"
#define ub(v,a) upper_bound(all(v),a)
#define lb(v,a) lower_bound(all(v),a)
#define F first
#define S second
#define vi vector<int>
#define vpi vector<pair<int,int> >
#define mi map<int,int>
#define pi pair<int,int>
using namespace std;
signed main()
{
    faster
    int t=1; cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; rep(i,0,n) cin>>a[i]; vpi v; bool f=false;
        rep(i,0,n){
            v.pb({a[i],i});
        }
        sort(all(v)); int ans=v[0].F;
        rep(i,0,n){
            if(a[i]!=v[i].F){
                if(v[i].S > i){
                if(__gcd(a[i],a[v[i].S])==ans) {swap(a[i],a[v[i].S]); swap(v[i].S,v[a[i]].S);}
                else { f=true; break;}
                }
            }
        }
        //rep(i,0,n) cout<<a[i]<<" ";
        if(f) cout<<"NO"<<N;
        else cout<<"YES"<<N;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define rep(i,a,n) for(int i=a;i<n;i++)
#define rop(i,a,n) for(int i=a;i<=n;i++)
#define in insert
#define bs binary_search
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(v) v.begin(),v.end()
#define PI 3.1415926535
#define sz(s) s.size()
#define N "\n"
#define ub(v,a) upper_bound(all(v),a)
#define lb(v,a) lower_bound(all(v),a)
#define F first
#define S second
#define vi vector<int>
#define vpi vector<pair<int,int> >
#define mi map<int,int>
#define pi pair<int,int>
using namespace std;
signed main()
{
    faster
    int t=1; cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; rep(i,0,n) cin>>a[i]; vpi v; bool f=false;
        rep(i,0,n){
            v.pb({a[i],i});
        }
        sort(all(v)); int ans=v[0].F;
        rep(i,0,n){
            if(a[i]!=v[i].F){
                if(v[i].S > i){
                if(__gcd(a[i],a[v[i].S])==ans) {swap(a[i],a[v[i].S]); v[i].S=v[a[i]].S; v[a[i]].S=v[i].S; }
                else { f=true; break;}
                }
            }
        }
        //rep(i,0,n) cout<<a[i]<<" ";
        if(f) cout<<"NO"<<N;
        else cout<<"YES"<<N;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define rep(i,a,n) for(int i=a;i<n;i++)
#define rop(i,a,n) for(int i=a;i<=n;i++)
#define in insert
#define bs binary_search
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(v) v.begin(),v.end()
#define PI 3.1415926535
#define sz(s) s.size()
#define N "\n"
#define ub(v,a) upper_bound(all(v),a)
#define lb(v,a) lower_bound(all(v),a)
#define F first
#define S second
#define vi vector<int>
#define vpi vector<pair<int,int> >
#define mi map<int,int>
#define pi pair<int,int>
using namespace std;
signed main()
{
    faster
    int t=1; cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; rep(i,0,n) cin>>a[i]; vpi v; bool f=false;
        rep(i,0,n){
            v.pb({a[i],i});
        }
        sort(all(v)); int ans=v[0].F;
        rep(i,0,n){
            if(a[i]!=v[i].F){
                if(__gcd(a[i],a[v[i].S])==ans) swap(a[i],a[v[i].S]);
                else { f=true; break;}
            }
        }
        if(f) cout<<"NO"<<N;
        else cout<<"YES"<<N;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define rep(i,a,n) for(int i=a;i<n;i++)
#define rop(i,a,n) for(int i=a;i<=n;i++)
#define in insert
#define bs binary_search
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(v) v.begin(),v.end()
#define PI 3.1415926535
#define sz(s) s.size()
#define N "\n"
#define ub(v,a) upper_bound(all(v),a)
#define lb(v,a) lower_bound(all(v),a)
#define F first
#define S second
#define vi vector<int>
#define vpi vector<pair<int,int> >
#define mi map<int,int>
#define pi pair<int,int>
using namespace std;
signed main()
{
    faster
    int t=1; cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; rep(i,0,n) cin>>a[i]; vpi v; bool f=false;
        rep(i,0,n){
            v.pb({a[i],i});
        }
        sort(all(v)); int ans=v[0].F;
        rep(i,0,n){
            if(a[i]!=v[i].F){
                if(v[i].S > i){
                if(__gcd(a[i],a[v[i].S])==ans) { swap(a[i],a[v[i].S]); v[i].S=v[a[i]].S; v[a[i]].S=v[i].S;
                    int c=v[i].S; v[i].S=v[a[i]].S; v[a[i]].S=c;
                }
                else { f=true; break;}
                }
            }
        }
        //rep(i,0,n) cout<<a[i]<<" ";
        if(f) cout<<"NO"<<N;
        else cout<<"YES"<<N;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define rep(i,a,n) for(int i=a;i<n;i++)
#define rop(i,a,n) for(int i=a;i<=n;i++)
#define in insert
#define bs binary_search
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(v) v.begin(),v.end()
#define PI 3.1415926535
#define sz(s) s.size()
#define N "\n"
#define ub(v,a) upper_bound(all(v),a)
#define lb(v,a) lower_bound(all(v),a)
#define F first
#define S second
#define vi vector<int>
#define vpi vector<pair<int,int> >
#define mi map<int,int>
#define pi pair<int,int>
using namespace std;
signed main()
{
    faster
    int t=1; cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; rep(i,0,n) cin>>a[i]; vpi v; bool f=false;
        rep(i,0,n){
            v.pb({a[i],i});
        }
        sort(all(v)); int ans=v[0].F;
        rep(i,0,n){
            if(v[i].S!=i){
                if(__gcd(a[i],a[v[i].S])==ans) swap(a[i],a[v[i].S]);
                else { f=true; break;}
            }
        }
        if(f) cout<<"NO"<<N;
        else cout<<"YES"<<N;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define rep(i,a,n) for(int i=a;i<n;i++)
#define rop(i,a,n) for(int i=a;i<=n;i++)
#define in insert
#define bs binary_search
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(v) v.begin(),v.end()
#define PI 3.1415926535
#define sz(s) s.size()
#define N "\n"
#define ub(v,a) upper_bound(all(v),a)
#define lb(v,a) lower_bound(all(v),a)
#define F first
#define S second
#define vi vector<int>
#define vpi vector<pair<int,int> >
#define mi map<int,int>
#define pi pair<int,int>
using namespace std;
signed main()
{
    faster
    int t=1; cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; rep(i,0,n) cin>>a[i]; vpi v; bool f=false;
        rep(i,0,n){
            v.pb({a[i],i});
        }
        sort(all(v)); int ans=v[0].F;
        rep(i,0,n){
            if(a[i]!=v[i].F){
                if(v[i].S > i){
                if(__gcd(a[i],a[v[i].S])==ans) { swap(a[i],a[v[i].S]); //v[i].S=v[a[i]].S; v[a[i]].S=v[i].S;
                    int c=v[i].S; v[i].S=v[a[i]].S; v[a[i]].S=c;
                }
                else { f=true; break;}
                }
            }
        }
        //rep(i,0,n) cout<<a[i]<<" ";
        if(f) cout<<"NO"<<N;
        else cout<<"YES"<<N;
    }
    return 0;
}


#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iterator>
#include<list>
#include<map>
#include<stack>
#include<queue>
#include<set>
#define ll long long
#define ld long double
#define pb push_back

using namespace std;
void solve()
{
   ll n,m,i,sum=0,x=0;
   cin>>n;
   if(n==1)
   {
       ll t;
       cin>>t;
       cout<<"YES"<<endl;
       return;
   }
   vector<ll> v,s;
   for(i=0;i<n;i++)
   {
       ll t;
       cin>>t;
       v.push_back(t);
       s.push_back(t);
   }
   sort(s.begin(),s.end());
   ll mini=s[0];
   for(i=0;i<n;i++)
   {
       if(v[i]!=s[i])
       {
           if(v[i]%mini!=0 || s[i]%mini!=0 )
           {
               cout<<"NO"<<endl;
               return;
           }
       }
   }
   cout<<"YES"<<endl;
}

int main()
{

    ll n;
    cin>>n;
    while(n--)
    {
        solve();
    }return 0;
}


#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iterator>
#include<list>
#include<map>
#include<stack>
#include<queue>
#include<set>
#define ll long long
#define ld long double
#define pb push_back

using namespace std;
void solve()
{
   ll n,m,i,sum=0,x=0;
   cin>>n;
   if(n==1)
   {
       cout<<"YES"<<endl;
       return;
   }
   vector<ll> v,s;
   for(i=0;i<n;i++)
   {
       ll t;
       cin>>t;
       v.push_back(t);
       s.push_back(t);
   }
   sort(s.begin(),s.end());
   ll mini=s[0];
   for(i=0;i<n;i++)
   {
       if(v[i]!=s[i])
       {
           if(v[i]%m!=0 || s[i]%m!=0 )
           {
               cout<<"NO"<<endl;
           }
       }
   }
   cout<<"YES"<<endl;
}

int main()
{

    ll n;
    cin>>n;
    while(n--)
    {
        solve();
    }return 0;
}


#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iterator>
#include<list>
#include<map>
#include<stack>
#include<queue>
#include<set>
#define ll long long
#define ld long double
#define pb push_back

using namespace std;
void solve()
{
   ll n,m,i,sum=0,x=0;
   cin>>n;
   if(n==1)
   {
       cout<<"YES"<<endl;
       return;
   }
   vector<ll> v,s;
   for(i=0;i<n;i++)
   {
       ll t;
       cin>>t;
       v.push_back(t);
       s.push_back(t);
   }
   sort(s.begin(),s.end());
   ll mini=s[0];
   for(i=0;i<n;i++)
   {
       if(v[i]!=s[i])
       {
           if(v[i]%mini!=0 || s[i]%mini!=0 )
           {
               cout<<"NO"<<endl;
           }
       }
   }
   cout<<"YES"<<endl;
}

int main()
{

    ll n;
    cin>>n;
    while(n--)
    {
        solve();
    }return 0;
}


#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iterator>
#include<list>
#include<queue>
#include<set>
#define ll long long
#define ld long double

using namespace std;

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

bool nond(vector<ll> v,int n)
{
    ll i=0,j=1;
    while(j!=n)
    {
        if(v[i++]>v[j++])
            return false;
    }
    return true;
}

int solve()
{
   ll n,i,t,j;
   cin>>n;
   vector<ll> v;
   for(i=0;i<n;i++)
   {
       cin>>t;
       v.push_back(t);
   }
   if(n==1)
        return 1;
    if(nond(v,n))
    {
        return 1;
    }

   auto least=min_element(v.begin(),v.end());
   for(i=0;i<n;i++)
   {
       for(j=i+1;j<n;j++)
       {
           if(v[i]>v[j])
           {
               ll a=gcd(v[i],v[j]);
               if(a==(*least))
               {
                   swap(v[i],v[j]);
                   if(nond(v,n))
                   {
                       return 1;
                   }

               }
           }
       }
   }
   return 0;

}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n;
    cin>>n;
    while(n--)
    {
        int k=solve();
        if(k)
            cout<<"YES\n";
        else
            cout<<"NO\n";
    }
    return 0;
}


#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iterator>
#include<list>
#include<map>
#include<stack>
#include<queue>
#include<set>
#define ll long long
#define ld long double
#define pb push_back

using namespace std;
void solve()
{
   ll n,m,i,sum=0,x=0;
   cin>>n;
   if(n==1)
   {
       ll t;
       cin>>t;
       cout<<"YES"<<endl;
       return;
   }
   vector<ll> v;
   vector<ll> s;
   for(i=0;i<n;i++)
   {
       ll t;
       cin>>t;
       v.push_back(t);
       s.push_back(t);
   }
   sort(s.begin(),s.end());
   ll mini=s[0];
   for(i=0;i<n;i++)
   {
       if(v[i]!=s[i])
       {
           ll k=__gcd(v[i],s[i]);
           if(k!=mini)
           {
               cout<<"NO"<<endl;
               return;
           }
       }
   }
   cout<<"YES"<<endl;
}

int main()
{

    ll n;
    cin>>n;
    while(n--)
    {
        solve();
    }return 0;
}


#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iterator>
#include<list>
#include<queue>
#include<set>
#define ll long long
#define ld long double

using namespace std;
ll gcd(ll a,ll b)
{
    if(b==0){
        return a;
    }
    else{
        return gcd(b,a%b);
}}

int nond(vector<ll> v,int n)
{
    ll i=0,j=1;
    while(j!=n)
    {
        if(v[i]>v[j]){
            return 0;}
        i++;
        j++;
    }
    return 1;
}

int solve()
{
   ll n,i,t,j,a=1;
   cin>>n;
   vector<ll> v;
   for(i=0;i<n;i++)
   {
       cin>>t;
       v.push_back(t);
   }
   if(n==1)
        return 1;
    if(nond(v,n))
    {
        cout<<"YES\n";
        return 0;
    }

   auto least=min_element(v.begin(),v.end());
   int c=*least;
   for(i=0;i<n;i++)
   {
       for(j=i+1;j<n;j++)
       {
           if(v[i]>v[j])
           {
               if(gcd(v[i],v[j])==c)
               {
                   swap(v[i],v[j]);
                   if(nond(v,n))
                    {
                        cout<<"YES\n";
                        return 0;
                    }
               }
           }
       }
   }
   cout<<"NO\n";
   return 0;

}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n;
    cin>>n;
    while(n--)
    {
        int k=solve();
    }
    return 0;
}


#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iterator>
#include<list>
#include<queue>
#include<set>
#define ll long long
#define ld long double

using namespace std;
ll gcd(ll a,ll b)
{
    if(b==0){
        return a;
    }
    else{
        return gcd(b,a%b);
}}

int nond(vector<ll> v,int n)
{
    ll i=0,j=1;
    while(j!=n)
    {
        if(v[i]>v[j]){
            return 0;}
        i++;
        j++;
    }
    return 1;
}

int solve()
{
   ll n,i,t,j,a=1;
   cin>>n;
   vector<ll> v;
   for(i=0;i<n;i++)
   {
       cin>>t;
       v.push_back(t);
   }
   if(n==1){
       cout<<"YES\n";
       return 0;
   }
    if(nond(v,n))
    {
        cout<<"YES\n";
        return 0;
    }

   auto least=min_element(v.begin(),v.end());
   int c=*least;
   for(i=0;i<n;i++)
   {
       for(j=i+1;j<n;j++)
       {
           if(v[i]>v[j])
           {
               if(gcd(v[i],v[j])==c)
               {
                   swap(v[i],v[j]);
                   if(nond(v,n))
                    {
                        cout<<"YES\n";
                        return 0;
                    }
               }
           }
       }
   }
   cout<<"NO\n";
   return 0;

}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n;
    cin>>n;
    while(n--)
    {
        int k=solve();
    }
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
   int t,n,i,g,minn;
   cin>>t;
   while(t--)
  {
    int ans=1;
    cin>>n;

    int a[n];
    int b[n];
    vector<int> v;

    for(i=0;i<n;i++)
    {
      cin>>a[i];
      b[i]=a[i];
    }

    if(n==1)
    {
    	cout<<"YES"<<endl;
    	continue;
	}

    sort(b,b+n);

    minn=*min_element(a , a + n);

    for(i=0;i<n;i++)
    {
       if(a[i]!=b[i])
	   {
	     if(a[i]%minn==0)
	     {
	     	continue;
		 }

		 else
		 {
			ans=0;
		 	break;
		 }
	   }

	}

	if(ans==1)
	{
		cout<<"YES"<<endl;;
	}

	else
	{
		cout<<"NO"<<endl;
	}

  }

}