#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#define pii pair<int, int>
#define piii pair<int, pii>
#define pll pair<long long, long long>
#define plll pair<long long, pll>
#define ff first
#define ss second
#define ee ss.ff
#define rr ss.ss

using namespace std;

vector<pii> gph[101010];
bool vst[101010];
bool color[101010];
int edge[202020];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m; cin >> n >> m;
    int tar[n]; for(auto &i : tar) cin >> i;
    for(int i = 0; i < m; ++i)
    {
        int s, e; cin >> s >> e; --s; --e;
        gph[s].push_back({e, i});
        gph[e].push_back({s, i});
    }

    int ord[n]; for(int i = 0; i < n; ++i) ord[i] = i;
    sort(ord, ord + n, [&tar](int x, int y){return tar[x] < tar[y];});

    for(auto x : ord)
    {
        if(vst[x]) continue;

        for(auto y : gph[x])
        {
            if(vst[y.ff])
            {
                vst[x] = true;
                color[x] = !color[y.ff];
                edge[y.ss] = tar[x];
                break;
            }
        }

        if(vst[x]) continue;

        for(auto y : gph[x])
        {
            if(tar[x] == tar[y.ff])
            {
                vst[x] = true;
                vst[y.ff] = true;
                color[x] = true;
                color[y.ff] = false;
                edge[y.ss] = tar[x];
                break;
            }
        }

        if(vst[x]) continue;

        cout << -1;
        return 0;
    }

    for(int i = 0; i < n; ++i) cout << (color[i] ? 'B' : 'W'); cout << '\n';
    for(int i = 0; i < m; ++i) cout << (edge[i] == 0 ? (int)1e9 : edge[i]) << '\n';
    return 0;
}
