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.awt.Point;
import java.io.*;
    import java.text.*;
    import java.util.*;
import java.util.regex.*;

    public class Main{
        static class Run implements Runnable{
            //TODO parameters
            final boolean consoleIO = true;
            final String inFile = "input.txt";
            final String outFile = "output.txt";
            
            String s1,s2;
            int n;
            int[][] d;
            
            void floyd() {
                for(int k = 0; k < 26; ++k)
                    for(int i = 0; i < 26; ++i)
                        for(int j = 0; j < 26; ++j) { 
                            int min = Math.min(d[i][j], d[i][k]+d[k][j]);
                            if(min >= 0)
                                d[i][j] = min;
                        }
            }
            
            StringBuilder buf;
            int price;
            
            boolean solve() {
                if(s1.length() != s2.length())
                    return false;
                
                price = 0;
                int len = s1.length();
                buf = new StringBuilder(len);
                
                for(int i = 0; i < len; ++i) {
                    int a = s1.charAt(i)-'a';
                    int b = s2.charAt(i)-'a';
                    
                    if(a==b) {
                        buf.append((char)(a+'a'));
                        continue;
                    }
                    
                    int rep1 = replace(a,b);
                    int rep2 = replace(b,a);
                    if(rep1==-1 && rep2==-1)
                        return false;
                    
                    int rep=-1;
                    if(rep1==-1)
                        rep = rep2;
                    else
                        if(rep2==-1)
                            rep = rep1;
                    
                    if(rep==-1) {
                        if(rep1>rep2)
                            rep = rep2;
                        else
                            rep = rep1;
                    }
                    
                    buf.append((char)(rep+'a'));
                    price += d[a][rep]+d[b][rep];
                }
                
                return true;
            }
            
            int replace(int a, int b) {
                int m = Integer.MAX_VALUE;
                int min = -1;
                
                for(int i = 0; i < 26; ++i) {
                    if(d[a][i]+d[b][i]>=0 && d[a][i]+d[b][i]<m) {
                        m = d[a][i]+d[b][i];
                        min = i;
                    }
                }
                
                return min;
            }
            
            @Override
            public void run() {
                s1 = nextLine();
                s2 = nextLine();
                n = nextInt();
                
                d = new int[26][26];
                for(int i = 0; i < 26; ++i)
                    Arrays.fill(d[i],Integer.MAX_VALUE);
                
                
                for(int i = 0; i < n; ++i) {
                    int a = nextToken().charAt(0)-'a';
                    int b = nextToken().charAt(0)-'a';
                    int p = nextInt();
                    d[a][b] = Math.min(p, d[a][b]);
                }
                
                for(int i = 0; i < 26; ++i)
                    d[i][i] = 0;
                
                floyd();
                
//              for(int i = 0; i < 26; ++i)
//                  for(int j = 0; j < 26; ++j)
//                      if(d[i][j] != Integer.MAX_VALUE)
//                          print((char)(i+'a')+" "+(char)(j+'a')+" "+d[i][j]+"\n");
                
                print(solve()?price+"\n"+buf:"-1");
                close();
            }
        //=========================================================================================================================
            BufferedReader in;
            PrintWriter out;
            StringTokenizer strTok;
           
            Run() {
                if (consoleIO) {
                    initConsoleIO();
                }
                else {
                    initFileIO();
                }
            }
           
            void initConsoleIO() {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(new OutputStreamWriter(System.out));
            }
           
            void initFileIO() {
                try {
                    in = new BufferedReader(new FileReader(inFile));
                    out = new PrintWriter(new FileWriter(outFile));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           
            void close() {
                try {
                    in.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           
            int nextInt() {
                return Integer.parseInt(nextToken());
            }
           
            double nextDouble() {
                return Double.parseDouble(nextToken());
            }
           
            float nextFloat() {
                return Float.parseFloat(nextToken());
            }
           
            long nextLong() {
                return Long.parseLong(nextToken());
            }
           
            String nextLine() {
                try {
                    return in.readLine();
                } catch (IOException e) {
                    return "__NULL";
                }
            }
           
            boolean hasMoreTokens() {
                return (strTok == null) || (strTok.hasMoreTokens());
            }
           
            String nextToken() {
                while (strTok == null || !strTok.hasMoreTokens()) {
                    String line;
                    try {
                        line = in.readLine();
                        strTok = new StringTokenizer(line);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
               
                return strTok.nextToken();
            }
           
            void cout(Object o){
                System.out.println(o);
            }
           
            void print(Object o) {
                out.write(o.toString());
            }
            
            void println(Object o) {
                out.write(o.toString() + '\n');
            }
           
            void printf(String format, Object... args) {
                out.printf(format, args);
            }
           
            String sprintf(String format, Object... args) {
            return MessageFormat.format(format, args);
        }
        }
       
        static class Pair<A, B> {
            A a;
            B b;
           
            A f() {
                return a;
            }
           
            B s() {
                return b;
            }
           
            Pair(A a, B b) {
                this.a = a;
                this.b = b;
            }
           
            Pair(Pair<A, B> p) {
                a = p.f();
                b = p.s();
            }
            
            @Override
            public String toString() {
                return a+" "+b;
            }
            
            @Override
            public int hashCode() {
                return toString().hashCode();
            }
            
            @Override
            public boolean equals(Object obj) {
                if(!(obj instanceof Pair))
                    return false;
                
                Pair<A,B> p = (Pair<A, B>) obj;
                return p.a==a && p.b==b;
            }
        }
       
        public static void main(String[] args) throws IOException {
            Run run = new Run();
            Thread thread = new Thread(run);
            thread.run();
        }
    }