#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define vec std::vector
using ll = long long;

template<class T> std::istream &operator >> (std::istream &is, std::vector<T> & v) {
  for(T &x : v) is >> x;
  return is;
}

int main() {
    vec<string> s(3); cin >> s;

    set<char> st;
    rep(i, 3) {
        for(auto &c : s[i]) st.insert(c);
    }

    if(st.size() > 10) {
        cout << "UNSOLVABLE" << '\n';
        return 0;
    }

    vector<char> ch;
    for(auto &c : st) ch.push_back(c);
    while(ch.size() < 10) ch.push_back('0');
    sort(ch.begin(), ch.end());

    vec<int> num(26, -1);

    auto tol = [&](string str) -> ll {
        ll res = 0, x = 1;
        for(int i = str.size() - 1; i >= 0; --i) {
            res += x * num[str[i] - '0'];
            x *= 10;
        }
        return res;
    };

    do {
        rep(i, 10) {
            if(ch[i] == '0') continue;
            num[ch[i] - '0'] = i;
        }
        ll a = tol(s[0]);
        ll b = tol(s[1]);
        ll c = tol(s[2]);
        if(!a || !b || !c) continue;;
        if(to_string(a).size() != s[0].size()) continue;
        if(to_string(b).size() != s[1].size()) continue;
        if(to_string(c).size() != s[2].size()) continue;
        if(a + b == c) {
            cout << a << '\n';
            cout << b << '\n';
            cout << c << '\n';
            return 0;
        }

    } while(next_permutation(ch.begin(), ch.end()));

    cout << "UNSOLVABLE" << '\n';
    return 0;
}

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
#define all(v) v.begin(), v.end()
using in = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
const in MOD = 1000000007;
const in INF=(in)1e18+7;
const int inf=max(int(1e18)+7,int(1e9)+7);
using P=pair<int,int>;
using T=tuple<int,int,int>;
vector<int> dx={0,1,-1,0};
vector<int> dy={1,0,0,-1};
template <typename Typ>
bool chmin(Typ &a, const Typ& b) {if (a > b) {a = b; return true;} return false;}
template <typename Typ>
bool chmax(Typ &a, const Typ& b) {if (a < b) {a = b; return true;} return false;}
template <typename Typ>
Typ ceil(Typ x, Typ y){return (x+y-1)/y;}
#ifdef  _DEBUG
    #define debug(x) cout<<"debug:"<<x<<endl
    #define vdebug(x) cout<<"debug:";\
                      for(auto asdf:x) cout<<asdf<<" ";\
                      cout<<endl
#else
    #define debug(x) 
    #define vdebug(x) 
#endif

int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
//cout << fixed << setprecision(12);
    int n,m;  cin>>n>>m;
    vector<int> a(m),b(m);
        rep(i,m) cin>>a[i]>>b[i];
    int k;  cin>>k;
    vector<int> c(k),d(k);
        rep(i,k) cin>>c[i]>>d[i];
    int ans=0;
    for(int tmp = 0;tmp< (1<<k); tmp++){
        bitset<16> bt(tmp);
        vector<bool> num(n,false);
        rep(i,k){
            if(bt.test(i)) num[d[i]-1]=true;
            else num[c[i]-1]=true;
        }
        int cnt=0;
        rep(i,m){
            if(num[a[i]-1] and num[b[i]-1]) cnt ++;
        }
        chmax(ans,cnt);
    }
    cout<<ans<<"\n";
    
}/*
./problem.exe
*/