#include <bits/stdc++.h>
using namespace std;
int n, m, K, f[110][110][30], ans = -1e9 - 7, a[30][30], c;
char s[110], x, y;
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  scanf("%d%d", &K, &m);
  for (int i = 1; i <= m; i++) {
    scanf(" %c %c %d", &x, &y, &c);
    a[x - 'a'][y - 'a'] = c;
  }
  memset(f, 0xcf, sizeof(f));
  for (int i = 0; i < 26; i++) f[1][s[1] - 'a' == i ? 0 : 1][i] = 0;
  for (int i = 2; i <= n; i++)
    for (int j = 0; j <= min(i, K); j++)
      for (int k = 0; k < 26; k++)
        for (int l = 0; l < 26; l++)
          if (s[i] - 'a' == l)
            f[i][j][l] = max(f[i][j][l], f[i - 1][j][k] + a[k][l]);
          else if (j)
            f[i][j][l] = max(f[i][j][l], f[i - 1][j - 1][k] + a[k][l]);
  for (int j = 0; j <= K; j++)
    for (int k = 0; k < 26; k++) ans = max(ans, f[n][j][k]);
  printf("%d", ans);
  return 0;
}
