n,*a=$<.read.split.map &:to_i s=->d{d.sort{|x,y|w=d[0];z=w.lcm(x)<=>w.lcm(y);z==0?x<=>y:z}} n.times{|i|a[i,n]=s[a[i,n]]} $><