#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const char nl = '\n';
const long long inf = 1e9;
const long long inf2 = 1e18 + 10;
const long double inf3 = 1e14;
const long long mod = 998244353;
const long double eps = 1e-6;
const int logn = 18, maxn = 1e5, maxm = 1e5 + 5;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int mi = *min_element((a).begin(), (a).end());
  vector<bool> g(n);
  for (int i = 0; i < n; i++) g[i] = (a[i] % mi == 0);
  vector<int> x, y;
  for (int i = 0; i < n; i++) {
    if (g[i])
      x.push_back(a[i]);
    else
      y.push_back(a[i]);
  }
  sort((x).begin(), (x).end());
  int i1 = 0, i2 = 0;
  for (int i = 0; i < n; i++) {
    if (g[i]) {
      a[i] = x[i1];
      i1++;
    } else {
      a[i] = y[i2];
      i2++;
    }
  }
  for (int i = 1; i < n; i++) {
    if (a[i - 1] > a[i]) {
      cout << "NO" << nl;
      return;
    }
  }
  cout << "YES" << nl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t;
  cin >> t;
  while (t--) solve();
}
