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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

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

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FastReader in = new FastReader();
		int graph[][] = new int[26][26];
		String s = in.nextLine();
		String t = in.nextLine();
		StringBuffer output = new StringBuffer();
		String o = "";
		if (s.length() != t.length()) {
			System.out.println(-1);
		} else {
			int n = in.nextInt();
			for (int i = 0; i < 26; i++)
				for (int j = 0; j < 26; j++)
					graph[i][j] = 3000;
			for (int i = 0; i < n; i++) {
				String a = in.next();
				String b = in.next().trim();
				int w = in.nextInt();
				if (graph[(int) a.charAt(0) % 97][(int) b.charAt(0) % 97] > w)
					graph[(int) a.charAt(0) % 97][(int) b.charAt(0) % 97] = w;
			}
			floyd(graph);
			int cost = 0;
			boolean check = false;
			for (int i = 0; i < t.length(); i++) {
				int r[] = comChar(graph, s.charAt(i), t.charAt(i), s.length());
				if (r[0] == -1) {
					check = true;
					break;
				}
				cost += r[1];
				output.append((char) r[0]);

			}
			if (check == false) {
				System.out.println(cost);
				System.out.println(output);
			} else
				System.out.println(-1);

		}

	}

	public static void floyd(int graph[][]) {
		for (int k = 0; k < 26; k++)
			for (int i = 0; i < 26; i++)
				for (int j = 0; j < 26; j++)
					if (graph[i][k] != 3000 && graph[k][j] != 3000 && graph[i][j] > graph[i][k] + graph[k][j])
						graph[i][j] = graph[i][k] + graph[k][j];

	}

	public static int[] comChar(int graph[][], int s, int t, int size) {
		int r[] = new int[2];
		r[0] = -1;
		r[1] = 3000;
		if (s == t) {
			r[0] = s;
			r[1] = 0;
			return r;
		}

		if (graph[s % 97][t % 97] != 3000) {
			r[0] = t;
			r[1] = graph[s % 97][t % 97];
		}
		if (graph[t % 97][s % 97] != 3000 && r[1] > graph[t % 97][s % 97]) {
			r[0] = s;
			r[1] = graph[t % 97][s % 97];
		}

		for (int i = 0; i < 26; i++)
			if (graph[s % 97][i] != 3000 && graph[t % 97][i] != 3000 && r[1] > graph[s % 97][i] + graph[t % 97][i]) {
				r[0] = i + 97;
				r[1] = graph[s % 97][i] + graph[t % 97][i];
			}
		return r;
	}
}