#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int p[n];
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  vector<int> v[n];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    v[x - 1].push_back(y - 1);
    v[y - 1].push_back(x - 1);
  }
  int vis[n];
  for (int i = 0; i < n; i++) {
    vis[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) {
      queue<int> q;
      q.push(i);
      vis[i] = 1;
      priority_queue<int> q1;
      priority_queue<int, vector<int>, greater<int> > q2;
      while (!q.empty()) {
        int x = q.front();
        q.pop();
        q1.push(p[x]);
        q2.push(x);
        for (int j = 0; j < v[x].size(); j++) {
          if (vis[v[x][j]] == 0) {
            q.push(v[x][j]);
            vis[v[x][j]] = 1;
          }
        }
      }
      while (!q1.empty()) {
        p[q2.top()] = q1.top();
        q1.pop();
        q2.pop();
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << p[i];
    if (i != n - 1) {
      cout << " ";
    }
  }
  cout << endl;
  return 0;
}
