#include <bits/stdc++.h>
using namespace std;
string s;
int k, n, dp[105][105][27], len, c[680], bonus[30][30];
char s1[680], s2[680];
int main() {
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
  cin >> s;
  cin >> k;
  cin >> n;
  memset(c, -100000000, sizeof(c));
  for (int i = 1; i <= n; i++) {
    cin >> s1[i] >> s2[i] >> c[i];
    bonus[s1[i] - 'a'][s2[i] - 'a'] = c[i];
  }
  len = s.length() - 1;
  for (int i = 0; i <= len; i++) {
    for (int j = 0; j <= k; j++) {
      for (int l = 0; l <= 25; l++) dp[i][j][l] = -100000000;
    }
  }
  for (int i = 0; i <= 25; i++) {
    if (s[0] == (char)(i + 97))
      dp[0][0][i] = 0;
    else
      dp[0][1][i] = 0;
  }
  for (int i = 1; i <= len; i++) {
    for (int l = 0; l <= k; l++) {
      for (int o = 0; o <= 25; o++) {
        for (int g = 0; g <= 25; g++) {
          if ((char)(o + 97) != s[i] && l >= 1)
            dp[i][l][o] = max(dp[i][l][o], dp[i - 1][l - 1][g] + bonus[g][o]);
          else if ((char)(o + 97) == s[i])
            dp[i][l][o] = max(dp[i][l][o], dp[i - 1][l][g] + bonus[g][o]);
        }
      }
    }
  }
  int max1 = -100000000;
  for (int l = 0; l <= k; l++) {
    for (int o = 0; o <= 25; o++) {
      max1 = max(max1, dp[len][l][o]);
    }
  }
  cout << max1;
}
