#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 23;
vector<long long> graf[N], comps[N], ids[N];
bool visited[N];
long long cnt = 0, a[N];
void dfs(long long x) {
  visited[x] = 1;
  comps[cnt].push_back(a[x]);
  ids[cnt].push_back(x);
  for (auto f : graf[x]) {
    if (!visited[f]) {
      dfs(f);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= m; i++) {
    long long a, b;
    cin >> a >> b;
    graf[a].push_back(b);
    graf[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
      cnt++;
    }
  }
  for (int i = 0; i < cnt; i++) {
    sort(comps[i].begin(), comps[i].end());
    sort(ids[i].begin(), ids[i].end());
    for (long j = 0; j < ids[i].size(); j++) {
      a[ids[i][j]] = comps[i][ids[i].size() - 1 - j];
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}
