#include <bits/stdc++.h>
using namespace std;
int dp[200][30][200], vis[200][30][200], k, cost[30][30];
string s;
int solve(int pos, int prev, int change) {
  if (pos == s.length()) return 0;
  if (vis[pos][prev][change]) return dp[pos][prev][change];
  int cur = s[pos] - 'a' + 1;
  int ans = cost[prev][cur] + solve(pos + 1, cur, change);
  for (int i = 1; i <= 26 && change < k; i++)
    ans = max(ans, solve(pos + 1, i, change + (cur != i)) + cost[prev][i]);
  vis[pos][prev][change] = 1;
  dp[pos][prev][change] = ans;
  return ans;
}
int main() {
  int i, j, q, x;
  char ch1, ch2;
  cin >> s >> k;
  cin >> q;
  while (q--) {
    cin >> ch1 >> ch2 >> x;
    ;
    cost[ch1 - 'a' + 1][ch2 - 'a' + 1] = x;
  }
  cout << solve(0, 0, 0);
  return 0;
}
