#include <bits/stdc++.h>
using namespace std;
void readi(int &x) {
  int v = 0, f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = v * 10 + c - '0';
  while (isdigit(c = getchar())) v = v * 10 + c - '0';
  x = v * f;
}
void readll(long long &x) {
  long long v = 0ll, f = 1ll;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = v * 10 + c - '0';
  while (isdigit(c = getchar())) v = v * 10 + c - '0';
  x = v * f;
}
void readc(char &x) {
  char c;
  while ((c = getchar()) == ' ')
    ;
  x = c;
}
void writes(string s) { puts(s.c_str()); }
void writeln() { writes(""); }
void writei(int x) {
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
void writell(long long x) {
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
int n, m, i, j, f[1000005], p[1000005];
vector<int> s[1000005], t[1000005];
int find(int x) {
  if (f[x] == x) return x;
  return f[x] = find(f[x]);
}
int main() {
  readi(n);
  readi(m);
  for (i = 1; i <= n; i++) {
    readi(p[i]);
    f[i] = i;
  }
  for (i = 1; i <= m; i++) {
    int x, y;
    readi(x);
    readi(y);
    if (find(x) != find(y)) {
      f[find(x)] = find(y);
    }
  }
  for (i = 1; i <= n; i++) {
    s[find(i)].push_back(i);
    t[find(i)].push_back(p[i]);
  }
  for (i = 1; i <= n; i++) {
    if (!t[i].empty()) {
      stable_sort((t[i]).begin(), (t[i]).end(), greater<int>());
      for (j = 0; j < s[i].size(); j++) {
        p[s[i][j]] = t[i][j];
      }
    }
  }
  for (i = 1; i <= n; i++) writei(p[i]), printf(" ");
  return 0;
}
