#include <bits/stdc++.h>
using namespace std;
string s;
int k;
int p[30][30], dp[109][109][30];
int rec(int index, int rem, int prevv) {
  if (index == s.size()) return 0;
  int &r = dp[index][rem][prevv];
  if (r != -1) return r;
  r = rec(index + 1, rem, s[index] - 'a') + p[prevv][s[index] - 'a'];
  if (rem < k) {
    for (int i = 0; i < 26; i++) {
      r = max(r, rec(index + 1, rem + 1, i) + p[prevv][i]);
    }
  }
  return r;
}
int main() {
  cin >> s >> k;
  int t;
  cin >> t;
  while (t--) {
    int val;
    char x, y;
    cin >> x >> y >> val;
    p[x - 'a'][y - 'a'] = val;
  }
  memset(dp, -1, sizeof dp);
  cout << rec(0, 0, 26);
}
