#include <bits/stdc++.h>
using namespace std;
const long long nmax = 1e9 + 7;
const long long Mod = 998244353;
const double PI = 2 * asin(1);
int N, M, P[1000005], Group[1000005], Ans[1000005], Next[1000005];
vector<int> Num[1000005];
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++) scanf("%d", P + i);
  for (int i = 1; i <= N; i++) Group[i] = i;
  for (int i = 0; i < M; i++) {
    int A, B;
    scanf("%d%d", &A, &B);
    int GA = Group[A], GB = Group[B];
    while (GA != Group[GA]) GA = Group[GA];
    while (GB != Group[GB]) GB = Group[GB];
    int G = min(GA, GB);
    Group[A] = G;
    Group[B] = G;
    Group[max(GA, GB)] = G;
  }
  for (int i = 1; i <= N; i++) {
    int G = Group[i];
    while (G != Group[G]) G = Group[G];
    Num[G].push_back(i);
  }
  for (int i = 1; i <= N; i++) {
    if (Num[i].size() == 0) continue;
    int S = Num[i].size();
    for (int j = 0; j < S; j++) {
      Next[j] = P[Num[i][j]];
    }
    sort(Next, Next + S, greater<int>());
    for (int j = 0; j < S; j++) {
      Ans[Num[i][j]] = Next[j];
    }
  }
  for (int i = 1; i <= N; i++) {
    printf("%d", Ans[i]);
    if (i < N)
      printf(" ");
    else
      printf("\n");
  }
  return 0;
}
