#include <bits/stdc++.h>
using namespace std;
string s, t;
int change[26][26];
int main() {
  int n;
  cin >> s >> t >> n;
  for (int i = 0; i < 26; i++)
    for (int j = 0; j < 26; j++)
      if (i != j) change[i][j] = 123456;
  for (int i = 0; i < n; i++) {
    char x, y;
    int price;
    cin >> x >> y >> price;
    change[x - 'a'][y - 'a'] = min(price, change[x - 'a'][y - 'a']);
  }
  if (s.size() != t.size()) {
    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++)
        change[i][j] = min(change[i][j], change[i][k] + change[k][j]);
  string ans = "";
  int mn, now, res = 0;
  for (int i = 0; i < s.size(); i++) {
    mn = 1234567;
    now = -1;
    for (int k = 0; k < 26; k++) {
      if (change[s[i] - 'a'][k] + change[k][t[i] - 'a'] < mn) {
        now = t[i] - 'a';
        mn = change[s[i] - 'a'][k] + change[k][t[i] - 'a'];
      }
      if (change[t[i] - 'a'][k] + change[k][s[i] - 'a'] < mn) {
        now = s[i] - 'a';
        mn = change[t[i] - 'a'][k] + change[k][s[i] - 'a'];
      }
      if (change[t[i] - 'a'][k] + change[s[i] - 'a'][k] < mn) {
        now = k;
        mn = change[t[i] - 'a'][k] + change[s[i] - 'a'][k];
      }
    }
    if (now == -1 || mn >= 123456) {
      cout << -1 << endl;
      return 0;
    }
    ans += (now + 'a');
    res += mn;
  }
  cout << res << endl;
  cout << ans << endl;
  return 0;
}
