#include <bits/stdc++.h>
using namespace std;
int dp[120][120][27];
int main() {
  char s[120];
  int n, m, e[27][27] = {};
  memset(dp, 192, sizeof(dp));
  scanf("%s%d%d", s + 1, &n, &m);
  dp[0][0][26] = 0;
  for (int i = 0; i < m; i++) {
    char x[10], y[10];
    scanf("%s%s", x, y);
    scanf("%d", &e[*x - 'a'][*y - 'a']);
  }
  int ans;
  for (int i = 1; s[i]; i++) {
    ans = 0x80808080;
    for (int x = 0; x < 27; x++) {
      for (int y = 0; y < 26; y++) {
        int o = (y + 'a' != s[i]);
        for (int j = o; j <= n; j++)
          ans = max(ans, dp[i][j][y] =
                             max(dp[i][j][y], dp[i - 1][j - o][x] + e[x][y]));
      }
    }
  }
  printf("%d\n", ans);
}
