#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll p = 1e9 + 7;
ll n, L, R, q;
ll add(ll x, ll y){ return (x % p + y % p) % p; }
ll mul(ll x, ll y){ return (x % p * y % p) % p; }
ll Pow(ll a, ll b){
if(!b) return 1;
ll cur = Pow(a, b / 2);
cur = mul(cur, cur);
if(b % 2 != 0) cur = mul(cur, a);
return cur;
}
const ll inv = Pow(2, p - 2);
ll C(ll x){
if(x < 2) return 0LL;
return mul(x, mul(x - 1, inv));
}
int main(){
freopen("love.inp", "r", stdin);
freopen("love.out", "w", stdout);
cin >> q;
while(q--){
cin >> L >> R >> n;
ll a = add(C(R - L + 1), R - L + 1);
ll newL = (L - 1) / n, newR = R / n;
ll b = add(C(newR - newL), (newR - newL));
cout << ((a - b) % p + p) % p << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwpjb25zdCBsbCBwID0gMWU5ICsgNzsKbGwgbiwgTCwgUiwgcTsKCgpsbCBhZGQobGwgeCwgbGwgeSl7IHJldHVybiAoeCAlIHAgKyB5ICUgcCkgJSBwOyB9CmxsIG11bChsbCB4LCBsbCB5KXsgcmV0dXJuICh4ICUgcCAqIHkgJSBwKSAlIHA7IH0KCmxsIFBvdyhsbCBhLCBsbCBiKXsKICAgIGlmKCFiKSByZXR1cm4gMTsKICAgIGxsIGN1ciA9IFBvdyhhLCBiIC8gMik7CiAgICBjdXIgPSBtdWwoY3VyLCBjdXIpOwogICAgaWYoYiAlIDIgIT0gMCkgY3VyID0gbXVsKGN1ciwgYSk7CiAgICByZXR1cm4gY3VyOwp9CmNvbnN0IGxsIGludiA9IFBvdygyLCBwIC0gMik7CgpsbCBDKGxsIHgpewogICAgaWYoeCA8IDIpIHJldHVybiAwTEw7CiAgICByZXR1cm4gbXVsKHgsIG11bCh4IC0gMSwgaW52KSk7Cn0KCmludCBtYWluKCl7CiAgICBm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