#include <bits/stdc++.h>
int w[26][26];
int max_e[100][26][101];
int main() {
  char s[101], k;
  scanf("%s %d", s, &k);
  int n;
  scanf("%d", &n);
  while (n--) {
    char x[2], y[2];
    int c;
    scanf("%s %s %d", x, y, &c);
    w[x[0] - 'a'][y[0] - 'a'] = c;
  }
  for (int j = 0; j < 26; j++) max_e[0][j][0] = -2147483647;
  max_e[0][s[0] - 'a'][0] = 0;
  for (int i = 1, d = strlen(s); i < d; i++) {
    for (int j = 0; j < 26; j++) {
      for (int kk = 0; kk <= k; kk++) {
        max_e[i][j][kk] = -2147483647;
        for (int c = 0; c < 26; c++) {
          if (s[i] - 'a' == j && max_e[i - 1][c][kk] != -2147483647)
            max_e[i][j][kk] =
                ((max_e[i][j][kk]) > (max_e[i - 1][c][kk] + w[c][j])
                     ? (max_e[i][j][kk])
                     : (max_e[i - 1][c][kk] + w[c][j]));
          else if (kk != 0 && max_e[i - 1][c][kk - 1] != -2147483647)
            max_e[i][j][kk] =
                ((max_e[i][j][kk]) > (max_e[i - 1][c][kk - 1] + w[c][j])
                     ? (max_e[i][j][kk])
                     : (max_e[i - 1][c][kk - 1] + w[c][j]));
        }
      }
    }
  }
  int ans = -2147483647;
  int len = strlen(s);
  for (int i = 0; i < 26; i++)
    if (max_e[len - 1][i][k] > ans) ans = max_e[len - 1][i][k];
  printf("%d\n", ans);
}
