#include <bits/stdc++.h>
using namespace std;
int conn[26][26];
int main() {
  string a, b;
  cin >> a >> b;
  int n;
  cin >> n;
  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < 26; j++) {
      if (i != j) conn[i][j] = 1000000;
    }
  }
  for (int i = 0; i < n; i++) {
    char c1, c2;
    cin >> c1 >> c2;
    int k1 = (int)c1 - 97;
    int k2 = (int)c2 - 97;
    int cost;
    cin >> cost;
    conn[k1][k2] = min(conn[k1][k2], cost);
  }
  for (int k = 0; k < 26; k++)
    for (int i = 0; i < 26; i++)
      for (int j = 0; j < 26; j++)
        conn[i][j] = min(conn[i][j], conn[i][k] + conn[k][j]);
  if (a.size() != b.size()) {
    cout << -1;
    return 0;
  }
  int ans = 0;
  string strans = "";
  for (int i = 0; i < a.size(); i++) {
    int x = (int)a[i] - 97;
    int y = (int)b[i] - 97;
    int to_add = 1000000;
    char strto_add;
    for (int j = 0; j < 26; j++) {
      int cur = conn[x][j] + conn[y][j];
      if (cur < to_add) {
        to_add = cur;
        strto_add = (char)j + 97;
      }
    }
    if (to_add == 1000000) {
      cout << -1;
      return 0;
    }
    ans += to_add;
    strans += strto_add;
  }
  cout << ans << endl;
  cout << strans;
}
