#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int mod = 1000000007;
const double pi = acos(-1);
inline void gn(long long &x) {
  int sg = 1;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? (sg = -1, x = 0) : (x = c - '0');
  while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
  x *= sg;
}
inline void gn(int &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(unsigned long long &x) {
  long long t;
  gn(t);
  x = t;
}
int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
long long powmod(long long a, long long x, long long mod) {
  long long t = 1ll;
  while (x) {
    if (x & 1) t = t * a % mod;
    a = a * a % mod;
    x >>= 1;
  }
  return t;
}
const int maxn = 1e6 + 100;
vector<int> v[maxn];
int f[maxn];
int ite[maxn] = {0};
int find(int x) {
  if (x == f[x]) return x;
  return find(f[x]);
}
void add(int a, int b) {
  a = find(a);
  b = find(b);
  f[a] = b;
}
bool cmp(int &a, int &b) { return b < a; }
int numb[maxn];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = (1); i <= (n); i++) gn(numb[i]), f[i] = i;
  int a, b;
  for (int i = (1); i <= (m); i++) {
    scanf("%d %d", &a, &b);
    add(a, b);
  }
  for (int i = (1); i <= (n); i++) v[find(f[i])].push_back(numb[i]);
  for (int i = (1); i <= (n); i++) sort(v[i].begin(), v[i].end(), cmp);
  for (int i = (1); i <= (n); i++) {
    int f = find(i);
    printf("%d ", v[f][ite[f]++]);
  }
  return 0;
}
