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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 

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
    private static Scanner sc;
    private static Printer pr;
    static long along=(int )1e9+7;
    static TreeSet<Integer>[]list;
    static TreeSet<Integer>set=new TreeSet<>();
    static boolean[]vis1;
    static boolean[]vis2;
    static int []input;
    private static void solve() throws IOException {
        //in the name of god
        //you are anything and we are nothing
        int n=sc.nextInt(),m=sc.nextInt();
        list=new TreeSet[n+1];
        input=new int[n+1];
        for (int i=1;i<=n;++i)
            input[i]=sc.nextInt();
        for (int i=1;i<=n;++i)
            list[i]=new TreeSet<>();
        vis1=new boolean[n+1];
        vis2=new boolean[n+1];
        while (m-->0){
            int u=sc.nextInt(),v=sc.nextInt();
            list[u].add(v);
            list[v].add(u);
        }
        for (int i=1;i<=n;++i){
            if (!vis1[i])
                dfs_1(i);
            if (!vis2[i])
                dfs_2(i);
        }
        for (int i=1;i<=n;++i)
            pr.print(input[i]+" ");
    }
    public static void dfs_1(int src){
        vis1[src]=true;
        set.add(src);
        for (int v:list[src]){
            if (!vis1[v])
                dfs_1(v);
        }
    }
    public static void dfs_2(int src){
        vis2[src]=true;
        input[src]=set.pollLast();
        for (int v:list[src]){
            if (!vis2[v])
                dfs_2(v);
        }
    }
    public static int[] radixSort(int[] f) {
        int[] to = new int[f.length];
        {
            int[] b = new int[65537];
            for (int i = 0; i < f.length; i++) b[1 + (f[i] & 0xffff)]++;
            for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
            for (int i = 0; i < f.length; i++) to[b[f[i] & 0xffff]++] = f[i];
            int[] d = f;
            f = to;
            to = d;
        }
        {
            int[] b = new int[65537];
            for (int i = 0; i < f.length; i++) b[1 + (f[i] >>> 16)]++;
            for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
            for (int i = 0; i < f.length; i++) to[b[f[i] >>> 16]++] = f[i];
            int[] d = f;
            f = to;
            to = d;
        }
        return f;
    }
    public static int val(char c){
        return c-'0';
    }
    public static long gcd(long a,long b) {
        if (a == 0) return b;
        return gcd(b % a, a);
    }
    public static void main(String[] args) throws IOException {
        sc = new Scanner(System.in);
        pr = new Printer(System.out);
        solve();

        pr.close();
        // sc.close();
    }
    private static class Scanner {
        BufferedReader br;

        Scanner (InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }

        private boolean isPrintable(int ch) {
            return ch >= '!' && ch <= '~';
        }

        private boolean isCRLF(int ch) {
            return ch == '\n' || ch == '\r' || ch == -1;
        }

        private int nextPrintable() {
            try {
                int ch;
                while (!isPrintable(ch = br.read())) {
                    if (ch == -1) {
                        throw new NoSuchElementException();
                    }
                }

                return ch;
            } catch (IOException e) {
                throw new NoSuchElementException();
            }
        }

        String next() {
            try {
                int ch = nextPrintable();
                StringBuilder sb = new StringBuilder();
                do {
                    sb.appendCodePoint(ch);
                } while (isPrintable(ch = br.read()));

                return sb.toString();
            } catch (IOException e) {
                throw new NoSuchElementException();
            }
        }

        int nextInt() {
            try {
                // parseInt from Integer.parseInt()
                boolean negative = false;
                int res = 0;
                int limit = -Integer.MAX_VALUE;
                int radix = 10;

                int fc = nextPrintable();
                if (fc < '0') {
                    if (fc == '-') {
                        negative = true;
                        limit = Integer.MIN_VALUE;
                    } else if (fc != '+') {
                        throw new NumberFormatException();
                    }
                    fc = br.read();
                }
                int multmin = limit / radix;

                int ch = fc;
                do {
                    int digit = ch - '0';
                    if (digit < 0 || digit >= radix) {
                        throw new NumberFormatException();
                    }
                    if (res < multmin) {
                        throw new NumberFormatException();
                    }
                    res *= radix;
                    if (res < limit + digit) {
                        throw new NumberFormatException();
                    }
                    res -= digit;

                } while (isPrintable(ch = br.read()));

                return negative ? res : -res;
            } catch (IOException e) {
                throw new NoSuchElementException();
            }
        }

        long nextLong() {
            try {
                // parseLong from Long.parseLong()
                boolean negative = false;
                long res = 0;
                long limit = -Long.MAX_VALUE;
                int radix = 10;

                int fc = nextPrintable();
                if (fc < '0') {
                    if (fc == '-') {
                        negative = true;
                        limit = Long.MIN_VALUE;
                    } else if (fc != '+') {
                        throw new NumberFormatException();
                    }
                    fc = br.read();
                }
                long multmin = limit / radix;

                int ch = fc;
                do {
                    int digit = ch - '0';
                    if (digit < 0 || digit >= radix) {
                        throw new NumberFormatException();
                    }
                    if (res < multmin) {
                        throw new NumberFormatException();
                    }
                    res *= radix;
                    if (res < limit + digit) {
                        throw new NumberFormatException();
                    }
                    res -= digit;

                } while (isPrintable(ch = br.read()));

                return negative ? res : -res;
            } catch (IOException e) {
                throw new NoSuchElementException();
            }
        }

        float nextFloat() {
            return Float.parseFloat(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            try {
                int ch;
                while (isCRLF(ch = br.read())) {
                    if (ch == -1) {
                        throw new NoSuchElementException();
                    }
                }
                StringBuilder sb = new StringBuilder();
                do {
                    sb.appendCodePoint(ch);
                } while (!isCRLF(ch = br.read()));

                return sb.toString();
            } catch (IOException e) {
                throw new NoSuchElementException();
            }
        }

        void close() {
            try {
                br.close();
            } catch (IOException e) {
//				throw new NoSuchElementException();
            }
        }
    }
    private static class Printer extends PrintWriter {
        Printer(PrintStream out) {
            super(out);
        }
    }
}