#include <bits/stdc++.h>
using namespace std;
const int INF = 100000;
int t[30][30];
int cost;
string Gao(string a, string b) {
  cost = 0;
  string ans = "";
  int la = a.length(), lb = b.length();
  if (la != lb) return "-1";
  for (int i = 0; i < la; i++)
    if (a[i] == b[i])
      ans += a[i];
    else {
      int c = INF;
      char cc;
      for (int j = 0; j < 26; j++) {
        if (t[a[i] - 'a'][j] == INF) continue;
        if (t[b[i] - 'a'][j] == INF) continue;
        if (c > t[a[i] - 'a'][j] + t[b[i] - 'a'][j]) {
          c = t[a[i] - 'a'][j] + t[b[i] - 'a'][j];
          cc = 'a' + j;
        }
      }
      if (c == INF) return "-1";
      ans += cc;
      cost += c;
    }
  return ans;
}
int main() {
  string a, b;
  while (cin >> a >> b) {
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < 26; j++) t[i][j] = INF;
      t[i][i] = 0;
    }
    int n;
    cin >> n;
    char u, v;
    int w;
    for (int i = 0; i < n; i++) {
      cin >> u >> v >> w;
      if (t[u - 'a'][v - 'a'] > w) t[u - 'a'][v - 'a'] = w;
    }
    for (int k = 0; k < 26; k++)
      for (int i = 0; i < 26; i++)
        for (int j = 0; j < 26; j++)
          if (t[i][k] + t[k][j] < t[i][j]) t[i][j] = t[i][k] + t[k][j];
    string ans = Gao(a, b);
    if (ans != "-1") cout << cost << endl;
    cout << ans << endl;
  }
  return 0;
}
