#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long a = 0, b = getchar(), c = 1;
  while (!isdigit(b)) c = b == '-' ? -1 : 1, b = getchar();
  while (isdigit(b)) a = a * 10 + b - '0', b = getchar();
  return a * c;
}
priority_queue<int> q[1000005];
int n, m, a[1000005], f[1000005];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++) a[i] = read(), f[i] = i;
  for (int i = 0; i < m; i++) {
    int x = read(), y = read();
    f[find(x)] = find(y);
  }
  for (int i = 1; i <= n; i++) find(i), q[f[i]].push(a[i]);
  for (int i = 1; i <= n; i++) printf("%d ", q[f[i]].top()), q[f[i]].pop();
  return 0;
}
