#include <bits/stdc++.h>
using namespace std;
const int MOD9 = 1e9 + 7;
const int MOD91 = 1e9 + 9;
const unsigned long long MOD12 = 1e12 + 39LL;
const unsigned long long MOD15 = 1e15 + 37LL;
const int INF = 1e9;
const int base = 1e9;
const int MAX = 2e5;
const long double EPS = 1e-10;
string second;
int n, k;
int a[30][30];
int dpt[101][30][101];
int dp(int pos, int prev, int left) {
  if (pos == second.size()) {
    return 0;
  }
  if (dpt[pos][prev][left] != -1) {
    return dpt[pos][prev][left];
  }
  if (left == 0) {
    return dpt[pos][prev][left] = dp(pos + 1, second[pos] - 'a', left) +
                                  a[prev][second[pos] - 'a'];
  }
  int b = -INF;
  for (int i = (0); i < (26); ++i) {
    b = max(b,
            dp(pos + 1, i, left - (i != int(second[pos] - 'a'))) + a[prev][i]);
  }
  return dpt[pos][prev][left] = b;
}
int main() {
  cin >> second >> k;
  cin >> n;
  memset(dpt, -1, sizeof(dpt));
  for (int i = (0); i < (n); ++i) {
    char x, y;
    int xx;
    cin >> x >> y >> xx;
    a[x - 'a'][y - 'a'] = xx;
  }
  cout << dp(0, 27, k);
  return 0;
}
