#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define FOR(i, j, k, in) for (int i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n, m, x, y;
    cin >> n >> m >> x >> y;
    x--, y--;
    vector<vector<tuple<ll, ll, ll>>> graph(n);
    vector<ll> distance(n, -1);
    REP(i, m) {
        ll ai, bi, ti, ki;
        cin >> ai >> bi >> ti >> ki;
        ai--, bi--;
        graph[ai].emplace_back(bi, ti, ki);
        graph[bi].emplace_back(ai, ti, ki);
    }

    vector<pair<ll, ll>> heap = {{0, x}};
    auto comp = [](const pair<ll, ll> &lhs, const pair<ll, ll> &rhs) {
        return lhs.first > rhs.first;
    };

    while (!heap.empty()) {
        pop_heap(begin(heap), end(heap), comp);
        auto p = heap.back();
        heap.pop_back();
        ll dist = p.first, pos = p.second;
        if (distance[pos] != -1) continue;
        distance[pos] = dist;
        if (pos == y) break;
        for (const auto tup : graph[pos]) {
            ll to = get<0>(tup), ti = get<1>(tup), ki = get<2>(tup);
            if (distance[to] != -1) continue;
            ll start;
            if (dist % ki == 0)
                start = (dist / ki) * ki;
            else
                start = (dist / ki + 1) * ki;
            heap.emplace_back(start + ti, to);
            push_heap(begin(heap), end(heap), comp);
        }
    }
    cout << distance[y] << endl;
    return 0;
}
#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
//#include<atcoder/all>
//using namespace atcoder;

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

char cn[1200010];
char* ci = cn, * di = cn;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	ll tmp = *(ll*)ci;
	int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += 72 - dig >> 3;
	return tmp;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	fread(cn, 1, 1200010, stdin);
	int T = getint();

	rep(t, T) {
		int N = getint();

		rep(i, N)* di++ = '0';
		rep(i, N)* di++ = '1';
		*di++ = '0';
		*di++ = '\n';
		ci += 6 * N + 3;
	}
	fwrite(cn, 1, di - cn, stdout);

	Would you please return 0;
}