Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character — non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than k letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters x and y (x immediately precedes y) the bonus c(x, y) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most k letters in the name of the Vasya's character.

Input

The first line contains character's name s and an integer number k (0 ≤ k ≤ 100). The length of the nonempty string s does not exceed 100. The second line contains an integer number n (0 ≤ n ≤ 676) — amount of pairs of letters, giving bonus to the euphony. The next n lines contain description of these pairs «x y c», which means that sequence xy gives bonus c (x, y — lowercase Latin letters,  - 1000 ≤ c ≤ 1000). It is guaranteed that no pair x y mentioned twice in the input data.

Output

Output the only number — maximum possible euphony оf the new character's name.

Examples

Input

winner 4
4
s e 7
o s 8
l o 13
o o 8


Output

36

Input

abcdef 1
5
a b -10
b c 5
c d 5
d e 5
e f 5


Output

20

Note

In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36.
import java.awt.*;
import java.io.*;
import java.math.*;
import java.util.*;
import static java.lang.Math.*;

public class BetaRound66_C implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok;

    @Override
    public void run() {
        try {
            long t1 = System.currentTimeMillis();
            tok = new StringTokenizer("");
            if (System.getProperty("ONLINE_JUDGE") != null) {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                in = new BufferedReader(new FileReader("input.txt"));
                out = new PrintWriter("output.txt");
            }
            Locale.setDefault(Locale.US);
            solve();
            in.close();
            out.close();
            long t2 = System.currentTimeMillis();
            System.err.println("Time = " + (t2 - t1));
        } catch (Throwable e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    String readString() throws IOException{
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    public static void main(String[] args) {
        new Thread(null, new BetaRound66_C(), "", 256 * (1L << 20)).start();
    }

    // solution

    int n, k;
    int[] s;
    int[][][] a;
    int[][] c = new int[26][26];
    
    final int UNDEFINED = -1000 * 1000 * 1000;
    
    void solve() throws IOException {
        String str = readString();
        n = str.length();
        s = new int[n];
        for (int i = 0; i < n; i++) {
            s[i] = str.charAt(i) - 'a';
        }
        k = readInt();
        int m = readInt();
        while (m --> 0) {
            int x = readString().charAt(0) - 'a';
            int y = readString().charAt(0) - 'a';
            int cost = readInt();
            c[x][y] = cost;
        }
        a = new int[n+1][k+1][26];
        for (int[][] zz : a) {
            for (int[] z : zz) {
                Arrays.fill(z, UNDEFINED);
            }
        }
        for (int i = 0; i < 26; i++) {
            a[0][0][i] = 0;
        }
        int max = UNDEFINED;
        for (int i = 0; i <= k; i++) {
            for (int j = 0; j < 26; j++) {
                max = max(max, get(n, i, j));
            }
        }
        out.println(max);
    }
    
    int get(int len, int took, int cur) {
        if (took < 0 || len < 0 || took > len) return UNDEFINED;
        if (a[len][took][cur] != UNDEFINED) return a[len][took][cur];
        int max = UNDEFINED;
        for (int prev = 0; prev < 26; prev++) {
            int bonus = len > 1 ? c[prev][cur] : 0;
            if (s[len-1] == cur) {
                max = max(max, get(len-1, took, prev) + bonus);
            } else {
                max = max(max, get(len-1, took-1, prev) + bonus);
            }
        }
        return a[len][took][cur] = max;
    }

}
