#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define bug(a) cerr << #a << " : " << a << endl;
#define FastRead ios_base::sync_with_stdio(false);cin.tie(NULL);
const int MAX = 3e6+10;
vector<pii>adj[MAX],newG[MAX];
map<pii,int>vis;
int node;
ll dist[MAX];
struct Info
{
ll d;
int u,x;
Info(){}
Info(ll _d,int _u,int _x)
{
d = _d;
u = _u;
x = _x;
}
};
bool operator<(const Info &x,const Info &y)
{
return x.d >= y.d;
}
bool endd[MAX];
int en;
void DFS(int src,int x,int par)
{
if(src == en)
endd[node] = 1;
vis[{src,x}] = node++;
int u = vis[{src,x}];
//cout << src << " " << x << " " << par << endl;
for(int j=0;j<adj[src].size();j++)
{
int v = adj[src][j].first , w = adj[src][j].second;
int newX = __gcd(x,w);
if(x >= w)
{
if(!vis[{v,newX}] && v != par)
{
DFS(v,newX,u);
v = vis[{v,newX}];
newG[u].push_back({v,w});
newG[v].push_back({u,w});
}
// else if(v != par)
// {
// v = vis[{v,newX}];
// newG[u].push_back({v,w});
// newG[v].push_back({u,w});
// }
}
}
}
ll dijkstra(int src,int en,int x)
{
priority_queue< Info > pq;
fill(dist,dist+MAX,1e18);
pq.push({0,src,x});
dist[src] = 0;
while(pq.size())
{
int u = pq.top().u;
ll d = pq.top().d;
pq.pop();
//cout << u << " >>> " << d << endl;
if(endd[u])
return d;
for(int j=0;j<newG[u].size();j++)
{
int v = newG[u][j].first , w = newG[u][j].second;
if(dist[v] > dist[u]+w)
{
dist[v] = dist[u]+w;
pq.push({dist[v],v,__gcd(x,w)});
}
}
}
return 1e18;
}
int main()
{
FastRead
int t,cas=1;
cin >> t;
while(t--)
{
int n,m,x,y,w;
cin >> n >> m;
for(int i=1;i<=n;i++)
adj[i].clear() , newG[i].clear();
while(m--)
{
cin >> x >> y >> w;
adj[x].push_back({y,w});
adj[y].push_back({x,w});
}
vis.clear();
memset(endd,0,sizeof endd);
node = 1;
int st;
cin >> st >> en >> x;
DFS(st,x,-1);
ll res = dijkstra(vis[{st,x}],en,x);
if(res < 1e18)
cout << "Case " << cas++ << ": " << res << endl;
else
cout << "Case " << cas++ << ": impossible" << endl;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgbGwgICAgICAgICAgIGxvbmcgbG9uZwojZGVmaW5lIHBpaSAgICAgICAgICBwYWlyPGxsLGxsPgojZGVmaW5lIGJ1ZyhhKSAgICAgICBjZXJyIDw8ICNhIDw8ICIgOiAiIDw8IGEgPDwgZW5kbDsKI2RlZmluZSBGYXN0UmVhZCAgICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7Y2luLnRpZShOVUxMKTsKCmNvbnN0IGludCBNQVggPSAzZTYrMTA7Cgp2ZWN0b3I8cGlpPmFkaltNQVhdLG5ld0dbTUFYXTsKbWFwPHBpaSxpbnQ+dmlzOwppbnQgbm9kZTsKbGwgZGlzdFtNQVhdOwoKc3RydWN0IEluZm8KewogICAgbGwgZDsKICAgIGludCB1LHg7CgogICAgSW5mbygpe30KICAgIEluZm8obGwgX2QsaW50IF91LGludCBfeCkKICAgIHsKICAgICAgICBkID0gX2Q7CiAgICAgICAgdSA9IF91OwogICAgICAgIHggPSBfeDsKICAgIH0KfTsKYm9vbCBvcGVyYXRvcjwoY29uc3QgSW5mbyAmeCxjb25zdCBJbmZvICZ5KQp7CiAgICByZXR1cm4geC5kID49IHkuZDsKfQpib29sIGVuZGRbTUFYXTsKaW50IGVuOwp2b2lkIERGUyhpbnQgc3JjLGludCB4LGludCBwYXIpCnsKICAgIGlmKHNyYyA9PSBlbikKICAgICAgICBlbmRkW25vZGVdID0gMTsKICAgIHZpc1t7c3JjLHh9XSA9IG5vZGUrKzsKICAgIGludCB1ID0gdmlzW3tzcmMseH1dOwogICAgLy9jb3V0IDw8IHNyYyA8PCAiICIgPDwgeCA8PCAiICIgPDwgcGFyIDw8IGVuZGw7CiAgICBmb3IoaW50IGo9MDtqPGFkaltzcmNdLnNpemUoKTtqKyspCiAgICB7CiAgICAgICAgaW50IHYgPSBhZGpbc3JjXVtqXS5maXJzdCAsIHcgPSBhZGpbc3JjXVtqXS5zZWNvbmQ7CiAgICAgICAgaW50IG5ld1ggPSBfX2djZCh4LHcpOwoKICAgICAgICBpZih4ID49IHcpCiAgICAgICAgewogICAgICAgICAgICBpZighdmlzW3t2LG5ld1h9XSAmJiB2ICE9IHBhcikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgREZTKHYsbmV3WCx1KTsKICAgICAgICAgICAgICAgIHYgPSB2aXNbe3YsbmV3WH1dOwogICAgICAgICAgICAgICAgbmV3R1t1XS5wdXNoX2JhY2soe3Ysd30pOwogICAgICAgICAgICAgICAgbmV3R1t2XS5wdXNoX2JhY2soe3Usd30pOwogICAgICAgICAgICB9Ci8vICAgICAgICAgICAgZWxzZSBpZih2ICE9IHBhcikKLy8gICAgICAgICAgICB7Ci8vICAgICAgICAgICAgICAgIHYgPSB2aXNbe3YsbmV3WH1dOwovLyAgICAgICAgICAgICAgICBuZXdHW3VdLnB1c2hfYmFjayh7dix3fSk7Ci8vICAgICAgICAgICAgICAgIG5ld0dbdl0ucHVzaF9iYWNrKHt1LHd9KTsKLy8gICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CmxsIGRpamtzdHJhKGludCBzcmMsaW50IGVuLGludCB4KQp7CiAgICBwcmlvcml0eV9xdWV1ZTwgSW5mbyA+IHBxOwogICAgZmlsbChkaXN0LGRpc3QrTUFYLDFlMTgpOwoKICAgIHBxLnB1c2goezAsc3JjLHh9KTsKICAgIGRpc3Rbc3JjXSA9IDA7CgogICAgd2hpbGUocHEuc2l6ZSgpKQogICAgewogICAgICAgIGludCB1ID0gcHEudG9wKCkudTsKICAgICAgICBsbCBkID0gcHEudG9wKCkuZDsKICAgICAgICBwcS5wb3AoKTsKICAgICAgICAvL2NvdXQgPDwgdSA8PCAiID4+PiAiIDw8IGQgPDwgZW5kbDsKICAgICAgICBpZihlbmRkW3VdKQogICAgICAgICAgICByZXR1cm4gZDsKCiAgICAgICAgZm9yKGludCBqPTA7ajxuZXdHW3VdLnNpemUoKTtqKyspCiAgICAgICAgewogICAgICAgICAgICBpbnQgdiA9IG5ld0dbdV1bal0uZmlyc3QgLCB3ID0gbmV3R1t1XVtqXS5zZWNvbmQ7CgogICAgICAgICAgICBpZihkaXN0W3ZdID4gZGlzdFt1XSt3KQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBkaXN0W3ZdID0gZGlzdFt1XSt3OwogICAgICAgICAgICAgICAgcHEucHVzaCh7ZGlzdFt2XSx2LF9fZ2NkKHgsdyl9KTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAxZTE4Owp9CmludCBtYWluKCkKewogICAgRmFzdFJlYWQKCiAgICBpbnQgdCxjYXM9MTsKCiAgICBjaW4gPj4gdDsKCiAgICB3aGlsZSh0LS0pCiAgICB7CiAgICAgICAgaW50IG4sbSx4LHksdzsKCiAgICAgICAgY2luID4+IG4gPj4gbTsKCiAgICAgICAgZm9yKGludCBpPTE7aTw9bjtpKyspCiAgICAgICAgICAgIGFkaltpXS5jbGVhcigpICwgbmV3R1tpXS5jbGVhcigpOwoKICAgICAgICB3aGlsZShtLS0pCiAgICAgICAgewogICAgICAgICAgICBjaW4gPj4geCA+PiB5ID4+IHc7CiAgICAgICAgICAgIGFkalt4XS5wdXNoX2JhY2soe3ksd30pOwogICAgICAgICAgICBhZGpbeV0ucHVzaF9iYWNrKHt4LHd9KTsKICAgICAgICB9CgogICAgICAgIHZpcy5jbGVhcigpOwogICAgICAgIG1lbXNldChlbmRkLDAsc2l6ZW9mIGVuZGQpOwogICAgICAgIG5vZGUgPSAxOwogICAgICAgIGludCBzdDsKCiAgICAgICAgY2luID4+IHN0ID4+IGVuID4+IHg7CgogICAgICAgIERGUyhzdCx4LC0xKTsKCgogICAgICAgIGxsIHJlcyA9IGRpamtzdHJhKHZpc1t7c3QseH1dLGVuLHgpOwoKICAgICAgICBpZihyZXMgPCAxZTE4KQogICAgICAgICAgICBjb3V0IDw8ICJDYXNlICIgPDwgY2FzKysgPDwgIjogIiA8PCByZXMgPDwgZW5kbDsKICAgICAgICBlbHNlCiAgICAgICAgICAgIGNvdXQgPDwgIkNhc2UgIiA8PCBjYXMrKyA8PCAiOiBpbXBvc3NpYmxlIiA8PCBlbmRsOwogICAgfQp9Cg==