#include<bits/stdc++.h>
#define mx 100009
using namespace std;
typedef long long ll;
struct edge
{
string u,v;
ll w;
bool operator < (const edge& p) const
{
return w>p.w;
}
};
map<string,string>pr;
vector<edge>e;
string fr,en;
ll m,n;
string fnd(string r)
{
return (pr[r]=="")?r:fnd(pr[r]);
}
ll mst()
{
sort(e.begin(),e.end());
ll min_val=10000000;
for(ll i=0;i<m;i++)
{
string u=fnd(e[i].u);
string v=fnd(e[i].v);
if(u!=v)
{
pr[u]=v;
if(min_val>e[i].w)
{
//cout<<e[i].w<<endl;
min_val=e[i].w;
}
}
if(fnd(fr)==fnd(en))
{
break;
}
}
return min_val;
}
int main()
{
ll k=1;
while(cin>>n>>m)
{
if(n==0&&m==0)
break;
e.clear();
pr.clear();
for(int i=1;i<=m;i++)
{
string u,v;
ll w;
cin>>u>>v>>w;
edge get;
get.u=u;
get.v=v;
get.w=w;
e.push_back(get);
}
cin>>fr>>en;
cout<<"Scenario #"<<k++<<endl<<mst()<<" tons"<<endl<<endl;
}
return 0;
}
#define mx 100009
using namespace std;
typedef long long ll;
struct edge
{
string u,v;
ll w;
bool operator < (const edge& p) const
{
return w>p.w;
}
};
map<string,string>pr;
vector<edge>e;
string fr,en;
ll m,n;
string fnd(string r)
{
return (pr[r]=="")?r:fnd(pr[r]);
}
ll mst()
{
sort(e.begin(),e.end());
ll min_val=10000000;
for(ll i=0;i<m;i++)
{
string u=fnd(e[i].u);
string v=fnd(e[i].v);
if(u!=v)
{
pr[u]=v;
if(min_val>e[i].w)
{
//cout<<e[i].w<<endl;
min_val=e[i].w;
}
}
if(fnd(fr)==fnd(en))
{
break;
}
}
return min_val;
}
int main()
{
ll k=1;
while(cin>>n>>m)
{
if(n==0&&m==0)
break;
e.clear();
pr.clear();
for(int i=1;i<=m;i++)
{
string u,v;
ll w;
cin>>u>>v>>w;
edge get;
get.u=u;
get.v=v;
get.w=w;
e.push_back(get);
}
cin>>fr>>en;
cout<<"Scenario #"<<k++<<endl<<mst()<<" tons"<<endl<<endl;
}
return 0;
}