Problem: Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character Ai in one of the strings into arbitrary character Bi, but he has to pay for every move a particular sum of money, equal to Wi. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings. 

Input

The first input line contains two initial non-empty strings s and t, consisting of lower case Latin letters. The length of each string doesn't exceed 105. The following line contains integer n (0 ≤ n ≤ 500) — amount of possible changings. Then follow n lines, each containing characters Ai and Bi (lower case Latin letters) and integer Wi (0 ≤ Wi ≤ 100), saying that it's allowed to change character Ai into character Bi in any of the strings and spend sum of money Wi.

Output

If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any.

Examples

Input

uayd
uxxd
3
a x 8
x y 13
d c 3


Output

21
uxyd


Input

a
b
3
a b 2
a b 3
b a 5


Output

2
b


Input

abc
ab
6
a b 4
a b 7
b a 8
c b 11
c a 3
a c 0


Output

-1

Solution (in JAVA):

Solution:
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Locale;

public class B33 
{
	public static final int INF = 1000000000;
	public static void main(String[] args) throws IOException 
	{
		setStandardIO();
		//setFileIO("test");		
		
		String sa = nextString();
		String sb = nextString();
		
		if (sa.length() != sb.length())
			println(-1);
		else {
			int n = 'z' - 'a' + 1;			
			int adj[][] = new int[n][n];
			
			for (int i = 0; i < n; i++)
				for (int j = 0; j < n; j++)
					if (i != j) adj[i][j] = INF;
			
			int m = nextInt();
			for (int i = 0; i < m; i++) {
				int u = nextChar() - 'a';
				int v = nextChar() - 'a';
				int w = nextInt();
				if (adj[u][v] >= w)
					adj[u][v] = w;				
			}
			
			for (int k = 0; k < n; k++) 
				for (int i = 0; i < n; i++) 
					for (int j = 0; j < n; j++) 
						adj[i][j] = Math.min(adj[i][j], adj[i][k] + adj[k][j]);
			
			long dist = 0;
			StringBuilder sbu = new StringBuilder();
			for (int i = 0; i < sa.length(); i++) {
				int u = sa.charAt(i) - 'a';
				int v = sb.charAt(i) - 'a';
				
				int min = Integer.MAX_VALUE;
				char c = 'a';
				for (char j = 'a'; j <= 'z'; j++) {
					int k = j - 'a';
					
					int nmin = adj[u][k] + adj[v][k];
					if (min > nmin) {
						min = nmin;
						c = j;
					}
				}
								
				if (min >= INF) {
					println(-1);
					flush();
					return;
				}
				dist += min;
				sbu.append(c);
					
			}
			println(dist);
			println(sbu.toString());
		}
		
		
		flush();
	}

	// ~ -----------------------------------------------------------------------
	// ~ - Auxiliary code ------------------------------------------------------
	// ~ -----------------------------------------------------------------------
	static InputStream in;
	static PrintWriter out;

	/**
	 * Simple buffered reader class. Reads if buffer is empty. Try to fill up
	 * whole buffer.
	 */
	public static class SimpleBufferedInputStream extends InputStream 
	{
		// ~ Static fields / Initializers --------------------------------------
		private static int defaultCharBufferSize = 16384;
		private InputStream in;
		byte[] buffer;
		int pos = 0;
		int len = 0;
		
		// ~ Constructors ------------------------------------------------------
		public SimpleBufferedInputStream(InputStream in) {
			this(in, defaultCharBufferSize);
		}

		public SimpleBufferedInputStream(InputStream in, int size) {
			this.in = in;
			this.buffer = new byte[size];
		}
	    
		// ~ Methods -----------------------------------------------------------
		@Override
		public int read() throws IOException {
			if (pos < len)
				return buffer[pos++];
			
			len = in.read(buffer);
			if (len == -1)
				return len;
			
			pos = 0;
			if (len <= 0)
				return -1;
			
			return buffer[pos++];
		}
	}

	public static void setStandardIO() {
		in = new SimpleBufferedInputStream(System.in);
		out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	}

	public static void setFileIO(String fileName) throws IOException {
		in = new SimpleBufferedInputStream(new FileInputStream(fileName + ".in"));
 		out = new PrintWriter(new BufferedWriter(new FileWriter(fileName + ".out")));
	}

	public static int skipWhitespaces() throws IOException {
		int c;
		for (;;) {
			c = in.read();
			if (c == -1) throw new IOException();
			if (!isWhitespace(c)) break;
		}	
		
		return c;
	}
	
	public static int skipNewLines() throws IOException {
		int c;
		for (;;) {
			c = in.read();
			if (c == -1) throw new IOException();
			if (!isNewLine(c)) break;
		}	
		
		return c;
	}

	public static boolean isNewLine(int c) {
		return c == '\n' || c == '\r' ? true : false;
	}

	public static boolean isWhitespace(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' ? true : false;
	}
	
	public static int nextNoWhitespace() throws IOException {
		int c = in.read();
		if (c == -1 || isWhitespace(c)) return -1;
		
		return c;
	}

	public static int nextNoNewLine() throws IOException {
		int c = in.read();
		if (c == -1 || isNewLine(c)) return -1;
		
		return c;
	}

	public static long nextLong() throws IOException {
		int c;
		for (;;) {
			c = in.read();
			if (!isWhitespace(c)) break;
			if (c == -1) throw new RuntimeException();
		}	

		boolean neg = false;
		long val = 0;
		if (c == '-')
			neg = true;
		else 
			val = c - '0';
		
		for (;;) {
			c = in.read();
			if (isWhitespace(c) || c == -1) 
				break;
			
			val = val * 10 + (c - '0');
		}

		if (neg) 
			return -val;
		
		return val;
	}

	public static int nextInt() throws IOException {
		int c;
		for (;;) {
			c = in.read();
			if (!isWhitespace(c)) break;
			if (c == -1) throw new RuntimeException();
		}	
		
		boolean neg = false;		
		int val = 0;
		if (c == '-')
			neg = true;
		else 
			val = c - '0';
		
		for (;;) {
			c = in.read();
			if (isWhitespace(c) || c == -1) 
				break;
			
			val = val * 10 + (c - '0');
		}

		if (neg) 
			return -val;
		
		return val;
	}

	public static byte nextByte() throws IOException {
		return (byte) nextInt();
	}

	public static String nextLine() throws IOException {
		int c = (char) skipNewLines();
		
		StringBuilder sb = new StringBuilder();
		sb.append((char) c);
		
		for (;;) {
			c = nextNoNewLine();
			if (c == -1)
				break;
			
			sb.append((char) c);
		}

		return sb.toString();
	}

	public static String nextString() throws IOException {
		int c = (char) skipWhitespaces();
		
		StringBuilder sb = new StringBuilder();
		sb.append((char) c);
		
		for (;;) {
			c = nextNoWhitespace();
			if (c == -1)
				break;
			
			sb.append((char) c);
		}

		return sb.toString();
	}

	public static double nextDouble() throws IOException {
		return Double.parseDouble(nextString());
	}	
	
	public static BigDecimal nextBigDecimal() throws IOException {
		return new BigDecimal(nextString());
	}
	
	public static char nextChar() throws IOException {
		return (char) skipWhitespaces();
	}

	public static void flush() {
		out.flush();
	}

	public static void println(Object object) {
		out.println(object);
	}

	public static void print(Object object) {
		out.print(object);
	}

	public static void println(double d, int precision) {
		String s = "%1$." + precision + "f";
		out.format(Locale.ENGLISH, s, d);
		out.println();
	}

	public static void println(BigDecimal d, int precision) {
		String s = "%1$." + precision + "f";
		out.format(Locale.ENGLISH, s, d);
		out.println();
	}
}