Language: C++


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;

public class Main{
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";

	public static void main(String[] args) throws Exception {
		new Main().run();
	}

	@SuppressWarnings("unchecked")
	void solver() {
		while (true) {
			int n = ni();
			int a = ni();
			int sv = ni();
			int gv = ni();
			if (n == 0 && a == 0 && sv == 0 && gv == 0)
				break;
			int[] src = new int[a];
			int[] dst = new int[a];
			String[] label = new String[a];
			int[] cnt = new int[a + 1];
			for (int i = 0; i < a; i++) {
				src[i] = ni();
				dst[i] = ni();
				label[i] = ns();
				cnt[i + 1] += label[i].length() - 1 + cnt[i];
			}

			ArrayList<Edge>[] g = new ArrayList[n + cnt[a]];
			ArrayList<Edge>[] rev = new ArrayList[n + cnt[a]];
			for (int i = 0; i < n + cnt[a]; i++) {
				g[i] = new ArrayList<>();
				rev[i] = new ArrayList<>();
			}

			for (int i = 0; i < a; i++) {
				for (int j = 0; j < label[i].length(); j++) {
					int s = (j == 0 ? src[i] : n + cnt[i] + j - 1);
					int d = (j == label[i].length() - 1 ? dst[i] : n + cnt[i] + j);
					g[s].add(new Edge(s, d, label[i].charAt(j)));
					rev[d].add(new Edge(d, s, label[i].charAt(j)));
				}
			}

			boolean[] reachable = new boolean[n + cnt[a]];
			ArrayDeque<Integer> que = new ArrayDeque<>();
			que.add(gv);
			while (!que.isEmpty()) {
				int v = que.poll();
				reachable[v] = true;
				for (Edge e : rev[v]) {
					if (!reachable[e.dst]) {
						reachable[e.dst] = true;
						que.add(e.dst);
					}
				}
			}

			if (!reachable[sv]) {
				out.println("NO");
				continue;
			}

			StringBuilder ans = new StringBuilder();
			ArrayDeque<Integer>[] lis = new ArrayDeque[n + cnt[a] + 2];
			for (int i = 0; i < n + cnt[a] + 2; i++)
				lis[i] = new ArrayDeque<>();
			lis[0].add(sv);
			out: for (int i = 0; i < n + cnt[a] + 2; i++) {
				if (i == n + cnt[a] + 1) {
					if (!lis[i].isEmpty())
						ans = new StringBuilder("NO");
					break out;
				}
				boolean[] looked=new boolean[n+cnt[a]];
				char min = '{';
				while (!lis[i].isEmpty()) {
					int v = lis[i].poll();
					if (v == gv)
						break out;
					for (Edge e : g[v]) {
						if (min > e.c && reachable[e.dst]) {
							min = e.c;
							looked=new boolean[n+cnt[a]];
							lis[i + 1].clear();
							lis[i + 1].add(e.dst);
							looked[e.dst]=true;
						} else if (min == e.c && reachable[e.dst]&&!looked[e.dst]) {
							lis[i + 1].add(e.dst);
							looked[e.dst]=true;
						}
					}
				}
				ans.append(min);
			}
			out.println(ans);
		}
	}

	class Edge {
		int src;
		int dst;
		char c;

		public Edge(int src, int dst, char c) {
			this.src = src;
			this.dst = dst;
			this.c = c;
		}
	}

	void run() {
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		solver();
		out.flush();

	}

	static long nl() {
		try {
			long num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	static char nc() {
		try {
			int b = skip();
			if (b == -1)
				return 0;
			return (char) b;
		} catch (IOException e) {
		}
		return 0;
	}

	static double nd() {
		try {
			return Double.parseDouble(ns());
		} catch (Exception e) {
		}
		return 0;
	}

	static String ns() {
		try {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			if (b == -1)
				return "";
			sb.append((char) b);
			while (true) {
				b = is.read();
				if (b == -1)
					return sb.toString();
				if (b <= ' ')
					return sb.toString();
				sb.append((char) b);
			}
		} catch (IOException e) {
		}
		return "";
	}

	public static char[] ns(int n) {
		char[] buf = new char[n];
		try {
			int b = skip(), p = 0;
			if (b == -1)
				return null;
			buf[p++] = (char) b;
			while (p < n) {
				b = is.read();
				if (b == -1 || b <= ' ')
					break;
				buf[p++] = (char) b;
			}
			return Arrays.copyOf(buf, p);
		} catch (IOException e) {
		}
		return null;
	}

	public static byte[] nse(int n) {
		byte[] buf = new byte[n];
		try {
			int b = skip();
			if (b == -1)
				return null;
			is.read(buf);
			return buf;
		} catch (IOException e) {
		}
		return null;
	}

	static int skip() throws IOException {
		int b;
		while ((b = is.read()) != -1 && !(b >= 33 && b <= 126))
			;
		return b;
	}

	static boolean eof() {
		try {
			is.mark(1000);
			int b = skip();
			is.reset();
			return b == -1;
		} catch (IOException e) {
			return true;
		}
	}

	static int ni() {
		try {
			int num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	static void tr(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
}

Compiler output:
a.cc:19:9: error: stray '@' in program
   19 |         @SuppressWarnings("unchecked")
      |         ^
a.cc:2:1: error: 'import' does not name a type
    2 | import java.io.ByteArrayInputStream;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:3:1: error: 'import' does not name a type
    3 | import java.io.IOException;
      | ^~~~~~
a.cc:3:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:4:1: error: 'import' does not name a type
    4 | import java.io.InputStream;
      | ^~~~~~
a.cc:4:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:5:1: error: 'import' does not name a type
    5 | import java.io.PrintWriter;
      | ^~~~~~
a.cc:5:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:6:1: error: 'import' does not name a type
    6 | import java.util.ArrayDeque;
      | ^~~~~~
a.cc:6:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:7:1: error: 'import' does not name a type
    7 | import java.util.ArrayList;
      | ^~~~~~
a.cc:7:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:8:1: error: 'import' does not name a type
    8 | import java.util.Arrays;
      | ^~~~~~
a.cc:8:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:10:1: error: expected unqualified-id before 'public'
   10 | public class Main{
      | ^~~~~~