#include <bits/stdc++.h>
using namespace std;
vector<int> num, con, a;
vector<vector<int> > g(1000001);
bool used[1000001];
void dfs(int v) {
  num.push_back(v);
  con.push_back(a[v - 1]);
  used[v] = true;
  if (g[v].empty()) return;
  for (int i = 0; i < g[v].size(); i++)
    if (!used[g[v][i]]) dfs(g[v][i]);
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  int x, y;
  a.assign(n, 0);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    num.resize(0);
    con.resize(0);
    if (used[i]) continue;
    dfs(a[i - 1]);
    sort(num.begin(), num.end());
    sort(con.rbegin(), con.rend());
    for (int j = 0; j < num.size(); j++) {
      a[num[j] - 1] = con[j];
    }
  }
  for (int i = 0; i < n; i++) cout << a[i] << ' ';
  return 0;
}
