#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
vector<int> g[N], pos1, pos2;
int n, m, p[N], x, y, ans[N];
bool used[N];
void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (!used[to]) {
      dfs(to);
    }
  }
  pos1.push_back(v);
  pos2.push_back(p[v]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      dfs(i);
      sort(pos1.rbegin(), pos1.rend());
      sort(pos2.begin(), pos2.end());
      for (int j = 0; j < pos1.size(); j++) {
        ans[pos1[j]] = pos2[j];
      }
      pos1.clear();
      pos2.clear();
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
}
