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


public class StringProblem1 {
    
    static int INFINITY = 1000000000;
    
    public static void main(String[] args) throws Exception {
        int i,j,k;
        int LA, LB;
        int c1,c2;
        int vc1,vc2;
        int value;
        String pom;
        char pk;
        
        int sa[] = new int[100010];
        int sb[] = new int[100010];
        
        InputStreamReader rd = new InputStreamReader(System.in);
        StringTokenizer st;
        
        LA = 0;
        while (true) {
            sa[LA] = rd.read();
            if (sa[LA] == 10)
                break;
            LA++;
        }
        LA--;
        
        LB = 0;
        while (true) {
            sb[LB] = rd.read();
            if (sb[LB] == 10)
                break;
            LB++;
        }
        LB--;
        
        //System.out.println(sa[0]+" "+sa[1]);
        
        if (LA != LB) {
            System.out.println(-1);
            return;
        }
        
        int change[][] = new int[97+26][97+26];
        
        for (i=97;i<(97+26);i++) {
            for (j=97;j<(97+26);j++) {
                change[i][j] = INFINITY;
            }
        }
        
        for (i=97;i<(97+26);i++) {
            change[i][i] = 0;
        }
                
        BufferedReader br = new BufferedReader(rd);
        
        
        int N = Integer.parseInt(br.readLine());
        
        for (i=0;i<N;i++) {
            st = new StringTokenizer(br.readLine());
            
            pom = st.nextToken();
            c1 = pom.charAt(0);
            pom = st.nextToken();
            c2 = pom.charAt(0);
            value = Integer.parseInt(st.nextToken());
            
            change[c1][c2] = Math.min(change[c1][c2], value);
            
        }
        
        for (k=97;k<(97+26);k++) {
            for (i=97;i<(97+26);i++) {
                for (j=97;j<(97+26);j++) {
                    change[i][j] = Math.min(change[i][j], change[i][k]+change[k][j]);
                }
            }
        }
        
        StringBuilder sres = new StringBuilder();
        
        String res = "";
        int score = 0;
        
        int total;
        int best;
        
        int cached_val[][] = new int[97+26][97+26];
        char cached_cha[][] = new char[97+26][97+26];
        
        for (i=97;i<(97+26);i++) {
            for (j=97;j<(97+26);j++) {
                cached_val[i][j] = -1;
            }
        }
        
        for (i=0;i<LA;i++) {
            c1 = sa[i];
            c2 = sb[i];
            
            
            if (cached_val[c1][c2] != -1) {
                
                score += cached_val[c1][c2];
                sres.append(cached_cha[c1][c2]);
                //res += cached_cha[c1][c2];
                
                continue;
            }
            
            total = 0;
            
            best = INFINITY;
            
            for (pk=97;pk<(97+26);pk++) {
                
                if ((change[c1][pk] < INFINITY)&&(change[c2][pk] < INFINITY)) {
                    
                    best = Math.min(best, change[c1][pk]+change[c2][pk]);
                    
                }
                
            }
            
            if (best == INFINITY) {
                System.out.println(-1);
                return;
            } else {
                
                for (pk=97;pk<(97+26);pk++) {
                    
                    if ((change[c1][pk] < INFINITY)&&(change[c2][pk] < INFINITY)) {
                        
                        total = change[c1][pk]+change[c2][pk];
                        if (total == best) {
                            
                            score += best;
                            //res += pk;
                            sres.append(pk);
                            
                            cached_val[c1][c2] = best;
                            cached_cha[c1][c2] = pk;
                            
                            cached_val[c2][c1] = best;
                            cached_cha[c2][c1] = pk;
                            
                            break;
                        }
                    }
                    
                }   
                
            }
            
        }
        
        System.out.println(score);
        System.out.println(sres.toString());

        
    }

}