import java.io.*;
import java.util.*;

public class roads_ia implements Runnable {

	private final int MAXN = 100000;
	private final int MAXM = 200000;
	private BufferedReader in;
	private PrintWriter out;
	private int n;
	private int m;
	private int[] a;
	private final String defaultMessage = "wrong input format.";
	private int lineNumber;
	private ArrayList<Road>[][] s;
	private ArrayList<Road> ans;
	private int[][] color;
	private int k;
	private int rt;

	public void run() {
		try {
			solve();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	private class Road {
		private int u, v, t, id;

		public Road(int u, int v, int t, int i) {
			this.u = u;
			this.v = v;
			this.t = t;
			this.id = i;
		}
		
		@Override
		public int hashCode() {
			return u * 239 + v * 23 + t * 53;
		}
		
		@Override
		public boolean equals(Object obj) {
			Road that = (Road) obj;
			return (that.u == u) && (that.v == v) && (that.t == t);
		}
	}
	
	private class DSU {
		
		final int[] r;
		final int[] p;

		public DSU(int n) {
			r = new int[n];
			p = new int[n];
			for (int i = 0; i < n; i++) {
				r[i] = 1;
				p[i] = i;
			}
		}

		public int get(int i) {
			if (i == p[i]) {
				return i;
			}
			p[i] = get(p[i]);
			return p[i];
		}

		public void join(int u, int v) {
			if (r[u] < r[v]) {
				int tmp = u;
				u = v;
				v = tmp;
			}
			p[v] = u;
			r[u] += r[v];
		}
		
	}

	@SuppressWarnings("unchecked")
	private void solve() throws IOException {
		in = new BufferedReader(new FileReader("roads.in"));
		out = new PrintWriter("roads.out");
		lineNumber = 0;
		String str = in.readLine();
		lineNumber++;
		StringTokenizer st = new StringTokenizer(str);
		n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		a = new int[2];
		a[0] = Integer.parseInt(st.nextToken());
		a[1] = Integer.parseInt(st.nextToken());
		assertEquality(n + " " + m + " " + a[0] + " " + a[1], str, null);
		assertBounds(n, 1, MAXN, "n");
		assertBounds(m, n - 1, MAXM, "m");
		assertBounds(a[0], 0, n - 1, "a");
		assertBounds(a[1], 0, n - 1, "b");
		assertStatement(a[0] + a[1] == n - 1, "a + b != n - 1.");
		HashSet<Road> roads = new HashSet<Road>(2 * n);
		
		s = new ArrayList[2][n];
		for (int t = 0; t < 2; t++) {
			for (int i = 0; i < n; i++) {
				s[t][i] = new ArrayList<Road>(2);
			}
		}
		
		for (int i = 0; i < m; i++) {
			str = in.readLine();
			lineNumber++;
			st = new StringTokenizer(str);
			int u = Integer.parseInt(st.nextToken());
			int v = Integer.parseInt(st.nextToken());
			int t = Integer.parseInt(st.nextToken());
			assertEquality(u + " " + v + " " + t, str, null);
			assertBounds(u, 1, n, "u[i]");
			assertBounds(v, 1, n, "v[i]");
			assertBounds(t, 0, 1, "t[i]");
			assertStatement(u != v, "u[i] == v[i].");
			if (u > v) {
				int tmp = u;
				u = v;
				v = tmp;
			}
			u--;
			v--;
			Road r = new Road(u, v, t, i + 1);
			assertStatement(!roads.contains(r), "equal roads found.");
			roads.add(r);
			s[t][u].add(r);
			s[t][v].add(r);
		}
		str = in.readLine();
		lineNumber++;
		assertStatement(str == null, null);
		in.close();
		
		ans = new ArrayList<Road>();
		color = new int[2][n];
		DSU[] d = new DSU[2];
		for (rt = 0; rt < 2; rt++) {
			Arrays.fill(color[rt], -1);
			k = 0;
			for (int i = 0; i < n; i++) {
				if (color[rt][i] == -1) {
					dfs(i);
					k++;
				}
			}
			d[rt] = new DSU(k);
		}
		
		DSU dsu = new DSU(n);
		for (Road e : roads) {
			int t = 1 - e.t;
			int u = e.u;
			int v = e.v;
			int ug = d[t].get(color[t][u]);
			int vg = d[t].get(color[t][v]);
			if (ug != vg) {
				d[t].join(ug, vg);
				dsu.join(dsu.get(e.u), dsu.get(e.v));
				ans.add(e);
				a[e.t]--;
			}
		}
		for (int t = 0; t < 2; t++) {
			if (a[t] < 0) {
				noSolution();
			}
		}
		
		for (Road e : roads) {
			if (a[e.t] == 0) {
				continue;
			}
			int u = dsu.get(e.u);
			int v = dsu.get(e.v);
			if (u == v) {
				continue;
			}
			dsu.join(u, v);
			ans.add(e);
			a[e.t]--;
		}
		lineNumber = -1;
		assertStatement((a[0] | a[1]) == 0, "internal error.");
		assertStatement(ans.size() == n - 1, "internal error.");
		
		boolean first = true;
		for (Road e : ans) {
			if (!first) {
				out.print(" ");
			}
			out.print(e.id);
			first = false;
		}
		out.close();
	}

	private void dfs(int u) {
		color[rt][u] = k;
		for (Road e : s[rt][u]) {
			int v = e.u ^ e.v ^ u;
			if (color[rt][v] == -1) {
				dfs(v);
			}
		}
	}

	private void noSolution() {
		out.println("Impossible");
		out.close();
		System.exit(0);
	}

	private void assertBounds(int n, int lo, int hi, String varName) {
		assertStatement(n >= lo && n <= hi, varName + " is out of bounds.");
	}

	private void assertEquality(String s1, String s2, String msg) {
		assertStatement(s1.equals(s2), msg);
	}

	private void assertStatement(boolean st, String msg) {
		if (msg == null) {
			msg = defaultMessage;
		}
		msg = "Line " + lineNumber + ": " + msg;
		if (!st) {
			System.out.println(msg);
			out.println(msg);
			out.close();
			throw new Error(msg);
		}
	}

	public static void main(String[] args) {
		new Thread(new roads_ia()).start();
	}

}
