#include <bits/stdc++.h>
using namespace std;
vector<int> g[1000001];
bool used[1000001] = {};
vector<int> p;
vector<int> pos, num;
void dfs(int v) {
  used[v] = 1;
  pos.push_back(v);
  num.push_back(p[v]);
  for (int e = 0; e < g[v].size(); ++e) {
    if (!used[g[v][e]]) {
      dfs(g[v][e]);
    }
  }
}
int main() {
  int n, m;
  int a, b;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a);
    p.push_back(a);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d %d", &a, &b);
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      pos.resize(0);
      num.resize(0);
      dfs(i);
      sort(pos.begin(), pos.end());
      sort(num.rbegin(), num.rend());
      for (int j = 0; j < pos.size(); ++j) {
        p[pos[j]] = num[j];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    printf("%d ", p[i]);
  }
  printf("\n");
  return 0;
}
