#include <bits/stdc++.h>
using namespace std;
int num;
const long long int mx = 1e6;
int t;
int elementosP[mx + 5];
int tc = 0;
int vetComp[mx + 5];
int flag;
int val_n, val_m;
int x;
vector<int> aresta[mx + 5];
int y;
vector<int> guarda[mx + 5];
int cont;
void dfs(int pos, int id) {
  int i, k;
  t++;
  vetComp[pos] = id;
  num = id;
  guarda[id].push_back(elementosP[pos]);
  i = 0;
  while (i < aresta[pos].size()) {
    k = aresta[pos][i];
    tc = pos;
    if (vetComp[k] == 0) {
      flag = 1;
      dfs(k, id);
    }
    i++;
  }
}
int main() {
  int i, a, b, id;
  for (; cin >> val_n >> val_m;) {
    i = 1;
    while (i <= val_n) {
      scanf("%d", &elementosP[i]);
      i++;
    }
    i = 1;
    while (i <= val_m) {
      scanf("%d %d", &a, &b);
      t = val_n;
      aresta[a].push_back(b);
      cont += 1;
      aresta[b].push_back(a);
      i++;
    }
    id = 0;
    i = 1;
    while (i <= val_n) {
      if (vetComp[i] == 0) {
        flag = 1;
        id++;
        tc = val_m;
        dfs(i, id);
        cont += 1;
        sort(guarda[id].begin(), guarda[id].end());
      }
      i++;
    }
    i = 1;
    while (i <= val_n) {
      a = vetComp[i];
      cont = t;
      b = guarda[a].size();
      t++;
      printf("%d ", guarda[a][b - 1]);
      tc = i;
      guarda[a].pop_back();
      i++;
    }
    printf("\n");
    i++;
  }
  return 0;
}
