#include <bits/stdc++.h>
using namespace std;
int mp[27][27];
int dp[111][111][26];
bool flag[111][111][26];
char s[111];
inline void checkmax(int &a, int b) {
  if (a < b) a = b;
}
int main() {
  int i, j, k, n, m;
  scanf("%s", s);
  scanf("%d", &k);
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    getchar();
    char ch1, ch2;
    scanf("%c", &ch1);
    getchar();
    scanf("%c%d", &ch2, &m);
    mp[ch1 - 'a'][ch2 - 'a'] = m;
  }
  memset(flag, 0, sizeof(flag));
  for (i = 0; i < 26; i++) {
    if (s[0] - 'a' == i) {
      dp[0][0][i] = 0;
      flag[0][0][i] = true;
    } else if (k > 0) {
      dp[0][1][i] = 0;
      flag[0][1][i] = true;
    }
  }
  for (i = 0; i < strlen(s) - 1; i++) {
    for (j = 0; j <= k; j++)
      for (int x = 0; x < 26; x++) {
        if (!flag[i][j][x]) continue;
        for (int y = 0; y < 26; y++)
          if (s[i + 1] - 'a' == y) {
            if (!flag[i + 1][j][y]) dp[i + 1][j][y] = dp[i][j][x] + mp[x][y];
            flag[i + 1][j][y] = true;
            checkmax(dp[i + 1][j][y], dp[i][j][x] + mp[x][y]);
          } else if (j < k) {
            if (!flag[i + 1][j + 1][y])
              dp[i + 1][j + 1][y] = dp[i][j][x] + mp[x][y];
            flag[i + 1][j + 1][y] = true;
            checkmax(dp[i + 1][j + 1][y], dp[i][j][x] + mp[x][y]);
          }
      }
  }
  m = -999999999;
  for (i = 0; i <= k; i++) {
    for (j = 0; j < 26; j++) {
      if (flag[strlen(s) - 1][i][j]) checkmax(m, dp[strlen(s) - 1][i][j]);
    }
  }
  cout << m << endl;
}
