#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int tc = 0; tc < t; tc++) {
    int n;
    cin >> n;
    vector<int> a;
    for (int i = 0; i < n; i++) {
      int ai;
      cin >> ai;
      a.push_back(ai);
    }
    int mini = INT_MAX;
    for (int i = 0; i < n; i++) {
      mini = min(mini, a[i]);
    }
    vector<int> notmultiples;
    vector<int> multiples;
    vector<bool> ismultiple(n);
    for (int i = 0; i < n; i++) {
      if (a[i] % mini > 0) {
        notmultiples.push_back(a[i]);
        ismultiple[i] = false;
      } else {
        multiples.push_back(a[i]);
        ismultiple[i] = true;
      }
    }
    sort(multiples.begin(), multiples.end());
    int mulitpleindex = 0;
    int notmultipleindex = 0;
    vector<int> sortedarray;
    for (int i = 0; i < n; i++) {
      if (ismultiple[i]) {
        sortedarray.push_back(multiples[mulitpleindex]);
        mulitpleindex++;
      } else {
        sortedarray.push_back(notmultiples[notmultipleindex]);
        notmultipleindex++;
      }
    }
    sort(a.begin(), a.end());
    bool sorted = true;
    for (int i = 0; i < n; i++) {
      if (a[i] != sortedarray[i]) sorted = false;
    }
    if (sorted) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}
