#include <bits/stdc++.h>
using namespace std;
int nums[1000010], dsu[1000010], n, m;
vector<int> parts[1000010];
vector<int> idxs[1000010];
int get_par(int pos) {
  if (dsu[pos] == pos) return pos;
  int r = get_par(dsu[pos]);
  dsu[pos] = r;
  return r;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) dsu[i] = i;
  for (int i = 0; i < n; i++) {
    scanf("%d", nums + i);
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    a = get_par(a);
    b = get_par(b);
    dsu[a] = b;
  }
  for (int i = 0; i < n; i++) {
    parts[get_par(i)].push_back(nums[i]);
    idxs[get_par(i)].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    sort(parts[i].begin(), parts[i].end(), greater<int>());
    for (int j = 0; j < parts[i].size(); j++) {
      nums[idxs[i][j]] = parts[i][j];
    }
  }
  for (int i = 0; i < n; i++) printf("%d ", nums[i]);
  printf("\n");
}
