#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define vi vector<int>
#define wgraph vector<pair<int,int>>
#define ll long long int
#define pb push_back
const int MAX_INT = numeric_limits<int>::max();
const int MIN_INT = numeric_limits<int>::min();
int main() {
//REMINDER DONT USE endl use "\n"
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int cases;
cin >> cases;
for (int i = 1; i <= cases; ++i) {
int n, lines, t;
cin >> n >> lines;
vector<pair<int,int>> adj[n];
for (int j = 0; j < lines; ++j) {
int u, v, w;
cin >> u;
cin >> v;
cin >> w;
pair<int,int> temp {v, w};
pair<int,int> temp2 {u, w};
adj[u].push_back(temp);
adj[v].push_back(temp2);
}
cin >> t;
int x = t;
priority_queue<pair<int,int>> q;
bool processed[n] = {false};
int distance[n];
for (int i = 0; i < n; i++) {
distance[i] = MAX_INT;
}
distance[x] = 0;
q.push({0,x});
while (!q.empty()) {
int a = q.top().second; q.pop();
if (processed[a]) continue;
processed[a] = true;
for (auto u : adj[a]) {
int b = u.first, w = u.second;
int bottleneckLarge = max(distance[a], w);
if (bottleneckLarge < distance[b]) {
distance[b] = bottleneckLarge;
q.push({-bottleneckLarge,b});
}
}
}
cout << "Case " << i << ":" << '\n';
for (int j = 0; j < n; ++j) {
if (j == t) {
cout << "0" << '\n';
} else {
int temp = distance[j];
if (temp == MAX_INT) {
cout << "Impossible" << "\n";
} else {
cout << temp << "\n";
}
}
}
}
return 0;
}