#include <bits/stdc++.h>
using namespace std;
int nextInt() {
  int x = 0;
  int c;
  while ('0' > (c = getchar()))
    ;
  x = c - '0';
  while ('0' <= (c = getchar())) x = (x << 1) + (x << 3) + c - '0';
  return x;
}
void putInt(int x) {
  static char buf[22] = {};
  if (x == 0) {
    putchar('0');
    return;
  }
  int i = 22;
  do buf[--i] = x % 10 + '0';
  while (x /= 10);
  while (i < 22) putchar(buf[i++]);
}
const int INF = (int)1e9;
const double PI = 2 * acos(0.0);
const double eps = 1e-9;
const int NPOS = -1;
const int N = 1000009;
int n, m, arr[N], x, y;
vector<int> sorted, numbers;
vector<int> adj[N];
bool vis[N];
void dfs(int s) {
  vis[s] = true;
  numbers.push_back(s);
  sorted.push_back(arr[s]);
  for (int u : adj[s]) {
    if (vis[u]) continue;
    dfs(u);
  }
}
void do_the_thing() {
  sort(((sorted).begin()), ((sorted).end()), greater<int>());
  sort(((numbers).begin()), ((numbers).end()));
  for (int i = int(0); i < int(numbers.size()); ++i)
    arr[numbers[i]] = sorted[i];
  sorted.clear();
  numbers.clear();
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  n = nextInt();
  m = nextInt();
  for (int i = int(1); i < int(n + 1); ++i) arr[i] = nextInt();
  for (int i = int(0); i < int(m); ++i) {
    x = nextInt();
    y = nextInt();
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (int i = int(1); i < int(n + 1); ++i) {
    if (vis[i]) continue;
    dfs(i);
    do_the_thing();
  }
  for (int i = int(1); i < int(n + 1); ++i) putInt(arr[i]), putchar(' ');
  return 0;
}
