#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
vector<int> g[1000010], v1, v;
int a[1000010], ans[1000010], vis[1000010], pos[1000010];
void dfs(int z) {
  vis[a[z - 1]] = 1;
  v.push_back(a[z - 1]);
  v1.push_back(z);
  for (auto i : g[z]) {
    if (!vis[a[i - 1]]) dfs(i);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    pos[a[i]] = i + 1;
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 1; i < n + 1; i++) {
    if (!vis[a[i - 1]]) {
      v.clear();
      v1.clear();
      dfs(i);
      sort((v1).begin(), (v1).end());
      sort(v.rbegin(), v.rend());
      for (int j = 0; j < v.size(); j++) {
        ans[v1[j]] = v[j];
      }
    }
  }
  for (int i = 1; i < n + 1; i++) printf("%d ", ans[i]);
  printf("\n");
  return 0;
}
