#include <bits/stdc++.h>
using namespace std;
vector<int> v[1000000];
int vis[1000000];
int a[1000000];
vector<int> num;
vector<int> idx;
int j;
void dfs(int node) {
  vis[node] = j;
  num.push_back(a[node]);
  idx.push_back(node);
  int i;
  for (i = 0; i < v[node].size(); i++) {
    int u = v[node][i];
    if (vis[u] > j) dfs(u);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int ans[n];
  int i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    ans[i] = a[i];
    vis[i] = INT_MAX;
  }
  int x, y;
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  for (i = 0; i < n; i++) {
    if (vis[i] > j) {
      num.clear();
      idx.clear();
      dfs(i);
      sort(num.begin(), num.end());
      sort(idx.begin(), idx.end());
      reverse(num.begin(), num.end());
      int k;
      for (k = 0; k < num.size(); k++) {
        ans[idx[k]] = num[k];
      }
      j++;
    }
  }
  for (i = 0; i < n; i++) cout << ans[i] << " ";
  return 0;
}
