#include <bits/stdc++.h>
using namespace std;
int main() {
  string aa, bb;
  cin >> aa >> bb;
  int n;
  cin >> n;
  int distance[26][26];
  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < 26; j++) {
      distance[i][j] = 100002;
    }
    distance[i][i] = 0;
  }
  for (int i = 0; i < n; i++) {
    string a, b;
    cin >> a >> b;
    int cost;
    cin >> cost;
    int u = a[0] - 'a';
    int v = b[0] - 'a';
    distance[u][v] = min(distance[u][v], cost);
  }
  if (aa.length() != bb.length()) {
    cout << "-1" << endl;
    return 0;
  }
  for (int k = 0; k < 26; k++) {
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < 26; j++) {
        distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]);
      }
    }
  }
  long long cost = 0;
  vector<char> ans;
  for (int i = 0; i < aa.length(); i++) {
    int t = aa[i] - 'a';
    int tt = bb[i] - 'a';
    char aaa = 'a';
    int flag = 0;
    int add = distance[t][0] + distance[tt][0];
    for (int i = 0; i < 26; i++) {
      int t1 = distance[t][i];
      int t2 = distance[tt][i];
      if (t1 != 100002 and t2 != 100002) {
        flag++;
      }
      int temp = distance[t][i] + distance[tt][i];
      if (temp < add) {
        add = temp;
        int temp = i + 'a';
        aaa = (char)temp;
      }
    }
    if (flag == 0) {
      cout << "-1" << endl;
      return 0;
    }
    cost = cost + add;
    ans.push_back(aaa);
  }
  cout << cost << endl;
  for (auto i : ans) {
    cout << i;
  }
  return 0;
}
