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5 1
2 2
2 3
 83 8
54 3 52 12 61 36 65 62 69 49 47 77 31 15 21 14 73 29 6 26 37 17 81 75 43 30 58 76 16 8 11 5 27 35 7 66 50 67 2 39 45 28 60 71 38 82 53 1 42 13 44 72 22 4 9 25 19 57 10 70 18 68 32 34 20 80 23 79 24 63 64 51 59 41 74 48 40 33 46 83 55 56 78
48 80
1 8
71 54
15 59
72 46
36 9
64 29
55 58
 1 1
1
1 1
 5 20
3 4 1 5 2
2 4
3 1
2 2
4 5
4 5
5 2
1 1
2 4
3 3
4 2
3 1
1 1
5 2
1 3
3 1
4 2
1 3
3 3
4 2
4 2
 6 20
4 6 1 3 2 5
2 2
6 5
3 4
3 4
5 6
3 3
5 5
6 6
4 3
2 2
2 2
5 2
3 4
1 4
5 2
4 3
2 5
1 4
3 1
4 3
 2 10
1 2
1 1
2 2
2 2
1 1
1 1
2 2
2 2
1 1
2 2
1 1
 5 3
5 2 3 4 1
2 4
1 4
3 4
 4 1
4 3 1 2
3 4
 3 1
1 3 2
1 2
 2 10
2 1
2 1
1 2
1 1
2 1
1 1
2 1
1 1
1 1
2 1
2 1
 3 1
2 3 1
1 1
 8 1
3 4 1 2 7 8 5 6
3 4
 3 10
1 2 3
2 2
1 1
2 2
3 3
1 1
3 3
3 3
3 3
2 2
1 1

3 2 1 
 6 2 5 7 3 1 4 
 4 2 3 1 
 9 8 6 7 5 3 4 2 1 
 7 6 5 4 3 2 1 
 3 2 1 
 8 4 7 2 6 5 3 1 
 2 3 1 
 4 12 7 1 16 19 3 10 14 8 13 2 11 9 20 5 18 17 6 15 
 6 8 9 4 5 1 7 2 3 
 5 4 3 2 1 
 3 4 1 2 
 5 3 2 4 1 6 
 62 3 52 12 61 36 65 54 69 49 47 77 31 15 21 14 73 29 6 26 37 17 81 75 43 30 58 76 34 8 11 5 27 35 7 66 50 67 2 39 45 28 60 71 38 82 53 83 42 13 44 72 22 64 57 25 19 9 10 70 18 68 32 16 20 80 23 79 24 63 4 51 59 41 74 48 40 33 46 1 55 56 78 
 1 
 3 5 1 4 2 
 4 6 3 1 5 2 
 1 2 
 5 4 3 2 1 
 4 3 2 1 
 3 1 2 
 2 1 
 2 3 1 
 3 4 2 1 7 8 5 6 
 1 2 3 

#include <bits/stdc++.h>
using namespace std;
inline int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
inline int lcm(int a, int b) { return (a * (b / gcd(a, b))); }
inline long long int bigmod(long long int a, long long int b, long long int m) {
  if (b == 0) return 1 % m;
  long long int x = bigmod(a, b / 2, m);
  x = (x * x) % m;
  if (b % 2 == 1) {
    x = (x * a) % m;
  }
  return x;
}
inline long long int phi(long long int n) {
  long long int ret = n;
  for (long long int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) n /= i;
    }
    ret -= ret / i;
  }
  if (n > 1) {
    ret -= ret / n;
  }
  return ret;
}
long long int missing_number(long long int A[], long long int n) {
  long long int x1 = A[0], x2 = 1;
  for (int i = 1; i < n; i++) x1 ^= A[i];
  for (int i = 2; i <= n + 1; i++) x2 ^= i;
  return (x1 ^ x2);
}
bool isFibo(long long int n) {
  long long int A = (5 * n * n + 4);
  long long int B = (5 * n * n - 4);
  long long int C = sqrt(A);
  long long int D = sqrt(B);
  return (A == (C * C) || B == (D * D));
}
long long int classini(long long int n) { return (n & 1) ? -1 : 1; }
long long int factorial(int n) {
  return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
long long int catanal(long long int n) {
  return (factorial(2 * n) / ((factorial(n + 1) * factorial(n))));
}
string orientation_3_ordered_point(vector<pair<int, int> > V) {
  long long int value =
      (V[1].second - V[0].second) * (V[2].first - V[1].first) -
      (V[1].first * V[0].first) * (V[2].second - V[1].second);
  string res;
  if (value == 0) res = "colinear";
  value == 1 ? res = "clockwise" : res = "counterclockwise";
  return res;
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1};
string make_string(long long int num) {
  stringstream ss;
  ss << num;
  string s;
  ss >> s;
  return s;
}
long long int make_num(string s) {
  stringstream ss;
  long long int tmp;
  ss << s;
  ss >> tmp;
  return tmp;
}
int SET(int mask, int pos) { return (mask | (1 << pos)); }
int check(int mask, int pos) { return (mask & (1 << pos)); }
int Rank[1000011], parent[1000011];
void Build() {
  for (int i = 1; i < 1000011; i++) {
    parent[i] = i;
    Rank[i] = 0;
  }
}
int Find(int x) {
  if (x == parent[x]) return x;
  return parent[x] = Find(parent[x]);
}
void Union(int u, int v) {
  int U = Find(u);
  int V = Find(v);
  if (U == V) return;
  if (Rank[U] > Rank[V]) {
    parent[V] = U;
  } else
    parent[U] = V;
  if (Rank[U] == Rank[V]) Rank[V] += 1;
}
bool Same(int u, int v) { return Find(u) == Find(v); }
int N, M;
int Ans[1000011];
int Arr[1000011];
vector<int> Index[1000011], Ele[1000011];
int F[1000011];
int main() {
  Build();
  scanf("%d", &N), scanf("%d", &M);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &Arr[i]);
  }
  set<int> P;
  while (M--) {
    int u, v;
    scanf("%d", &u), scanf("%d", &v);
    if (Same(Arr[u], Arr[v]))
      continue;
    else {
      Union(Arr[u], Arr[v]);
      int node = parent[Arr[u]];
      P.insert(node);
      if (F[u] == 0) {
        Index[node].push_back(u);
        Ele[node].push_back(Arr[u]);
      }
      if (F[v] == 0) {
        Index[node].push_back(v);
        Ele[node].push_back(Arr[v]);
      }
      F[u] = 1;
      F[v] = 1;
    }
  }
  for (auto x : P) {
    int num = x;
    sort(Index[num].begin(), Index[num].end());
    sort(Ele[num].begin(), Ele[num].end());
    int sz = Index[num].size();
    int i = 0;
    for (int j = sz - 1; j >= 0; j--) {
      Ans[Index[num][i]] = Ele[num][j];
      i++;
    }
  }
  for (int i = 1; i <= N; i++) {
    if (Ans[i] == 0)
      printf("%d ", Arr[i]);
    else
      printf("%d ", Ans[i]);
  }
  printf("\n");
  return 0;
}
