#include <bits/stdc++.h>
long long mod = 1000000007;
long long mod2 = 998244353;
long long OO = 1e18;
long long david_villa_pow(long long x, long long n) {
  if (n == 0) return 1;
  long long u = david_villa_pow(x, n / 2);
  u = ((u % mod) * (u % mod)) % mod;
  if (n % 2 == 1) u = ((u % mod) * (x % mod)) % mod;
  return u % mod;
}
long long up(long long x, long long y) {
  if (x % y == 0) return x / y;
  return x / y + 1;
}
using namespace std;
int n, m;
int Leader[2000200];
int sz[2000200];
int get_Leader(int node);
void dsu(int x, int y) {
  int lx = get_Leader(x);
  int ly = get_Leader(y);
  if (sz[lx] < sz[ly]) {
    Leader[lx] = ly;
    sz[ly] += sz[lx];
  } else {
    Leader[ly] = lx;
    sz[lx] += sz[ly];
  }
}
int get_Leader(int node) {
  if (Leader[node] == node) return node;
  return Leader[node] = get_Leader(Leader[node]);
}
int p[2000200];
set<int> s;
set<int>::iterator it;
set<int> s2;
set<int>::iterator it2;
vector<int> v[2000200];
int res[2000200];
void El_MaraVilla() {
  long long i, j;
  ;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    Leader[i] = i;
    sz[i] = 1;
  }
  for (i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    dsu(x, y);
  }
  for (i = 1; i <= n; i++) {
    v[get_Leader(i)].push_back(i);
    s.insert(get_Leader(i));
  }
  for (it = s.begin(); it != s.end(); it++) {
    int L = (*it);
    for (j = 0; j < v[L].size(); j++) s2.insert(p[v[L][j]]);
    j = v[L].size() - 1;
    for (it2 = s2.begin(); it2 != s2.end(); it2++) res[v[L][j--]] = (*it2);
    s2.clear();
  }
  for (i = 1; i <= n; i++) printf("%d ", res[i]);
}
int main() {
  long long T = 1;
  while (T--) {
    El_MaraVilla();
  }
  return 0;
}
