#include <bits/stdc++.h>
using namespace std;
int id[1000005];
int root(int x) {
  while (x != id[x]) x = id[x];
  return x;
}
void join(int a, int b) {
  int ra = root(a), rb = root(b);
  id[ra] = rb;
}
vector<int> v[1000005];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i <= n; i++) id[i] = i;
  int a[n + 1];
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  int p, q;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &p, &q);
    p--;
    q--;
    if (root(p) != root(q)) join(p, q);
  }
  for (int i = 0; i < n; i++) {
    v[root(i)].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    vector<int> vt;
    for (int j = 0; j < v[i].size(); j++) {
      vt.push_back(a[v[i][j]]);
    }
    sort(vt.rbegin(), vt.rend());
    sort(v[i].begin(), v[i].end());
    for (int j = 0; j < v[i].size(); j++) {
      a[v[i][j]] = vt[j];
    }
  }
  for (int i = 0; i < n; i++) cout << a[i] << " ";
  cout << endl;
}
