#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
string s1, s2, t;
int g[205][205];
int n;
int main() {
  for (int i = 0; i < int(205); i++)
    for (int j = 0; j < int(205); j++) g[i][j] = 1000000009;
  getline(cin, s1);
  getline(cin, s2);
  cin >> n;
  for (int i = 0; i < int(n); i++) {
    string t1, t2;
    int c;
    cin >> t1 >> t2 >> c;
    g[t1[0]][t2[0]] = min(g[t1[0]][t2[0]], c);
  }
  for (int i = 0; i < int(205); i++) g[i][i] = 0;
  for (int k = 0; k < int(205); k++) {
    for (int i = 0; i < int(205); i++) {
      for (int j = 0; j < int(205); j++) {
        if (g[i][k] < 1000000009 && g[k][j] < 1000000009)
          g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
  if (s1.size() != s2.size()) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = 0;
  for (int i = 0; i < int(s1.size()); i++) {
    int mn = 1000000009;
    char res;
    for (char c = 'a'; c <= 'z'; c++) {
      if (g[s1[i]][c] + g[s2[i]][c] < mn) {
        mn = g[s1[i]][c] + g[s2[i]][c];
        res = c;
      }
    }
    if (mn == 1000000009) {
      cout << -1 << endl;
      return 0;
    }
    t += res;
    ans += mn;
  }
  cout << ans << endl;
  puts(t.c_str());
  return 0;
}
