#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
int a, b, c, d, i, j, n, m, k;
char str[102], s1[3], s2[3];
int cs[26][26];
int dp[102][102][26];
int main() {
  scanf("%s%d", str, &k);
  n = (int)strlen(str);
  scanf("%d", &m);
  for (int _n((m)-1), i(0); i <= _n; i++) {
    scanf("%s%s%d", s1, s2, &a);
    cs[s1[0] - 'a'][s2[0] - 'a'] = a;
  }
  for (int _n((n)-1), i(0); i <= _n; i++) str[i] -= 'a';
  for (int _n((102) - 1), i(0); i <= _n; i++)
    for (int _n((102) - 1), j(0); j <= _n; j++)
      for (int _n((26) - 1), z(0); z <= _n; z++) dp[i][j][z] = -1000000000;
  dp[0][0][str[0]] = 0;
  if (k)
    for (int _n((26) - 1), i(0); i <= _n; i++) {
      if (i == str[0]) continue;
      dp[0][1][i] = 0;
    }
  for (int _n((n - 1)), i(1); i <= _n; i++) {
    for (int _n((k + 1) - 1), j(0); j <= _n; j++) {
      for (int _n((26) - 1), z(0); z <= _n; z++) {
        if (str[i] == z)
          c = 0;
        else
          c = 1;
        if (c > j) continue;
        for (int _n((26) - 1), o(0); o <= _n; o++) {
          dp[i][j][z] = max(dp[i][j][z], dp[i - 1][j - c][o] + cs[o][z]);
        }
      }
    }
  }
  a = -1000000000;
  for (int _n((k + 1) - 1), j(0); j <= _n; j++)
    for (int _n((26) - 1), z(0); z <= _n; z++) a = max(a, dp[n - 1][j][z]);
  printf("%d\n", a);
}
