#include <bits/stdc++.h>
using namespace std;
void solve() {
  string s;
  long long int k, n;
  cin >> s >> k >> n;
  vector<vector<long long int>> bonus(28, vector<long long int>(28));
  for (long long int i = 0; i < n; i++) {
    char c1, c2;
    long long int num;
    cin >> c1 >> c2 >> num;
    bonus[c1 - 'a'][c2 - 'a'] = num;
  }
  n = s.length();
  vector<vector<vector<long long int>>> dp(
      n + 2,
      vector<vector<long long int>>(k + 2, vector<long long int>(28, INT_MIN)));
  dp[0][0][26] = 0;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j <= k; j++) {
      for (long long int alp = 0; alp <= 26; alp++) {
        if (dp[i][j][alp] == INT_MIN) {
          continue;
        }
        long long int curr = s[i] - 'a';
        dp[i + 1][j][curr] =
            max(dp[i + 1][j][curr], dp[i][j][alp] + bonus[alp][curr]);
        for (long long int cc = 0; cc < 26; cc++) {
          dp[i + 1][j + 1][cc] =
              max(dp[i + 1][j + 1][cc], dp[i][j][alp] + bonus[alp][cc]);
        }
      }
    }
  }
  long long int ans = INT_MIN;
  for (long long int i = 0; i <= k; i++) {
    for (long long int j = 0; j < 26; j++) {
      ans = max(ans, dp[n][i][j]);
    }
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
