#include <bits/stdc++.h>
using namespace std;
int par[1000005];
int find(int z) {
  if (par[z] == z)
    return z;
  else
    return par[z] = find(par[z]);
}
void Union(int a, int b) {
  int p1 = find(a);
  int p2 = find(b);
  if (p1 > p2) swap(p1, p2);
  par[p2] = p1;
}
int main() {
  int n, m;
  cin >> n >> m;
  pair<int, int> arr[n + 1];
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> p;
    arr[i].first = p;
    arr[i].second = i;
  }
  for (int i = 1; i <= n; i++) par[i] = i;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    Union(x, y);
  }
  vector<int> val[n + 1];
  vector<int> ind[n + 1];
  set<int> p;
  for (int i = 1; i <= n; i++) {
    val[find(i)].push_back(arr[i].first);
    ind[find(i)].push_back(arr[i].second);
    p.insert(find(i));
  }
  int ans[n + 1];
  set<int> vis;
  for (int i = 1; i <= n; i++) {
    if (p.count(i) && !vis.count(i)) {
      vis.insert(i);
      sort(val[i].rbegin(), val[i].rend());
      int j = 0;
      while (j < val[i].size()) {
        ans[ind[i][j]] = val[i][j];
        j++;
      }
    }
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}
