#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (a < b) {       swap(a, b);     }     if (a % b == 0) {       return b;     }     return gcd(b, a % b);   }   vector<vector<int>> gr;   vector<int> used;   void dfs(int v) {     used[v] = 1;     for (auto i : gr[v]) {       if (!used[i]) {         dfs(i);       }     }   }   void solution() {     long int n;     cin >> n;     vector<long long> mass(n);     long long minn = 1e10;     long long minn_ind = 0;     for (int i = 0; i < n; i++) {       cin >> mass[i];       if (mass[i] < minn) {         minn = mass[i];         minn_ind = i;       }     }     gr = vector<vector<int>>(n);     used = vector<int>(n, 0);     for (int i = 0; i < n; i++) {       if (gcd(mass[i], minn) == minn) {         gr[i].push_back(minn_ind);         gr[minn_ind].push_back(i);       }     }     dfs(minn_ind);     vector<long long> sorted = mass;     sort(sorted.begin(), sorted.end());     vector<long long> get_ans;     for (int i = 0; i < n; i++) {       if (mass[i] != sorted[i] || i == minn_ind) {         get_ans.push_back(i);       }     }     bool ans = 1;     for (auto i : get_ans) {       if (!used[i]) {         ans = 0;         break;       }     }     if (ans) {       cout <<  YES ;     } else {       cout <<  NO ;     }     cout << endl;   }   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       solution();     }   }   