#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
pair<int, int> edges[N];
int adj[N][2];
int conf[N];
int ass[N];
int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count());
  uniform_int_distribution<int>(1, 1000 * 1000 * 1000)(rng);
  vector<int> permutation;
  shuffle(permutation.begin(), permutation.end(), rng);
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, e;
  cin >> n >> e;
  for (int i = 1; i <= e; i++) {
    cin >> edges[i].first >> edges[i].second;
  }
  shuffle(edges + 1, edges + e + 1, rng);
  for (int i = 1; i < N; i++) {
    conf[i] = uniform_int_distribution<int>(1, 2)(rng);
  }
  int T = 1;
  for (int i = 1; i <= n; i++) {
    int l;
    cin >> l;
    while (l--) {
      int a;
      cin >> a;
      adj[i][conf[a]] = a;
      T = max(T, a);
    }
    if (adj[i][1] == 0 or adj[i][2] == 0) {
      ass[i] = adj[i][1] ^ adj[i][2];
    }
  }
  for (int i = 1; i <= e; i++) {
    int a = edges[i].first, b = edges[i].second;
    if (ass[a] and ass[b])
      ;
    else if (ass[a]) {
      ass[b] = adj[b][3 - conf[ass[a]]];
    } else if (ass[b]) {
      ass[a] = adj[a][3 - conf[ass[b]]];
    } else {
      ass[a] = adj[a][uniform_int_distribution<int>(1, 2)(rng)];
      ass[b] = adj[b][3 - conf[ass[a]]];
    }
  }
  for (int i = 1; i <= n; i++) {
    if (ass[i] == 0) {
      ass[i] = adj[i][1];
    }
    cout << ass[i] << " ";
  }
  cout << "\n";
  for (int i = 1; i <= T; i++) {
    cout << conf[i] << " ";
  }
  cout << "\n";
  return 0;
}
