#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000010;
int a[MAXN], parent[MAXN], rnk[MAXN];
vector<int> arr[MAXN];
vector<int> ans;
void make_set(int v) {
  parent[v] = v;
  rnk[v] = 0;
}
int find_set(int v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (rnk[a] < rnk[b]) swap(a, b);
    parent[b] = a;
    if (rnk[a] == rnk[b]) rnk[a]++;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  ans.clear();
  ans.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    make_set(i);
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    union_sets(u, v);
  }
  for (int i = 0; i < n; i++) {
    int st = find_set(i);
    arr[st].push_back(a[i]);
  }
  for (int i = 0; i < n; i++) {
    sort(arr[i].begin(), arr[i].end());
  }
  for (int i = 0; i < n; i++) {
    int st = find_set(i);
    int x = arr[st].back();
    arr[st].pop_back();
    ans[i] = x;
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
}
