#include <bits/stdc++.h>
using namespace std;
const long long inf = -(1LL << 60);
long long dp[101][101][27], c[26][26];
char buf[101];
long long ans = inf;
int main() {
  int n, k;
  scanf("%s %d %d", buf, &k, &n);
  string s = buf;
  while (n--) {
    char c1, c2;
    long long cost;
    scanf(" %c %c %I64d", &c1, &c2, &cost);
    c[c1 - 'a'][c2 - 'a'] = cost;
  }
  for (int i = 1; i <= ((int)(s).size()); i++)
    for (int j = 0; j <= k; j++)
      for (int cc = 0; cc < 26; cc++) dp[i][j][cc] = inf;
  for (int i = 0; i < 26; i++) dp[1][1][i] = 0;
  dp[1][0][s[0] - 'a'] = 0;
  for (int i = 1; i < ((int)(s).size()); i++)
    for (int j = 0; j <= i; j++)
      for (int c1 = 0; c1 < 26; c1++)
        for (int c2 = 0; c2 < 26; c2++) {
          dp[i + 1][j + 1][c2] =
              max(dp[i + 1][j + 1][c2], dp[i][j][c1] + c[c1][c2]);
          if (s[i] - 'a' == c2)
            dp[i + 1][j][c2] = max(dp[i + 1][j][c2], dp[i][j][c1] + c[c1][c2]);
        }
  for (int i = 0; i <= k; i++)
    for (int cc = 0; cc < 26; cc++)
      ans = max(ans, dp[((int)(s).size())][i][cc]);
  printf("%I64d\n", ans);
  return 0;
}
