#include using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return vector(arg, init); else return vector(arg, vec(init, args...)); } template auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template auto distance(const Container &c, decltype(begin(c)) iter) { return distance(begin(c), iter); } template ::value_type>> auto isort(RIter first, RIter last, Compare comp = Compare()) { vector i(distance(first, last)); iota(begin(i), end(i), 0); sort(begin(i), end(i), [&](auto x, auto y) { return comp(*(first + x), *(first + y)); }); return i; } template typename, typename = void_t<>> struct detect : false_type {}; template typename Check> struct detect>> : true_type {}; template typename Check> constexpr inline bool detect_v = detect::value; template using has_member_sort = decltype(declval().sort()); template > auto sorted(Container c, Compare comp = Compare()) { if constexpr (detect_v) { c.sort(comp); return c; } else { sort(begin(c), end(c), comp); return c; } } template > auto uniqued(Container c, Compare comp = Compare()) { c.erase(unique(begin(c), end(c), comp), end(c)); return c; } template > T &chmin(T &l, T r, Compare &&f = less()) { return l = min(l, r, f); } template > T &chmax(T &l, T r, Compare &&f = less()) { return l = max(l, r, f); } template struct matrix { matrix(): matrix(0, 0) { } matrix(size_t h, size_t w, T init = E1()): h_(h), w_(w), mat_(h * w, std::move(init)) { } matrix(const matrix &r): h_(r.h_), w_(r.w_), mat_(r.mat_) { } matrix(matrix &&r): h_(r.h_), w_(r.w_), mat_() { mat_ = std::move(r.mat_); } matrix &operator =(const matrix &r) { h_ = r.h_; w_ = r.w_; mat_ = r.mat_; return *this; } matrix &operator =(matrix &&r) { h_ = r.h_; w_ = r.w_; mat_ = std::move(r.mat_); return *this; } size_t height() const noexcept { return h_; } size_t width() const noexcept { return w_; } T &at(size_t y, size_t x) { return mat_[w_ * y + x]; } const T &at(size_t y, size_t x) const { return mat_[w_ * y + x]; } matrix &operator +=(const matrix &r) { assert(h_ == r.h_ && w_ == r.w_); for (size_t i = 0; i < w_; ++i) { for (size_t j = 0; j < h_; ++j) { at(i, j) = Op(at(i, j), r.at(i, j)); } } return *this; } matrix operator +(const matrix &r) { return matrix(*this) += r; } matrix operator *(const matrix &r) { assert(w_ == r.h_); matrix m(h_, r.w_); for (size_t i = 0; i < h_; ++i) { for (size_t j = 0; j < r.w_; ++j) { for (size_t k = 0; k < w_; ++k) { m.at(i, j) = Op(m.at(i, j), Prod(at(i, k), r.at(k, j))); } } } return m; } private: size_t h_, w_; std::vector mat_; }; constexpr auto inf = INT64_MAX / 2; constexpr int64_t e1() { return -inf; } constexpr int64_t e2() { return 0; } constexpr int64_t op(int64_t x, int64_t y) { return max(x, y); } constexpr int64_t prod(int64_t x, int64_t y) { return x + y; } using mat = matrix; int main() { constexpr int alpha = 'z' - 'a' + 1; constexpr auto lim = 16; array c; for (auto &e : c) { cin >> e; --e; } array k; for (auto &e : k) cin >> e; int n; cin >> n; auto edge = vec>(uns, n); for (auto &[bits, a, b, e] : edge) { string s; cin >> s >> a >> b >> e; --a; --b; bits = 0; for (auto c : s) { bits |= (1 << (c - 'A')); } } auto dist = vec(uns, alpha); for (int i = 0; i < alpha; ++i) { mat m(lim, lim); for (auto [bits, a, b, e] : edge) { if (!(bits & (1 << i))) { continue; } chmax(m.at(a, b), e); chmax(m.at(b, a), e); } for (int j = 0; j < lim; ++j) { m.at(j, j) = 0; } mat x(lim, lim); for (int j = 0; j < lim; ++j) { x.at(j, j) = 0; } int y = k[i]; while (0 < y) { if (y & 1) { x = m * x; } m = m * m; y >>= 1; } mat r(lim, 1); r.at(c[i], 0) = 0; dist[i] = x * r; } int64_t ans = -inf; for (int i = 0; i < lim; ++i) { bool found = false; int64_t sum = 0; for (int j = 0; j < alpha; ++j) { if (-inf / 10 < dist[j].at(i, 0)) { sum += dist[j].at(i, 0); } else { found = true; } } if (found) { continue; } chmax(ans, sum); } if (ans != -inf) { cout << ans << endl; } else { cout << "Impossible" << endl; } }