#include <bits/stdc++.h>
using namespace std;
int l[50][50];
vector<char> v;
bool used[30];
int k, n, m, dp[105][50][105];
string s;
int solve(int ind, int prev, int change) {
  if (ind == s.size()) return 0;
  if (dp[ind][prev][change] != -1) return dp[ind][prev][change];
  int ans = 0;
  ans = solve(ind + 1, (int)(s[ind] - 'a'), change) +
        l[prev][(int)(s[ind] - 'a')];
  for (int i = 0; i < v.size(); i++) {
    if (change + 1 <= k)
      ans = max(ans, solve(ind + 1, (int)(v[i] - 'a'), change + 1) +
                         l[prev][(int)(v[i] - 'a')]);
  }
  return dp[ind][prev][change] = ans;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  memset(dp, -1, sizeof(dp));
  char x, y;
  cin >> s >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> m;
    l[(int)(x - 'a')][(int)(y - 'a')] = m;
    if (!used[(int)(x - 'a')]) {
      v.push_back(x);
      used[(int)(x - 'a')] = 1;
    }
    if (!used[(int)(y - 'a')]) {
      v.push_back(y);
      used[(int)(y - 'a')] = 1;
    }
  }
  cout << solve(0, 30, 0);
  return 0;
}
