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.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;


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) {
		FastReader in = new FastReader();
		String s, t;
		StringBuffer sb = new StringBuffer();
		char a1, a2;
		boolean r = true;
		int n, p, sl, tl, custo = 0, menordist, menorchar = 0;
		s = in.nextLine();
		t = in.nextLine();
		n = in.nextInt();
		Graph g = new Graph(26);
		for (int i=0; i<n; i++) {
			a1 = in.next().charAt(0);
			a2 = in.next().charAt(0);
			p = in.nextInt();
			g.setEdge(a1 - 97, a2 - 97, p);
		}
		sl = s.length();
		tl = t.length();
		g.Floyd(new int[26][26]);
		if (sl == tl) {
			for (int i = 0; i < sl; i++) {
				a1 = s.charAt(i);
				a2 = t.charAt(i);
				if (a1 != a2) {
					menordist = Integer.MAX_VALUE;
					for (int j = 0; j < 26; j++) {
						if (g.getwt(a1 - 97, j)!=Integer.MAX_VALUE && g.getwt(a2 - 97, j) != Integer.MAX_VALUE) {
							if (menordist > g.getwt(a1 - 97, j) + g.getwt(a2 - 97, j)) {
								menorchar = j;
								menordist = g.getwt(a1 - 97, j) + g.getwt(a2 - 97, j);
							}
						}
					}
					if (menordist == Integer.MAX_VALUE) {
						i = sl;
						r = false;
					}
					custo += menordist;
					sb.append((char) (menorchar+97));
				} else {
					sb.append(a1);
				}
			}
			if (r) {
				System.out.println(custo);
				System.out.print(sb);
			} else {
				System.out.print(-1);
			}
		} else {
			System.out.print(-1);
		}
	}

}

class Graph {
	private int[][] matriz;
	private int numEdge;
	private int[] mark;
	
	public Graph(int n) {
		this.matriz = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				this.matriz[i][j] = Integer.MAX_VALUE;
			}
		}
		this.numEdge = 0;
		this.mark = new int[n];
	}
	
	public int n() {
		return this.mark.length;
	}
	
	public int first(int v) {
		for (int i = 0; i < n(); i++) {
			if (this.matriz[v][i]!= 0) {
				return i;
			}
		}
		return n();
	}
	
	public int next(int v, int w) {
		for (int i = w+1; i < n(); i++) {
			if (this.matriz[v][i] != 0) {
				return i;
			}
		}
		return n();
	}
	
	public void setEdge(int i, int j, int wt) {	
		if (this.matriz[i][j] != 0) {
			if (wt < this.matriz[i][j]) {
				this.matriz[i][j] = wt;
				this.numEdge ++;
			}
		}
	}
	
	
	public boolean isEdge(int i, int j) {
		if ((i < n()) && (j < n())) {
			if (this.matriz[i][j] == 1) {
				return true;
			}
		}
		return false;
	}
	
	public int getwt(int i, int j) {
		if ((i < n()) && (j < n())) {
			return this.matriz[i][j];
		}
		return 0;
	}
	
	public void setMark(int v, int val) {
		if (v < n()) 
			this.mark[v] = val;
	}
	
	public Integer getMark(int v) {
		if (v < n())
			return this.mark[v];
		return null;
	}
	
	public void Floyd(int[][] d) {
		d = this.matriz;
		for (int i = 0; i < n(); i++) {
			d[i][i] = 0;
		}
		for (int k = 0; k <n(); k++) {
			for (int i = 0; i<n(); i++) {
				for (int j = 0; j < n(); j++) {
					if (d[i][k] != Integer.MAX_VALUE && d[k][j] != Integer.MAX_VALUE && d[i][j] > d[i][k] + d[k][j]) {
						d[i][j] = d[i][k] + d[k][j];
					}
				}
			}
		}
		this.matriz = d;
	}

}