#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7, mod1 = 1e9 + 9;
const long long N = 1000005;
vector<long long> v, v2;
int a[N], vis[N];
vector<long long> ed[N];
void dfs(int in) {
  vis[in] = 1;
  v.push_back((a[in]));
  v2.push_back(in);
  for (auto x : ed[in]) {
    if (!vis[x]) dfs(x);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int n, m;
  cin >> n >> m;
  for (long long i = 1; i <= (n); i++) cin >> a[i];
  for (long long i = 0; i < (m); i++) {
    int x, y;
    cin >> x >> y;
    ed[x].push_back(y), ed[y].push_back(x);
  }
  for (long long i = 1; i <= (n); i++) {
    if (!vis[i])
      dfs(i);
    else
      continue;
    sort((v).begin(), (v).end());
    reverse((v).begin(), (v).end());
    sort((v2).begin(), (v2).end());
    for (long long i = 0; i < ((int)v.size()); i++) {
      a[v2[i]] = v[i];
    }
    v.clear();
    v2.clear();
  }
  for (long long i = 1; i <= (n); i++) cout << a[i] << ' ';
  return 0;
}
