#include <bits/stdc++.h>
using namespace std;
long long int n, arr[1000005], sz[1000005], fav[1000005], ans[1000005];
priority_queue<int> v[1000005];
void initi() {
  for (int i = 1; i <= n; i++) {
    arr[i] = i;
    sz[i] = 1;
  }
}
int root(int a) {
  int x = arr[a];
  while (x != arr[x]) {
    x = arr[x];
  }
  return x;
}
void merge(int a, int b) {
  int x = root(a);
  int y = root(b);
  if (sz[y] > sz[x]) swap(x, y);
  arr[y] = x;
  sz[x] += sz[y];
  sz[y] = 0;
}
bool find(int a, int b) {
  if (root(a) == root(b))
    return true;
  else
    return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long i, j, k, m, x, y;
  cin >> n >> m;
  initi();
  for (i = 1; i <= n; i++) {
    cin >> ans[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> x >> y;
    if (!(find(x, y))) {
      merge(x, y);
    }
  }
  for (i = 1; i <= n; i++) {
    x = root(i);
    v[x].push(ans[i]);
  }
  for (i = 1; i <= n; i++) {
    x = root(i);
    y = root(x);
    j = v[y].top();
    fav[i] = j;
    v[y].pop();
    cout << j << " ";
  }
}
