#include <bits/stdc++.h>
using namespace std;
int Numbers[26][26];
int search(int i, int j) { return Numbers[i][j]; }
string s;
int dp[105][105][26];
int f(int i, int j, int k) {
  if (j < 0 || i >= s.size() - 1) return 0;
  if (dp[i][j][k] != -1) return dp[i][j][k];
  int ans = search(k, s[i + 1] - 'a') + f(i + 1, j, s[i + 1] - 'a');
  if (j == 0) {
    return dp[i][j][k] = ans;
  }
  for (int i1 = 0; i1 < 26; i1++) {
    if (i1 == s[i + 1] - 'a') continue;
    ans = max(ans, search(k, i1) + f(i + 1, j - 1, i1));
  }
  return dp[i][j][k] = ans;
}
int main() {
  cin >> s;
  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    char c1, c2;
    int w;
    cin >> c1 >> c2 >> w;
    Numbers[c1 - 'a'][c2 - 'a'] = w;
  }
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 105; j++) {
      for (int z = 0; z < 26; z++) dp[i][j][z] = -1;
    }
  }
  int ans = f(0, k, s[0] - 'a');
  if (k > 0) {
    for (int i = 0; i < 26; i++) {
      if (i == s[0] - 'a') continue;
      ans = max(ans, f(0, k - 1, i));
    }
  }
  cout << ans << endl;
}
