#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6 + 7;
vector<vector<int>> node;
vector<vector<int>> pos;
int point[maxN];
int n, m, cnt = 0;
int par[maxN], a[maxN];
int root(int p) {
  if (par[p] == p)
    return p;
  else
    return (par[p] = root(par[p]));
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen("test.inp", "r")) freopen("test.inp", "r", stdin);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    par[i] = i;
  }
  while (m--) {
    int x, y;
    cin >> x >> y;
    int X = root(x);
    int Y = root(y);
    if (X != Y) {
      par[X] = Y;
    }
  }
  memset(point, -1, sizeof point);
  for (int i = 1; i <= n; i++) {
    if (point[root(i)] == -1) {
      point[root(i)] = cnt++;
      node.push_back({a[i]});
      pos.push_back({i});
    } else {
      node[point[root(i)]].push_back(a[i]);
      pos[point[root(i)]].push_back(i);
    }
  }
  for (int i = 0; i < node.size(); i++) {
    sort(node[i].begin(), node[i].end(), greater<int>());
  }
  for (int i = 0; i < node.size(); i++) {
    for (int j = 0; j < node[i].size(); j++) {
      a[pos[i][j]] = node[i][j];
    }
  }
  for (int i = 1; i <= n; i++) cout << a[i] << " ";
}
