#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > AdjList;
bool visited[1000005];
int arr[1000005];
vector<pair<int, int> > elem;
void dfs(int u) {
  visited[u] = true;
  elem.push_back(pair<int, int>(arr[u], u));
  for (int i = 0; i < AdjList[u].size(); i++) {
    int v = AdjList[u][i];
    if (!visited[v]) {
      dfs(v);
    }
  }
}
bool comp1(pair<int, int> x, pair<int, int> y) { return x.first < y.first; }
bool comp2(pair<int, int> x, pair<int, int> y) { return x.second < y.second; }
int ans[1000005];
int main(int argc, char** argv) {
  int n, m;
  scanf("%d", &n);
  AdjList.resize(n + 1);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d", &x);
    scanf("%d", &y);
    AdjList[x].push_back(y);
    AdjList[y].push_back(x);
  }
  memset(visited, 0, sizeof visited);
  for (int i = 1; i <= n; i++) {
    elem.clear();
    if (!visited[i]) {
      dfs(i);
      sort(elem.begin(), elem.end(), comp2);
      vector<pair<int, int> > hp = elem;
      sort(elem.begin(), elem.end(), comp1);
      for (int i = 0; i < elem.size(); i++)
        ans[hp[i].second] = elem[elem.size() - i - 1].first;
    }
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
  return 0;
}
