#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
#define CRP(t, x) const t &x
#define OPX(tr, op, t, x) tr operator op(CRP(t, x)) const
#define OPL(t, x) OPX(bool, <, t, x)
#define FIL(x, v) memset(x, v, sizeof(x))
#define CLR(x) FIL(x, 0)
#define NE1(x) FIL(x, -1)
#define INF(x) FIL(x, 0x3f)
#ifndef _DEBUG
#define _DEBUG 0
#endif // !_DEBUG
#define IFD if (_DEBUG)
typedef long long ll, i64;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
const ll mod = 1e9 + 7;
inline ll fpow(ll a, ll b)
{
    ll r = 1;
    for (; b; b >>= 1, a = a * a % mod)
        if (b & 1)
            r = r * a % mod;
    return r;
}
inline ll inv(ll x) { return fpow(x, mod - 2); }
inline char getchar(int)
{
    static char buf[64 << 20], *S = buf, *T = buf;
    if (S == T) T = fread(S = buf, 1, 64 << 20, stdin) + S;
    return S == T ? EOF : *S++;
}
template <typename T>
inline typename enable_if<is_integral<T>::value>::type read(T &x)
{
    int ch = x = 0;
    while (!isdigit(ch)) ch = getchar();
    for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
}
const int N = 5e5 + 50;
ll r[N], s[N], x[N], a[N];
struct frac
{
    ll h, l;
    OPX(frac, +, frac, rhs)
    {
        return frac{h * rhs.l + l * rhs.h, l * rhs.l}.reduce();
    }
    OPX(frac, -, frac, rhs)
    {
        return frac{h * rhs.l + l * -rhs.h, l * rhs.l}.reduce();
    }
    OPX(frac, *, frac, rhs)
    {
        return frac{h * rhs.h, l * rhs.l}.reduce();
    }
    OPX(frac, /, frac, rhs)
    {
        return frac{h * rhs.l, l * rhs.h}.reduce();
    }
    frac &reduce()
    {
        if (l == 0) l = 1;
        ll g = gcd(h, l);
        if (g > 1) h /= g, l /= g;
        return *this;
    }
    void print()
    {
        reduce();
        printf("%lld\n", ll(h * inv(l) % mod));
    }
};
ll psum[N];
int main()
{
    int T, n, q;
    read(T);
    while (T--)
    {
        read(n), read(q);
        for (int i = 1; i <= n; i++) read(r[i]), read(s[i]), read(x[i]), read(a[i]);
        /*psum[0] = psum[1] = {0, 1};
        for (int i = 1; i <= n; i++)
            psum[i + 1] = psum[i] +
                          frac{a[i], 1} * frac{s[i], r[i]} +
                          (psum[i] - psum[x[i]]) * frac{s[i] - r[i], r[i]};
        for (int i = 1, l, r; i <= q; i++)
            read(l), read(r), (psum[r] - psum[l]).print();*/
        for (int i = 1; i <= n; i++)
            psum[i + 1] = (psum[i] + (a[i] * s[i] + (s[i] - r[i]) * (psum[i] - psum[x[i]] + mod)) % mod * inv(r[i])) % mod;
        for (int i = 1, l, r; i <= q; i++)
            read(l), read(r), printf("%lld\n", (psum[r] - psum[l] + mod) % mod);
    }
    return 0;
}