import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class roads_dp implements Runnable {

    private BufferedReader in;
    private PrintWriter out;
    private ArrayList<Integer> answer;
    private ArrayList<Edge> edges0;
    private ArrayList<Edge> edges1;
    private StringTokenizer st;

    public void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        int cnt0 = nextInt();
        int cnt1 = nextInt();
        assert 1 <= n && n <= 100000;
        assert n - 1 <= m && m <= 200000;
        assert 0 <= cnt0 && 0 <= cnt1 && cnt0 + cnt1 == n - 1;
        edges0 = new ArrayList<Edge>();
        edges1 = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge edge = new Edge(i, nextInt() - 1, nextInt() - 1);
            assert edge.v1 != edge.v2;
            int t = nextInt();
            if (t == 0) {
                edges0.add(edge);
            } else {
                edges1.add(edge);
            }
        }
        
        SNM snm = new SNM(n);
        int count0 = 0;
        for (Edge edge: edges0) {
            if (snm.join(edge.v1, edge.v2)) {
                count0++;
            }
        }
        SNM snm1 = new SNM(n);
        int count1 = 0;
        answer = new ArrayList<Integer>();
        for (Edge edge: edges1) {
            if (snm.join(edge.v1, edge.v2)) {
                if (snm1.join(edge.v1, edge.v2)); {
                    count1++;
                    answer.add(edge.id);
                }
            }
        }
        assert count0 + count1 == n - 1;
        for (Edge edge: edges1) {
            if (count1 >= cnt1) {
                break;
            }
            if (snm1.join(edge.v1, edge.v2)) {
                count1++;
                answer.add(edge.id);
            }
        }
        if (count1 != cnt1) {
            out.println("Impossible");
            return;
        }
        for (Edge edge: edges0) {
            if (snm1.join(edge.v1, edge.v2)) {
                answer.add(edge.id);
            }
        }
        for (int x: answer) {
            out.print((x + 1) + " ");
        }
    }
    
    class SNM {
        private int[] par;
        private int[] rang;
        
        SNM(int n) {
            par = new int[n];
            rang = new int[n];
            for (int i = 0; i < n; i++) {
                par[i] = i;
            }
        }
        
        private boolean join(int v1, int v2) {
            v1 = find(v1);
            v2 = find(v2);
            if (v1 == v2) {
                return false;
            }
            if (rang[v1] <= rang[v2]) {
                par[v1] = v2;
                rang[v2] = Math.max(rang[v2], rang[v1] + 1);
            } else {
                par[v2] = v1;
            }
            return true;
        }

        private int find(int v) {
            return (v == par[v]) ? v : (par[v] = find(par[v]));
        }
        
        public String toString() {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < par.length; i++) {
                if (sb.length() > 0) {
                    sb.append(" ");
                }
                sb.append(find(i));
            }
            return sb.toString();
        }
    }
    
    class Edge {
        int id;
        int v1;
        int v2;
        
        Edge(int id, int v1, int v2) {
            this.id = id;
            this.v1 = v1;
            this.v2 = v2;
        }
    }
    
    public void run() {
        try {
            solve();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(239);
        } finally {
            out.close();
        }
    }

    public roads_dp(String file) {
        try {
            in = new BufferedReader(new FileReader(new File(file + ".in")));
            st = new StringTokenizer(in.readLine());
            out = new PrintWriter(new File(file + ".out"));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(239);
        }
    }

    public String nextToken() throws IOException {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }
    
    public int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }
    
    public static void main(String[] args) {
        new Thread(new roads_dp("roads")).start();
    }
}
