#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
void solve() {
  int n;
  cin >> n;
  int ar[n], lock[n];
  for (int i = 0; i < n; i++) cin >> ar[i];
  vector<int> temp, res;
  bool flag = false;
  for (int i = 0; i < n - 1; i++) {
    if (ar[i] > ar[i + 1]) {
      flag = true;
    }
  }
  if (flag == false) {
    cout << "YES" << endl;
    return;
  }
  int minn = *min_element(ar, ar + n);
  for (int i = 0; i < n; i++) {
    if (ar[i] % minn == 0) {
      temp.push_back(ar[i]);
    }
  }
  sort(temp.begin(), temp.end());
  int j = 0;
  for (int i = 0; i < n; i++) {
    if (ar[i] % minn != 0) {
      res.push_back(ar[i]);
    } else
      res.push_back(temp[j++]);
  }
  for (int i = 0; i < n - 1; i++) {
    if (res[i] > res[i + 1]) {
      cout << "NO" << endl;
      return;
    }
  }
  cout << "YES" << endl;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    solve();
  }
}
