#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, m;
int a[N];
int col[N], color = 0;
vector<int> g[N];
vector<int> pos[N];
vector<int> num[N];
void paint(int v) {
  col[v] = color;
  pos[color].push_back(v);
  num[color].push_back(a[v]);
  for (int i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (col[to] == -1) paint(to);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  memset(col, -1, sizeof col);
  for (int i = 1; i <= n; ++i)
    if (col[i] == -1) {
      ++color;
      paint(i);
    }
  for (int i = 1; i <= color; ++i) {
    sort(pos[i].begin(), pos[i].end());
    sort(num[i].begin(), num[i].end());
    reverse(num[i].begin(), num[i].end());
    for (int j = 0; j < pos[i].size(); ++j) {
      int index = pos[i][j];
      a[index] = num[i][j];
    }
  }
  for (int i = 1; i <= n; ++i) printf("%d ", a[i]);
  return 0;
}
