#include <bits/stdc++.h>
using namespace std;
const int M_COST = 1000000;
int main(void) {
  string s, t;
  string res;
  int cost = 0;
  int n;
  vector<vector<int> > c(30, vector<int>(30, M_COST));
  cin >> s >> t;
  cin >> n;
  for (int i = (0); i < ((30)); ++i) c[i][i] = 0;
  for (int i = (0); i < ((n)); ++i) {
    char a, b;
    int w;
    cin >> a >> b >> w;
    c[a - 'a'][b - 'a'] = min(c[a - 'a'][b - 'a'], w);
  }
  if (((s).size()) != ((t).size())) {
    cout << -1;
    return 0;
  }
  for (int k = (0); k < ((30)); ++k) {
    for (int i = (0); i < ((30)); ++i) {
      for (int j = (0); j < ((30)); ++j) {
        c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
      }
    }
  }
  for (int i = (0); i < ((((s).size()))); ++i) {
    if (s[i] != t[i]) {
      int mc = M_COST;
      int index = -1;
      for (int j = (0); j < ((30)); ++j) {
        if (c[s[i] - 'a'][j] + c[t[i] - 'a'][j] < mc) {
          mc = c[s[i] - 'a'][j] + c[t[i] - 'a'][j];
          index = j;
        }
      }
      if (index == -1) {
        cout << -1;
        return 0;
      }
      cost += mc;
      res += index + 'a';
    } else {
      res += s[i];
    }
  }
  cout << cost << endl;
  cout << res;
}
