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.io.PrintWriter;
import java.util.StringTokenizer;


public class PB implements Runnable {
	private BufferedReader rd;
	private StringTokenizer tok = null;
	
	String a;
	String b;
	
	int[][] g;

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

	@Override
	public void run() {
		try {
			solve();
		} catch (IOException e) {
			throw new RuntimeException();
		}
	}

	private void solve() throws IOException {
		readData();
	}

	private void readData() throws IOException {
		rd = new BufferedReader(new InputStreamReader(System.in));
		
		a = rd.readLine();
		b = rd.readLine();
		
		g = new int[26][26];
		
		int n = nextInt();
		
		for (int i = 0; i < 26; ++i) {
			for (int j = 0; j < 26; ++j) {
				g[i][j] = Integer.MAX_VALUE;
			}
		}
		
		for (int i = 0; i < 26; ++i) {
			g[i][i] = 0;
		}
		
		for (int i = 0; i < n; ++i) {
			int from = nextToken().charAt(0) - 'a';
			int to = nextToken().charAt(0) - 'a';
			
			int move = nextInt();
			g[from][to] = Math.min(g[from][to], move);
		}
		
		for (int k = 0; k < 26; ++k) {
			for (int i = 0; i < 26; ++i) {
				for (int j = 0; j < 26; ++j) {
					if ((g[i][k] != Integer.MAX_VALUE) && (g[k][j] != Integer.MAX_VALUE)) {
						g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
					}
				}
			}
		}
		
		int best = 0;
		int l1 = a.length();
		int l2 = b.length();
		if (l1 != l2) {
			PrintWriter wr = new PrintWriter(System.out);
			wr.println(-1);
			wr.close();
			return;
		}
		
		StringBuffer s = new StringBuffer();
		
		int l = l1;
		for (int i = 0; i < l; ++i) {
			int cha = a.charAt(i) - 'a';
			int chb = b.charAt(i) - 'a';
			int bestmove = Integer.MAX_VALUE;
			char bestch = '.'; 
			for (int j = 0; j < 26; ++j) {
				if ((g[cha][j] != Integer.MAX_VALUE) && (g[chb][j] != Integer.MAX_VALUE)) {
					int newmove = g[cha][j] + g[chb][j];
					if (newmove < bestmove) {
						bestmove = newmove;
						bestch = (char)(j + 'a');
					}
				}
			}
			if (bestmove != Integer.MAX_VALUE) {
				s.append(bestch);
			} else {
				PrintWriter wr = new PrintWriter(System.out);
				wr.println(-1);
				wr.close();
				return;
			}
			best += bestmove;
		}
		
		PrintWriter wr = new PrintWriter(System.out);
		wr.println(best);
		wr.println(s.toString());
		wr.close();
		
		rd.close();
	}

	private int nextInt() throws IOException {
		while (true) {
			if ((tok != null) && (tok.hasMoreTokens())) {
				break;
			}
			tok = new StringTokenizer(rd.readLine());
		}
		return Integer.parseInt(tok.nextToken());
	}
	
	private String nextToken() throws IOException {
		while (true) {
			if ((tok != null) && (tok.hasMoreTokens())) {
				break;
			}
			tok = new StringTokenizer(rd.readLine());
		}
		return tok.nextToken();
	}
}