#include <bits/stdc++.h>
typedef long long ll;
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = (ll)(a); i <= (ll)(b); i++ )
#define ROF(i,a,b) for( ll i = (ll)(a); i >= (ll)(b); i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl;
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define INPFILE freopen("input.in","r",stdin)
#define OUTFILE freopen("output.out","w",stdout)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

ll parent[70];
ll ans[70];
set<tuple<ll,ll,ll>> rules;

ll find(ll x) {
  if(x == parent[x]) return x;
  return parent[x] = find(parent[x]);
}

void join(ll x, ll y) {
  ll p = find(x);
  ll q = find(y);

  if(p == q) return;

  parent[p] = q;
}

void reset() {
  FOR(i,0,69) parent[i] = i;
}

bool rec(set<ll> Q,  ll last) {
  //debug(last);
  //for(ll x : Q) cerr << x << ' ';
  //cerr << '\n';
  if(Q.empty()) return true;
  set<tuple<ll,ll,ll>> v;
  for(auto p : rules) {
    ll x = get<0>(p);
    ll y = get<1>(p);
    ll z = get<2>(p);

    bool hasx = Q.find(x) != Q.end();
    bool hasy = Q.find(y) != Q.end();
    bool hasz = Q.find(z) != Q.end();

    if(hasz && (!hasx || !hasy)) return false;
    else if(hasz || (hasx && hasy)) v.insert(p);
  }

  for(ll cur : Q) {
    reset();
    bool pos = true;
    for(auto p : v) {
      ll x = get<0>(p);
      ll y = get<1>(p);
      ll z = get<2>(p);

      if(cur != z) {
        join(x, z);
        join(y, z);
      }
      if((cur == x || cur == y) && cur != z) {
        pos = false;
        break;
      }
    }
    for(auto p : v) {
      ll x = get<0>(p);
      ll y = get<1>(p);
      ll z = get<2>(p);

      if(cur == z && find(x) == find(y)) {
        pos = false;
        break;
      }
    }

    if(pos) {
      map<ll,set<ll>> grp;
      for(ll x : Q) if(x != cur) {
        grp[find(x)].insert(x);
      }
      ans[cur] = last;
      bool ret = true;
      for(auto it : grp) {
        ret &= rec(it.S, cur);
      }
      return ret;
    }
  }

  return false;
}

int main() {
  ll _t, _c = 1; cin >> _t;
  while(_t--) {
    FOR(i,0,69) ans[i] = -1;
    rules.clear();
    ll n, m;
    cin >> n >> m;

    while(m--) {
      ll x, y, z;
      cin >> x >> y >> z;
      rules.insert({min(x,y), max(x,y), z});
    }

    set<ll> Q;
    FOR(i,1,n) Q.insert(i);

    bool ret = rec(Q, 0);
    cout << "Case #" << _c++ << ": ";
    if(!ret) {
      cout << "Impossible\n";
    } else {
      FOR(i,1,n) cout << ans[i] << ' ';
      cout << '\n';
    }
  }
}