#include <bits/stdc++.h>
using namespace std;
string s, t;
char t1, t2, str[100010];
int cost[1000], to[1000], fr[1000], res = 0, n;
int path[26][26];
int sh[26][26];
int main() {
  cin >> s >> t >> n;
  for (int i = 0; i < n; i++) {
    cin >> t1 >> t2 >> cost[i];
    fr[i] = t1 - 'a';
    to[i] = t2 - 'a';
  }
  if (s.size() != t.size()) {
    cout << "-1" << endl;
    return 0;
  }
  for (int i = 0; i < 26; i++)
    for (int j = 0; j < 26; j++) {
      path[i][j] = 100000;
      sh[i][j] = 100000;
    }
  for (int i = 0; i < 26; i++) {
    path[i][i] = 0;
    sh[i][i] = 0;
  }
  for (int i = 0; i < n; i++)
    path[fr[i]][to[i]] = min(path[fr[i]][to[i]], cost[i]);
  for (int i = 0; i < 26; i++) {
    bool update = true;
    while (update) {
      update = false;
      for (int j = 0; j < n; j++)
        if (sh[i][fr[j]] != 100000 &&
            sh[i][fr[j]] + path[fr[j]][to[j]] < sh[i][to[j]]) {
          sh[i][to[j]] = sh[i][fr[j]] + path[fr[j]][to[j]];
          update = true;
        }
    }
  }
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == t[i])
      str[i] = s[i];
    else {
      int ob = 0, tmp = 100000, sum = 0;
      for (int j = 0; j < 26; j++) {
        if (sh[s[i] - 'a'][j] + sh[t[i] - 'a'][j] < tmp) {
          ob = j;
          tmp = sh[s[i] - 'a'][j] + sh[t[i] - 'a'][j];
        }
      }
      if (tmp == 100000) {
        cout << "-1" << endl;
        return 0;
      } else {
        str[i] = ob + 'a';
        res += tmp;
      }
    }
  }
  str[s.size()] = 0;
  cout << res << endl;
  cout << str << endl;
  return 0;
}
