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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.*;

public class Main implements Runnable {
	private static final int INF = Integer.MAX_VALUE/2;
	private boolean _READ_FROM_FILE = false; //System.getSecurityManager() == null; //System.getProperty("ONLINE_JUDGE") == null;
	private boolean _WRITE_TO_FILE = false;
	static final String _TASK_ID = "in";
	static final String _IN_FILE = _TASK_ID + ".in";
	static final String _OUT_FILE = _TASK_ID + ".out";
	BufferedReader reader;
	StringTokenizer tokenizer;

	private void core() throws Exception {
		String s = nextToken(), t = nextToken();
		if (s.length() != t.length()) {
			System.out.println(-1);
			return;
		}
		int[][] g = new int[26][26];
		for (int[] is : g) {
			Arrays.fill(is, INF);
		}
		int n = nextInt();
		for (int i = 0; i < n; i++) {
			char u = nextToken().charAt(0), v = nextToken().charAt(0);
			int c = nextInt();
			g[u - 'a'][v - 'a'] = Math.min(g[u - 'a'][v - 'a'], c);
		}
		
		for (int i = 0; i < g.length; i++) {
			g[i][i] = 0;
		}
		for (int k = 0; k < g.length; k++) {
			for (int i = 0; i < g.length; i++) {
				for (int j = 0; j < g.length; j++) {
					g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
				}
			}
		}
		
		char[] c = new char[s.length()];
		int res = 0;
		boolean ok = true;
		for (int i = 0; i < s.length(); i++) {
			int u = s.charAt(i) - 'a';
			int v = t.charAt(i) - 'a';
			int best = INF;
			if (u == v) {
				c[i] = s.charAt(i);
			}
			else {
				for (int j = 0; j < g.length; j++) {
					if (best > g[u][j] + g[v][j]) {
						best = g[u][j] + g[v][j];
						c[i] = (char) ('a' + j);
					}
				}
				if (best == INF) {
					ok = false;
				}
				else {
					res += best;
				}
			}
		}
		
		if (!ok) {
			System.out.println(-1);
		}
		else {
			System.out.println(res);
			System.out.println(c);
		}
	}
	void debug(Object...os) {
		System.out.println(Arrays.deepToString(os));
	}
//--------------------- IO stuffs ---------------------
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Main());
        thread.start();
        thread.join();
    }
	public void run() {
        try {
        	reader = new BufferedReader(_READ_FROM_FILE ? new FileReader(_IN_FILE) : new InputStreamReader(System.in));
        	System.setOut(new PrintStream(new BufferedOutputStream(_WRITE_TO_FILE ? new FileOutputStream(_OUT_FILE) : System.out)));
            tokenizer = new StringTokenizer("");
            core();
            reader.close();
            System.out.flush();
            System.out.close();
            System.exit(0);
        } catch (Exception e) {
        	System.out.flush();
            e.printStackTrace();
            System.exit(1);
        }
    }
    int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }
    long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }
    double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
    String nextToken() throws Exception {
        while (!tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    boolean nextIsAvailable() throws Exception {
    	while (!tokenizer.hasMoreElements()) {
    		String line = reader.readLine();
    		if (line == null) return false;
    		tokenizer = new StringTokenizer(line);
    	}
    	return true;
    }
}