#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define MC(a,b) memcpy(a,b,sizeof(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define read freopen("out.txt","r",stdin)
#define write freopen("out2.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-6;
const double pi = acos(-1.0);
const int maxn = 10111;
struct Node{
int now;
int to;
int c;
};
int n, m, s, t;
vector<Node>g[maxn];
int be[maxn];
bool vis[maxn];
queue<int>q;
int nb;
int mt[1000][1000];
double mx[1000][1000];
double ans[1000];
int yes[maxn];
void bfs0(){
while (!q.empty()) q.pop();
for (int i = 0; i <= n; i++){
yes[i] = false;
}
q.push(s);
yes[s] = true;
int now, to;
while (!q.empty()){
now = q.front();
q.pop();
for (int i = 0; i < g[now].size(); i++){
to = g[now][i].to;
if (!yes[to]){
yes[to] = true;
q.push(to);
}
}
}
}
void bfs(int now){
while (!q.empty())q.pop();
q.push(now);
vis[now] = true;
be[now] = nb;
int to;
while (!q.empty()){
now = q.front();
q.pop();
for (int i = 0; i < g[now].size(); i++){
if (g[now][i].c == 0){
to = g[now][i].to;
if (!vis[to]){
vis[to] = true;
be[to] = nb;
q.push(to);
}
}
}
}
}
void change(int row){
int temp;
for (int x = row; x <= nb; x++){
if (abs(mx[x][row]) > eps){
temp = x;
break;
}
}
for (int y = 0; y <= nb; y++){
swap(mx[row][y], mx[temp][y]);
}
}
double start(){
bfs0();
if (!yes[t]){
return -1;
}
nb = 1;
for (int i = 1; i <= n; i++){
vis[i] = false;
}
for (int i = 1; i <= n; i++){
if (yes[i] && !vis[i]) {
bfs(i);
nb++;
}
}
nb--;
for (int i = 0; i <= nb; i++){
for (int j = 0; j <= nb; j++){
mt[i][j] = 0;
mx[i][j] = 0;
}
}
int to;
for (int now = 1; now <= n; now++){
for (int i = 0; i < g[now].size(); i++){
to = g[now][i].to;
mt[be[now]][be[to]]++;
}
}
int begin = be[s];
int end = be[t];
for (int i = 1; i <= nb; i++){
for (int j = 1; j <= nb; j++){
mx[i][i] -= mt[i][j];
mx[i][j] += mt[i][j];
}
}
mx[begin][0] = 1.0;
mx[end][0] = -1.0;
double temp;
for (int row = 1; row < nb; row++){
change(row);
temp = mx[row][row];
for (int y = 0; y <= nb; y++){
mx[row][y] /= temp;
}
for (int x = row+1; x <= nb; x++){
temp = mx[x][row];
for (int y = 0; y <= nb; y++){
mx[x][y] -= temp*mx[row][y];
}
}
}
for (int i = 1; i <= nb; i++){
ans[i] = 0;
}
for (int x = nb - 1; x >= 1; x--){
ans[x] = mx[x][0];
for (int y = nb; y > x; y--){
ans[x] -= ans[y] * mx[x][y];
}
}
return ans[end] - ans[begin];
}
int main(){
int T;
cin >> T;
while (T--){
cin >> n >> m >> s >> t;
for (int i = 0; i <= n; i++){
g[i].clear();
}
Node node;
for (int i = 1; i <= m; i++){
//cin >> node.now >> node.to >> node.c;
SS(node.now); SS(node.to); SS(node.c);
g[node.now].push_back(node);
swap(node.now, node.to);
g[node.now].push_back(node);
}
double ss = start();
if (ss <0){
cout << "inf" << endl;
}
else{
printf("%.6lf\n", ss);
}
}
return 0;
}