#include <bits/stdc++.h>
using namespace std;
int dp[102][102][27], n, K;
int v[27][27];
int ans = -INT_MAX;
string s;
int main() {
  for (int i = 0; i < 102; i++)
    for (int j = 0; j < 102; j++)
      for (int k = 0; k < 27; k++) dp[i][j][k] = -0x7f7f7f7f;
  cin >> s >> K;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    char a, b;
    cin >> a >> b;
    a -= 'a', b -= 'a';
    cin >> v[a][b];
  }
  for (int k = 0; k < 26; k++) {
    if (k == s[0] - 'a')
      dp[1][0][k] = 0;
    else
      dp[1][1][k] = 0;
  }
  for (int i = 2; i <= s.size(); i++) {
    for (int j = 0; j <= min(i, K); j++) {
      for (int k = 0; k < 26; k++) {
        for (int nk = 0; nk < 26; nk++) {
          if (nk != s[i - 1] - 'a')
            dp[i][j + 1][nk] =
                max(dp[i - 1][j][k] + v[k][nk], dp[i][j + 1][nk]);
          else
            dp[i][j][nk] = max(dp[i - 1][j][k] + v[k][nk], dp[i][j][nk]);
        }
      }
    }
  }
  for (int j = 0; j <= K; j++)
    for (int i = 0; i < 26; i++) {
      ans = max(ans, dp[s.size()][j][i]);
    }
  cout << ans;
  return 0;
}
