#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g;
vector<int> used;
vector<vector<int> > ccs;
void dfs(int first, int cc) {
  stack<int> s;
  s.push(first);
  used[first] = 1;
  while (!s.empty()) {
    int v = s.top();
    s.pop();
    if (used[v] == 2) {
      continue;
    }
    used[v] = 2;
    ccs[cc].push_back(v);
    for (int i = 0; i < g[v].size(); ++i) {
      if (used[g[v][i]] != 2) {
        s.push(g[v][i]);
        used[g[v][i]] = 1;
      }
    }
  }
}
int maxx(int fd, int sd) { return (fd > sd) ? fd : sd; }
int minn(int fd, int sd) { return (fd < sd) ? fd : sd; }
int main() {
  long n, k;
  scanf("%ld %ld", &n, &k);
  vector<long> a(n);
  g.resize(n);
  for (int i = 0; i < n; ++i) {
    scanf("%ld", &a[i]);
  }
  for (int i = 0; i < k; ++i) {
    long c, d;
    scanf("%ld %ld", &c, &d);
    c--;
    d--;
    g[c].push_back(d);
    g[d].push_back(c);
  }
  int cc = -1;
  used.resize(g.size(), 0);
  ccs.resize(n);
  for (int i = 0; i < n; ++i) {
    if (used[i] == 0) {
      cc++;
      dfs(i, cc);
    }
  }
  vector<int> answer(n, 0);
  {
    for (int curCC = 0; curCC <= cc; ++curCC) {
      sort(ccs[curCC].begin(), ccs[curCC].end());
      vector<long> subanswer;
      for (int i = 0; i < ccs[curCC].size(); ++i) {
        subanswer.push_back(a[ccs[curCC][i]]);
      }
      sort(subanswer.begin(), subanswer.end());
      for (int i = 0; i < ccs[curCC].size(); ++i) {
        answer[ccs[curCC][i]] = subanswer[subanswer.size() - i - 1];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    printf("%ld ", answer[i]);
  }
  return 0;
}
