#include <bits/stdc++.h>
using namespace std;
string s;
int k, len, m, ans = -0x3f3f3f3f;
int sc[30][30];
int dp[110][110][30];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> k;
  len = s.size();
  k = min(k, len);
  cin >> m;
  for (int i = 1; i <= m; i++) {
    char c1, c2;
    cin >> c1 >> c2;
    cin >> sc[c1 - 'a'][c2 - 'a'];
  }
  for (int i = 0; i <= len; i++)
    for (int j = 0; j <= len; j++)
      for (int kk = 0; kk <= 26; kk++) dp[i][j][kk] = -0x3f3f3f3f;
  for (int i = 0; i < 26; i++) dp[1][(s[0] == 'a' + i) ? 0 : 1][i] = 0;
  for (int i = 1; i <= len; i++)
    for (int j = 0; j <= i; j++)
      for (int a = 0; a < 26; a++)
        if (dp[i][j][a] != -0x3f3f3f3f)
          for (int b = 0; b < 26; b++) {
            int jj = j + ((s[i] == 'a' + b) ? 0 : 1);
            int tp = dp[i][j][a] + sc[a][b];
            if (tp > dp[i + 1][jj][b]) dp[i + 1][jj][b] = tp;
          }
  for (int i = 0; i <= k; i++)
    for (int j = 0; j < 26; j++) ans = max(ans, dp[len][i][j]);
  cout << ans << endl;
  return 0;
}
