#include using namespace std; typedef long long ll; #define int ll #define endl '\n' #define pii pair #define _FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define pb push_back #define pii pair #define F first #define S second #define M_PI 3.14159265358979323846 const int mod = 1000000007; const int MAXX = 1e6 + 5; int t , n , m , x , y , a , b , c , dn; signed main() { _FastIO; scanf("%lld" , &t); while(t--){ scanf("%lld %lld %lld %lld" , &n , &m , &x , &y); map d; map > adj; while(m--){ scanf("%lld %lld %lld %lld" , &a , &b , &c , &dn); adj[a].push_back(c); adj[a].push_back(dn); adj[b].push_back(c); adj[b].push_back(dn); } if(x == y){ putchar('0'); putchar('\n'); continue; } queue q; q.push(x); while(!q.empty()){ int from = q.front(); q.pop(); for(int i = 0; i < adj[from].size(); i++){ int to = adj[from][i]; if(!d[to]){ d[to] = d[from] + 1; q.push(to); } } } if(!d[y]){ printf("-1"); putchar('\n'); continue; } printf("%lld" , d[y]); putchar('\n'); } return 0; }