#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000005;
long long n, m;
long long a[MAX];
vector<long long> g[MAX];
long long visited[MAX];
vector<long long> k;
void dfs(long long node) {
  visited[node] = 1;
  k.push_back(node);
  for (auto i : g[node]) {
    if (!visited[i]) dfs(i);
  }
}
int main() {
  cin >> n >> m;
  for (long long i = 0; i < n; i++) cin >> a[i + 1];
  for (long long i = 0; i < m; i++) {
    long long x, y;
    cin >> x >> y;
    if (x != y) {
      g[x].push_back(y);
      g[y].push_back(x);
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (!visited[i]) {
      k.clear();
      dfs(i);
      sort(k.begin(), k.end());
      vector<long long> num;
      for (long long i = 0; i < k.size(); i++) num.push_back(a[k[i]]);
      sort(num.begin(), num.end(), greater<long long>());
      for (long long i = 0; i < num.size(); i++) a[k[i]] = num[i];
    }
  }
  for (long long i = 0; i < n; i++) cout << a[i + 1] << " ";
}
