#include <bits/stdc++.h>
using namespace std;
int a[1111111];
vector<int> g[1111111];
int c[1111111];
vector<int> nums[1111111];
vector<int> ids[1111111];
void dfs(int v, int cc) {
  c[v] = cc;
  for (int i : g[v]) {
    if (c[i] == -1) {
      dfs(i, cc);
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x;
    --y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  memset(c, -1, sizeof(c));
  int cc = 0;
  for (int i = 0; i < n; i++) {
    if (c[i] == -1) {
      dfs(i, cc++);
    }
  }
  for (int i = 0; i < n; i++) {
    nums[c[i]].push_back(a[i]);
    ids[c[i]].push_back(i);
  }
  for (int i = 0; i < cc; i++) {
    sort(nums[i].rbegin(), nums[i].rend());
    for (int j = 0; j < nums[i].size(); j++) {
      a[ids[i][j]] = nums[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    printf("%d ", a[i]);
  }
  return 0;
}
