#include <bits/stdc++.h>
using namespace std;
const int N = 1000009;
vector<long long int> v[N], v1, v2;
bool check[N];
long long int a[N];
void DFS(int x) {
  check[x] = true;
  for (int i = 0; i < v[x].size(); i++)
    if (!check[v[x][i]]) {
      v1.push_back(v[x][i]);
      v2.push_back(a[v[x][i]]);
      DFS(v[x][i]);
    }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long int n, m, j, k;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) {
    cin >> j >> k;
    v[j].push_back(k);
    v[k].push_back(j);
  }
  for (int i = 1; i <= n; i++) {
    if (!check[i]) {
      v1.push_back(i);
      v2.push_back(a[i]);
      DFS(i);
    }
    sort(v2.rbegin(), v2.rend());
    sort(v1.begin(), v1.end());
    for (int j = 0; j < v2.size(); j++) a[v1[j]] = v2[j];
    v1.clear();
    v2.clear();
  }
  for (int i = 1; i <= n; i++) cout << a[i] << ' ';
  return cout << endl, 0;
}
