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

public class Main {

	public static void main(String[] args) {
		FastReader in = new FastReader();
		StringBuffer a = new StringBuffer(), b = new StringBuffer(),answ = new StringBuffer();
		int cases = 0, aux = 0, fim = 0,aux1 = 0, aux2 = 0;
		boolean possible = true,print = true;
		int[][] d = new int[30][30];
		listGraph g = new listGraph();
		g.createGraph(30);
		a.append(in.next());
		b.append(in.next());
		if (a.length()==b.length()) {
			cases = in.nextInteger();
			for (int i = 0; i < cases; ++i) {
				aux1 = in.next().charAt(0)-97;
				aux2 = in.next().charAt(0)-97;
				if (g.getEdge(aux1, aux2) == null)
					g.setEdge(aux1, aux2, in.nextInteger());
				else {
					if (g.getEdge(aux1, aux2).d > (aux = in.nextInteger())) {
						g.delEdge(aux1, aux2);
						g.setEdge(aux1, aux2, aux);
					}
				}
			}g.floydWarshall(g,d);
			aux=-1;
			aux1=-1;
			aux2=-1;
			for (int i = 0; i<a.length(); ++i) {
				if (a.charAt(i) != b.charAt(i)) {
					for (int j = 0; j < g.getN(); ++j) {
						if(d[b.charAt(i)-97][j] != 999999999 && d[a.charAt(i)-97][j]!= 999999999) {							
							aux = d[b.charAt(i)-97][j] + d[a.charAt(i)-97][j];
							possible = true;
							if (aux<aux1 || aux2==-1) {
								aux1 = aux;
								aux2 = j+97;
							}
						}
					}
					if (!possible) {
						print = false;
						System.out.println(-1);
						break;
					}fim+=aux1;
					if (aux2>0)
						answ.append(Character.toChars(aux2));
					aux1 = -1;
					aux2 = -1;
					aux = -1;
				}else {
					answ.append(a.charAt(i));
				}
				possible = false;
			}
			if (print) {
			System.out.println(fim);
			System.out.println(answ);
			}
		}else
			System.out.println(-1);
	}static class listGraph{
		private int n;
		private ArrayList<ArrayList<edge>> graph;
		private int[] mark;
		private int nedge;
		public void createGraph(int n) {
			this.n = n;
			this.nedge = 0;
			this.graph = new ArrayList<ArrayList<edge>>();
			this.mark = new int[n];
			for (int i = 0; i<n; ++i) {
				mark[i]	= 0;
				this.graph.add(new ArrayList<edge>());
			}
		}
		public edge first(int x) {
			ArrayList<edge> list = this.graph.get(x);
			if (list.size()>0)
				return list.get(0);
			else
				return new edge(this.n,this.n,0);
		}
		public edge next(int x, int index) {
			ArrayList<edge> list = this.graph.get(x);
			if (index < list.size() )
				return list.get(index);
			else return new edge(this.n,this.n,0);
			
		}
		public void setEdge(int x, int y, int d) {
			ArrayList<edge> list = this.graph.get(x);
			edge e = new edge(x,y,d);
			if (list.indexOf(e) == -1) {
				this.nedge++;
				this.graph.get(x).add(e);
			}
		}
		public void delEdge(int x, int y) {
			ArrayList<edge> list = this.graph.get(x);
			for (int i = 0; i<list.size(); ++i) {
				if (list.get(i).v == y) {
					this.nedge--;
					this.graph.get(x).remove(i);
				}
			}
		}
		public int getN() {
			return this.n;
		}
		public int getnEdge() {
			return this.nedge;
		}
		public ArrayList<ArrayList<edge>> getGraph() {
			return this.graph;
		}
		public edge getEdge(int x, int y) {
			ArrayList<edge> list = this.graph.get(x);
			for (int i = 0; i<list.size(); ++i) {
				if (list.get(i).v == y) {
					return list.get(i);
				}
			}
			return null;
		}
		public int[] getMark() {
			return this.mark;
		}public void floydWarshall (listGraph g, int[][] d) {
			for (int i = 0; i<g.getN(); ++i) {
				for (int j = 0; j<g.getN();j++) {
					if (i==j)
						d[i][j] = 0;
					else if (g.graph.get(i).size()!=0) {
						if (g.getEdge(i, j)!=null)
							d[i][j] = g.getEdge(i,j).d;
						else
							d[i][j] = 999999999;
					}else {
						d[i][j] = 999999999;
					}
				}
			}for (int k = 0; k<g.getN(); ++k) {
				for (int l = 0; l<g.getN(); ++l) {
					for (int m = 0; m<g.getN(); ++m) {
						if (d[l][k] != 999999999 && d[k][m] != 999999999) {
							if (d[l][k] + d[k][m] < d[l][m])
								d[l][m] = d[l][k] + d[k][m];
						}
					}
				}
			}
		}
	}
	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(); 
	    } 

	    Integer nextInteger() 
	    { 
	        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; 
	    } 
	}
	static class edge implements Comparable<edge>{
		public int p;
		public int v;
		public Integer d;
		public edge(int p, int v, Integer d) {
			this.p = p;
			this.v = v;
			this.d = d;
		}
		public int compareTo(edge e) {
			if (this.d.intValue() == e.d.intValue()) 
				return 0;
			if (this.d.intValue() > e.d.intValue())
				return 1;
			else
				return -1;
		}

	}
}