#include<cstdio>
#include<algorithm>
#define FACTOR_LIM (int) 1e6+2 // used by preFactor(n). Defined as <= n <= 1e8
using namespace std;
int lowestDiv[FACTOR_LIM+1], a[FACTOR_LIM], c[FACTOR_LIM], n;
int o[FACTOR_LIM][21];
void preFactor(int n) {
int root = 2;
for(int i = 2; i*i <= n; i++) {
if(lowestDiv[i]) continue;
root = lowestDiv[i] = i;
for(int j = i*i; j <= n; j+=i) {
lowestDiv[j] = (lowestDiv[j]) ? lowestDiv[j] : i;
}
}
for(int i = root; i <= n; i++) {
if(!lowestDiv[i]) {
lowestDiv[i] = i;
}
}
}
void fastFactor(int n, int i) {
int j = 0;
while(n > 1) {
o[i][j++] = (lowestDiv[n]);
n /= lowestDiv[n];
}
if(n > 1) o[i][j++] = n;
}
bool cmp(const int i, const int j) {
int vi = 0, vj = 0;
while(o[i][vi] != 0 && o[j][vj] != 0) {
if(o[i][vi] < o[j][vj]) return true;
else if(o[i][vi] > o[j][vj]) return false;
vi++;
vj++;
}
return o[j][vj];
}
int main() {
preFactor(FACTOR_LIM-1);
scanf("%d",&n);
for(int i = 0; i < n; i++) {
scanf("%d",&a[i]);
fastFactor(a[i], i);
c[i] = i;
}
sort(c,c+n,cmp);
for(int i = 0; i < n; i++) {
printf("%d\n",a[c[i]]);
}
return 0;
}
I2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTxhbGdvcml0aG0+CgojZGVmaW5lIEZBQ1RPUl9MSU0gKGludCkgMWU2KzIgLy8gdXNlZCBieSBwcmVGYWN0b3IobikuIERlZmluZWQgYXMgPD0gbiA8PSAxZTgKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbG93ZXN0RGl2W0ZBQ1RPUl9MSU0rMV0sIGFbRkFDVE9SX0xJTV0sIGNbRkFDVE9SX0xJTV0sIG47CmludCBvW0ZBQ1RPUl9MSU1dWzIxXTsKCnZvaWQgcHJlRmFjdG9yKGludCBuKSB7CglpbnQgcm9vdCA9IDI7Cglmb3IoaW50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