#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj_list;
int par[1000010];
int n, m;
int numb[1000010];
int x, y;
vector<int> group[1000010];
vector<int> pos_group[1000010];
int part[1000010];
int ans[1000010];
int anc(int p) {
  if (par[p] == p) return p;
  return par[p] = anc(par[p]);
}
void join(int p, int q) { par[anc(p)] = anc(q); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> numb[i];
    par[i] = i;
  }
  for (int i = 1; i <= m; ++i) {
    cin >> x >> y;
    join(x, y);
  }
  for (int i = 1; i <= n; ++i) {
    part[i] = anc(i);
  }
  for (int i = 1; i <= n; ++i) {
    group[part[i]].push_back(numb[i]);
  }
  for (int i = n; i >= 1; --i) {
    pos_group[part[i]].push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    sort(group[i].begin(), group[i].end());
    for (int j = group[i].size() - 1; j >= 0; --j) {
      ans[pos_group[i][j]] = group[i][j];
    }
  }
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << endl;
  }
}
