#include <cassert>
#include <cstdio>
#include <vector>
double solve(int N, int M, int K, int H, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr);
int main() {
int T;
assert(1 == scanf("%d", &T));
while (T--){
int N,M,K,H;
assert(4 == scanf("%d %d %d\n%d", &N, &M, &K, &H));
std::vector<int> x(M);
std::vector<int> y(M);
std::vector<int> c(M);
std::vector<int> arr(N);
for (int i=0;i<N;i++)
assert(1 == scanf("%d", &arr[i]));
for (int i=0;i<M;i++)
assert(3 == scanf("%d %d %d", &x[i], &y[i], &c[i]));
printf("%.12lf\n", solve(N, M, K, H, x, y, c, arr));
}
}
#include <bits/stdc++.h>
using namespace std;
namespace Le0{
const int N=1e5+5,M=7e6+5;
#define fi first
#define se second
#define db double
int n,m,K,t;
int a[N];
db dis[M];
bool vis2[M],vis[M];
tuple<int,int,int> b[N];
vector<pair<int,db>> g[M];
vector<int> g2[N];
void add(int x,int y,db c){g[x].push_back({y,c});}
int gc(int x,int y){return x*n+y;}
int gt(int x){return x%n==0?n:x%n;}
void dfs(int x){
vis2[x]=1;
for(auto y:g2[x]){
if(y!=t&&!vis2[y]){
dfs(y);
}
}
}
void dijk(int s){
priority_queue<pair<db,int>> q;
q.push({0,s});
for(int i=1;i<=gc(K,n);i++) dis[i]=1e18;
dis[s]=0;
while(q.size()){
auto [v,x]=q.top();q.pop();
if(vis[x]) continue;
vis[x]=1;
for(auto [y,w]:g[x]){
if(gt(y)!=t){
if(dis[y]>dis[x]+w){
dis[y]=dis[x]+w;
q.push({-dis[y],y});
}
}
}
}
}
db main(){
K=min(K,67);
for(int i=1;i<=n;i++){
for(int j=0;j<=K;j++){
g[gc(j,i)].clear(),vis[gc(j,i)]=vis2[gc(j,i)]=0;
}
g2[i].clear();
}
for(int i=1;i<=m;i++){
auto [x,y,c]=b[i];
g2[x].push_back(y);
g2[y].push_back(x);
for(int j=0;j<=K;j++){
add(gc(j,x),gc(j,y),c*1.0/(1<<j));
add(gc(j,y),gc(j,x),c*1.0/(1<<j));
}
if(a[y]==2){
for(int j=0;j<K;j++){
add(gc(j,x),gc(j+1,y),c*1.0/(1<<j));
}
}
if(a[x]==2){
for(int j=0;j<K;j++){
add(gc(j,y),gc(j+1,x),c*1.0/(1<<j));
}
}
}
dfs(1);
dijk(gc(0,t));
db ans=1e18;
for(int i=1;i<=n;i++){
for(int j=0;j<=K;j++){
if(a[i]==0||i==1){
if(vis2[i]){
ans=min(ans,dis[gc(j,i)]);
}
}
}
}
return ans==1e18?-1:ans;
}
}
double solve(int n, int m, int K, int t, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr){
Le0::n=n;Le0::m=m;Le0::K=K;Le0::t=t+1;
for(int i=0;i<m;i++){
Le0::b[i+1]={x[i]+1,y[i]+1,c[i]};
}
for(int i=0;i<n;i++) Le0::a[i+1]=arr[i];
return Le0::main();
}