#include <bits/stdc++.h>
using namespace std;
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int n, k, ar[2000005];
vector<int> vec[2000005];
int w[2000005];
int get(int x) {
  if (w[x] == x) return x;
  return w[x] = get(w[x]);
}
void merge(int a, int b) {
  a = get(a);
  b = get(b);
  w[a] = b;
}
int ans[2000005];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  for (int i = 1; i <= n; i++) w[i] = i;
  for (int i = 1; i <= k; i++) {
    int a, b;
    cin >> a >> b;
    merge(a, b);
  }
  for (int i = 1; i <= n; i++) {
    int id = get(i);
    vec[id].push_back(ar[i]);
  }
  for (int i = 1; i <= n; i++) sort(vec[i].begin(), vec[i].end());
  for (int i = 1; i <= n; i++) {
    int id = get(i);
    int val = vec[id].back();
    vec[id].pop_back();
    ans[i] = val;
  }
  for (int i = 1; i <= n; i++) {
    if (i > 1) cout << " ";
    cout << ans[i];
  }
  cout << endl;
  cin.get();
  cin.get();
  return 0;
}
