#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MODD = 1e9 + 7;
int q;
ll a, b, u, v, k;

ll calc(ll u, ll v, ll k, ll o = 0) {
    if (u > v) swap(u, v);
    if (u < 1 || k <= 0) return 0;

    ll p = 1, res = 0;
    while (p*2LL <= v) p *= 2LL;

    ll tmp = min(p, k);

    if (u <= p) {
        res = (tmp*(tmp+1)/2 % MODD) * u % MODD;
        res += (o * tmp % MODD) * u % MODD + calc(u, v-p, k-p, o+p);
        res %= MODD;
    } else {
        res = (tmp*(tmp+1)/2 % MODD) * p % MODD;
        res += (o * tmp % MODD) * p % MODD;
        res += calc(u-p, v-p, k, o)+calc(p, v-p, k-p, o+p)+calc(u-p, p, k-p, o+p);
    }

    return res;
}

int main() {
    scanf("%d", &q);

    while (q --> 0) {
        scanf("%lld %lld %lld %lld %lld", &a, &b, &u, &v, &k);

        ll res = (calc(u,v,k) + calc(a-1,b-1,k)) % MODD;
        res = (res - calc(a-1,v,k) - calc(u,b-1,k) + MODD*MODD) % MODD;
        printf("%lld\n", res);
    }
    return 0;
}

#include <cstdio>
#include <cstring>

#define mod (1000000007)

int dp[33][2][2][2], sum[33][2][2][2];

int solve(int n, int m, int k){
	if(n <= 0 || m <= 0 || k <= 0) return 0;

	memset(dp , 0, sizeof(dp));
	memset(sum, 0, sizeof(sum));

	dp [32][1][1][1] = 1;
	sum[32][1][1][1] = 0;

	for(int i = 31; i >= 0; -- i){
		int x = (n >> i) & 1;
		int y = (m >> i) & 1;
		int z = (k >> i) & 1;
		for(int a = 0; a < 2; ++ a) for(int b = 0; b < 2; ++ b) for(int c = 0; c < 2; ++ c){
			//dp[i + 1][a][b][c] => dp[i][aa][bb][cc](using xx, yy, zz while n, m, k is x, y, z)
			for(int xx = 0; xx < 2; ++ xx) for(int yy = 0; yy < 2; ++ yy){
				int zz = xx ^ yy;

				if((a && xx > x) || (b && yy > y) || (c && zz > z)) continue;

				int aa = a & (xx == x), bb = b & (yy == y), cc = c & (zz == z);

				#define add(a, b) (a = (a + b) % mod)
				#define sub(a, b) (a = (a - b + mod) % mod)

				add(dp [i][aa][bb][cc], dp [i + 1][a][b][c]);
				add(sum[i][aa][bb][cc], sum[i + 1][a][b][c]);
				add(sum[i][aa][bb][cc], zz * (1ll << i) % mod * dp[i + 1][a][b][c] % mod);
			}
		}
	}

	return (dp[0][0][0][0] + sum[0][0][0][0]) % mod;
}

int main(){
	int q;
	scanf("%d", &q);
	while(q --){
		int x1, y1, x2, y2, k;
		scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
		int ans = 0;
		add(ans, solve(x2    , y2    , k));
		add(ans, solve(x1 - 1, y1 - 1, k));
		sub(ans, solve(x1 - 1, y2    , k));
		sub(ans, solve(x2    , y1 - 1, k));
		printf("%d\n", ans);
	}
    return 0;
}

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
inline void Rd(int &res){
	char c;res=0;
	while(c=getchar(),c<'0');
	do{
		res=(res<<1)+(res<<3)+(c^48);
	}while(c=getchar(),c>='0');
}
const int N=32;
const int M=35;
const int P=(int)1e9+7;
int q;
int dp1[M][2][2][2],dp2[M][2][2][2],X[M],Y[M],K[M];
inline void Add(int &a,int b){
	a=(a+b)%P;
}
int solve(int x,int y,int k){
	if(x<0||y<0)return 0;
	memset(dp1,0,sizeof(dp1));
	memset(dp2,0,sizeof(dp2));
	for(int i=1;i<=N;i++){
		X[i]=(x&1),Y[i]=(y&1),K[i]=(k&1);
		x>>=1,y>>=1,k>>=1;
	}
	reverse(X+1,X+N+1),reverse(Y+1,Y+N+1),reverse(K+1,K+N+1);
	dp1[1][1][1][1]=1;
	for(int i=1;i<N;i++)
		for(int j=0;j<2;j++)
			for(int k=0;k<2;k++)
				for(int t=0;t<2;t++)
					for(int x=0;x<2;x++)
						for(int y=0;y<2;y++){
							if(!dp1[i][j][k][t])continue;
							int z=x^y;
							if(j&&(x>X[i+1]))continue;
							if(k&&(y>Y[i+1]))continue;
							if(t&&(z>K[i+1]))continue;
							Add(dp1[i+1][j&(x==X[i+1])][k&(y==Y[i+1])][t&(z==K[i+1])],dp1[i][j][k][t]);
							Add(dp2[i+1][j&(x==X[i+1])][k&(y==Y[i+1])][t&(z==K[i+1])],dp2[i][j][k][t]);
							Add(dp2[i+1][j&(x==X[i+1])][k&(y==Y[i+1])][t&(z==K[i+1])],dp1[i][j][k][t]*((ll)z<<(N-1-i))%P);
						}
	int res=0;
	for(int i=0;i<2;i++)
		for(int j=0;j<2;j++)
			for(int t=0;t<2;t++)
				Add(res,(dp1[N][i][j][t]+dp2[N][i][j][t])%P);
	return res;
}
int main(){
	Rd(q);
	while(q--){
		int x1,y1,x2,y2,k,ans=0;
		Rd(x1),Rd(y1),Rd(x2),Rd(y2),Rd(k);
		x1--,y1--,x2--,y2--,k--;
		ans=(ans+solve(x2,y2,k))%P;
		ans=(ans-solve(x1-1,y2,k)+P)%P;
		ans=(ans-solve(x2,y1-1,k)+P)%P;
		ans=(ans+solve(x1-1,y1-1,k))%P;
		printf("%d\n",ans);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

template <typename T1, typename T2>
string print_iterable(T1 begin_iter, T2 end_iter, int counter) {
    bool done_something = false;
    stringstream res;
    res << "[";
    for (; begin_iter != end_iter and counter; ++begin_iter) {
        done_something = true;
        counter--;
        res << *begin_iter << ", ";
    }
    string str = res.str();
    if (done_something) {
        str.pop_back();
        str.pop_back();
    }
    str += "]";
    return str;
}

vector<int> SortIndex(int size, std::function<bool(int, int)> compare) {
    vector<int> ord(size);
    for (int i = 0; i < size; i++) ord[i] = i;
    sort(ord.begin(), ord.end(), compare);
    return ord;
}

template <typename T>
bool MinPlace(T& a, const T& b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T>
bool MaxPlace(T& a, const T& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename S, typename T>
ostream& operator <<(ostream& out, const pair<S, T>& p) {
    out << "{" << p.first << ", " << p.second << "}";
    return out;
}

template <typename T>
ostream& operator <<(ostream& out, const vector<T>& v) {
    out << "[";
    for (int i = 0; i < (int)v.size(); i++) {
        out << v[i];
        if (i != (int)v.size()-1) out << ", ";
    }
    out << "]";
    return out;
}

#define INF 1<<30
#define eps 1e-9

#if DEBUG && !ONLINE_JUDGE
    ifstream input_from_file("input.txt");
    #define cin input_from_file

    #define dbg_var(x) clog << #x  << ": " << x << endl;
    #define dbg_array(x, len) clog << #x << ": " << print_iterable(x, x+len, -1) << endl;
#else
    #define dbg_var(x)
    #define dbg_vec(x)
    #define dbg_array(x, len)
#endif

///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////

const LL mod = 1e9 + 7;
LL p[100];

LL TrivialComputeNum(LL k, int i, int j) {
    LL res = 0;
    for (int a = 0; a < (1<<i); a++) {
        for (int b = 0; b < (1<<j); b++) {
            if ((a^b) <= k) res++;
        }
    }
    return res;
}

LL TrivialComputeSum(LL k, int i, int j) {
    LL res = 0;
    for (int a = 0; a < (1<<i); a++) {
        for (int b = 0; b < (1<<j); b++) {
            if ((a^b) <= k) res += a^b;
        }
    }
    return res;
}

LL TrivialSimpleSum(LL k, int x, int y) {
    LL res = 0;
    for (int a = 0; a < x; a++) {
        for (int b = 0; b < y; b++) {
            if ((a^b) <= k) res += 1+(a^b);
        }
    }
    return res;
}

// Number of pairs 0 <= x < 2**i, 0 <= y < 2**j such that x^y <= k.
LL ComputeNum(LL k, int i, int j) {
    if (i < j) swap(i, j);
    if (1<<i <= k) return p[i+j]; // all entries

    LL res = p[j] % mod;
    for (int s = 0; 1<<s <= k; s++) {
        if (k & 1<<s) {
            res += p[s+j];
            res %= mod;
        }
    }
    return res;
}
// Sum of x^y over all pairs 0 <= x < 2**i, 0 <= y < 2**j such that x^y <= k.
LL ComputeSum(LL k, int i, int j) {
    if (i < j) swap(i, j);
    if (1<<i <= k) { // all entries
        LL res1 = p[j];
        LL res2 = (p[i] * (p[i] - 1ll)) / 2ll;
        res1 %= mod;
        res2 %= mod;
        return (res1 * res2) % mod;
    }

    LL res = (p[j] * k) % mod;
    for (int s = 0; (1<<s) <= k; s++) {
        if (k & (1<<s)) {
            LL ris1 = k - (k % p[s+1]);
            LL ris2 = (p[s] * (p[s] - 1ll)) / 2ll;
            ris1 %= mod;
            ris2 %= mod;
            ris1 *= p[j+s];
            ris2 *= p[j];
            res += (ris1 + ris2) % mod;
            res %= mod;
        }
    }
    return res;
}

LL SimpleSum(LL k, int x, int y) {
    LL ans = 0;
    if (x <= 0 or y <= 0) return 0;
    for (int i = 0; 1<<i <= x; i++) {
        for (int j = 0; 1<<j <= y; j++) {
            if (x&(1<<i) && y&(1<<j)) {
                int x1 = x - x % (1<<(i+1));
                int y1 = y - y % (1<<(j+1));
                x1 = x1 - x1 % (1<<max(i, j));
                y1 = y1 - y1 % (1<<max(i, j));
                int v = x1 ^ y1;
                if (k < v) continue;
                ans += (LL)(1 + v) * ComputeNum(k-v, i, j) + ComputeSum(k-v, i, j);
                ans %= mod;
            }
        }
    }
    return ans;
}

LL GetSum(int x1, int y1, int x2, int y2, LL k) {
    LL res = SimpleSum(k-1, x2, y2) - SimpleSum(k-1, x2, y1) - SimpleSum(k-1, x1, y2) + SimpleSum(k-1, x1, y1);
    res %= mod;
    res += mod;
    return res % mod;
}

int main() {
    p[0] = 1;
    for (int i = 1; i < 100; i++) p[i] = (p[i-1] * 2ll) %mod;
    // cout << SimpleSum(1000, 3, 2) << endl;
    // cout << TrivialSimpleSum(1000, 3, 2) << endl;
    // return 0;
    ios::sync_with_stdio(false);
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int x1, y1, x2, y2;
        LL k;
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        cout << GetSum(x1-1, y1-1, x2, y2, k) << "\n";
    }
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long

#define maxN 33
#define mod 1000000007

int q, i;
ll px1, px2, py1, py2, k;

ll cnt[maxN][2][2][2];
ll dp[maxN][2][2][2];

void add(ll& a, ll b) {
    a += b;
    if (a >= mod) a %= mod;
}

ll solve(ll x, ll y) {
    ll i, bit_x, bit_y, bit, px, py, pk, bit_k, gx, gy, gk;
    int fx, fy, fk;

    if (x < 0 || y < 0) return 0;

    memset(cnt, 0, sizeof(cnt));
    memset(dp, 0, sizeof(dp));

    dp[31][1][1][1] = 0;
    cnt[31][1][1][1] = 1;

    for (bit = 31; bit > 0; bit--) {
        bit_x = (x >> (bit - 1)) & 1;
        bit_y = (y >> (bit - 1)) & 1;
        bit_k = (k >> (bit - 1)) & 1;

        for (fx = 0; fx < 2; fx++) {
            for (fy = 0; fy < 2; fy++) {
                for (fk = 0; fk < 2; fk++) {

                    if (dp[bit][fx][fy][fk] == 0 && cnt[bit][fx][fy][fk] == 0) continue;

                    for (px = 0; px < 2; px++) {
                        for (py = 0; py < 2; py++) {
                            pk = px ^ py;

                            if (fx == 1 && bit_x == 0 && px == 1) continue;
                            if (fy == 1 && bit_y == 0 && py == 1) continue;
                            if (fk == 1 && bit_k == 0 && pk == 1) continue;

                            gx = fx && (bit_x == px);
                            gy = fy && (bit_y == py);
                            gk = fk && (bit_k == pk);

                            add(cnt[bit - 1][gx][gy][gk], cnt[bit][fx][fy][fk]);
                            add(dp[bit - 1][gx][gy][gk], dp[bit][fx][fy][fk] +
                                                         cnt[bit][fx][fy][fk] * pk * (1LL << (bit - 1)));
                        }
                    }

                }
            }
        }
    }

    ll ans = 0;

    for (fx = 0; fx < 2; fx++)
        for (fy = 0; fy < 2; fy++)
            for (fk = 0; fk < 2; fk++)
                add(ans, dp[0][fx][fy][fk] + cnt[0][fx][fy][fk]);

    return ans;
}

int main()
{
    //freopen("test.in","r",stdin);

    cin >> q;
    for (i = 1; i <= q; i++) {
        cin >> px1 >> py1 >> px2 >> py2 >> k;
        px1--; px2--;
        py1--; py2--;
        k--;

        ll ans = solve(px2, py2);
        ans += mod - solve(px1 - 1, py2);
        ans += mod - solve(px2, py1 - 1);
        ans += solve(px1 - 1, py1 - 1);
        ans %= mod;

        cout << ans << '\n';
    }

    return 0;
}

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<set>
#include<cstring>
#include<string>
#include<assert.h>
#include<bitset>
#include<cmath>
using namespace std;
#define FOR(i,a,b) for(int i=(a),i_##END_=(b);i<=i_##END_;++i)
#define REP(i,a,b) for(int i=(a),i_##END_=(b);i>=i_##END_;--i)
#define Edge(i,x) for(int i=head[x];~i;i=G[i].nxt)
#define INF 1e9+7
#define debug() cerr<<"Why So Serious?"<<endl
#define M 1005
#define N 100005
#define eps (1e-7)
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef long double db;
typedef unsigned long long ull;
typedef pair<ll,int> Pa;
const int md=1e9+7;
void Rd(int &res){
	res=0;static char c;
	while(c=getchar(),c<48);
	do res=(res<<1)+(res<<3)+(c^48);
	while(c=getchar(),c>47);
}
void Add(int &x,int y){
	x+=y;
	if(x>=md)x-=md;
}
int dp[2][2][2][2],Sum[2][2][2][2];
int Calc(int x,int y,int K){
	if(!x||!y)return 0;
	FOR(i,0,1)FOR(j,0,1)FOR(k,0,1)FOR(h,0,1)Sum[i][j][k][h]=dp[i][j][k][h]=0;
	int cur=0;
	dp[cur][0][0][0]=1;
	x--;y--;
	REP(i,30,0){
		cur^=1;
		int a=(x>>i)&1,b=(y>>i)&1,c=(K>>i)&1;
		int res=(1<<i)%md;
		FOR(j,0,1)FOR(k,0,1)FOR(h,0,1)Sum[cur][j][k][h]=dp[cur][j][k][h]=0;
		FOR(j,0,1)FOR(k,0,1)FOR(h,0,1){
			Add(dp[cur][j|a][k|b][h|c],dp[cur^1][j][k][h]);
			Add(Sum[cur][j|a][k|b][h|c],Sum[cur^1][j][k][h]);
			if((j||a)&&(k||b)){
				Add(dp[cur][j][k][h|c],dp[cur^1][j][k][h]);
				Add(Sum[cur][j][k][h|c],Sum[cur^1][j][k][h]);
			}
			if(h||c){
				if(j||a){
					Add(dp[cur][j][k|b][h],dp[cur^1][j][k][h]);
					int &t=Sum[cur][j][k|b][h];
					Add(t,Sum[cur^1][j][k][h]);
					Add(t,1ll*res*dp[cur^1][j][k][h]%md);
				}
				if(k||b){
					Add(dp[cur][j|a][k][h],dp[cur^1][j][k][h]);
					int &t=Sum[cur][j|a][k][h];
					Add(t,Sum[cur^1][j][k][h]);
					Add(t,1ll*res*dp[cur^1][j][k][h]%md);
				}
			}
		}
	}
	int ans=0;
	FOR(i,0,1)FOR(j,0,1)Add(ans,(dp[cur][i][j][1]+Sum[cur][i][j][1])%md);
	return ans;
}
int main(){
	int _;
	int x1,y1,x2,y2,K;
	for(Rd(_);_;_--){
		Rd(x1),Rd(y1),Rd(x2),Rd(y2),Rd(K);
		int ans=(Calc(x2,y2,K)+Calc(x1-1,y1-1,K)-Calc(x1-1,y2,K)-Calc(x2,y1-1,K))%md;
		if(ans<0)ans+=md;
		printf("%d\n",ans);
	}
	return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
const int mo=1000000007;
int lx[55],ly[55],rx[55],ry[55];
int v[55],vis[55][2][2][2][2][2];
pii f[55][2][2][2][2][2];
pii F(int d,int v1,int v2,int v3,int v4,int v5){
	if (d==-1) return pii(0,1);
	if (vis[d][v1][v2][v3][v4][v5])
		return f[d][v1][v2][v3][v4][v5];
	pii ans(0,0);
	For(vx,v1?lx[d]:0,v2?rx[d]:1)
		For(vy,v3?ly[d]:0,v4?ry[d]:1)
			if (!v5||((vx^vy)<=v[d])){
				pii tmp=F(d-1,v1&(vx==lx[d]),v2&(vx==rx[d]),
							  v3&(vy==ly[d]),v4&(vy==ry[d]),
							  v5&((vx^vy)==v[d]));
				if (vx^vy) tmp.fi=(tmp.fi+(1ll<<d)*tmp.se)%mo;
				ans.fi=(ans.fi+tmp.fi)%mo;
				ans.se=(ans.se+tmp.se)%mo;
			}
	vis[d][v1][v2][v3][v4][v5]=1;
	return f[d][v1][v2][v3][v4][v5]=ans;
}
void decode(int *a,int d){
	For(i,0,29)
		a[i]=(d>>i)&1;
}
void solve(){
	int a,b,c,d,e;
	scanf("%d%d%d%d%d",&a,&c,&b,&d,&e);
	decode(lx,a-1); decode(rx,b-1);
	decode(ly,c-1); decode(ry,d-1);
	decode(v,min(e,1<<30)-1);
	memset(f,0,sizeof(f));
	memset(vis,0,sizeof(vis));
	pii ans=F(29,1,1,1,1,1);
	printf("%d\n",(ans.fi+ans.se)%mo);
}
int main(){
	int T;
	scanf("%d",&T);
	while (T--) solve();
}

#include<bits/stdc++.h>
#define LL long long
#define db double
#define il inline

using namespace std;
const int N=3e5+10,mod=1e9+7;
il LL rd()
{
    LL x=0,w=1;char ch=0;
    while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*w;
}
int f[2][2][2][2],g[2][2][2][2];
void ad(int &x,int y){x+=y,x-=(x>=mod)?mod:0;}
LL sov(int mx,int my,int mk)
{
	if(mx<0||my<0) return 0;
	memset(f,0,sizeof(f)),memset(g,0,sizeof(g));
	int nw=1,la=0;
	g[0][1][1][1]=1;
	for(int i=30;~i;--i)
	{
		int x=mx>>i&1,y=my>>i&1,kk=mk>>i&1;
		for(int j=0;j<=1;++j)
			for(int k=0;k<=1;++k)
				for(int l=0;l<=1;++l)
				{
					if(!g[la][j][k][l]) continue;
					for(int xx=0;xx<=(x|(!j));++xx)
						for(int yy=0;yy<=(y|(!k));++yy)
							if((xx^yy)<=kk||!l)
							{
								ad(f[nw][j&(xx==x)][k&(yy==y)][l&((xx^yy)==kk)],(f[la][j][k][l]+1ll*g[la][j][k][l]*((xx^yy)<<i)%mod)%mod);
								ad(g[nw][j&(xx==x)][k&(yy==y)][l&((xx^yy)==kk)],g[la][j][k][l]);
							}
					f[la][j][k][l]=g[la][j][k][l]=0;
				}
		nw=!nw,la=!la;
	}
	int ans=0;
	for(int j=0;j<=1;++j)
		for(int k=0;k<=1;++k)
			for(int l=0;l<=1;++l)
				ad(ans,(f[la][j][k][l]+g[la][j][k][l])%mod);
	return ans;
}

int main()
{
    //aha
	int T=rd();
	while(T--)
	{
		int ax=rd()-1,ay=rd()-1,bx=rd()-1,by=rd()-1,k=rd()-1;
		printf("%lld\n",(sov(bx,by,k)-sov(ax-1,by,k)-sov(bx,ay-1,k)+sov(ax-1,ay-1,k)+mod+mod)%mod);
	}
    return 0;
}

#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef double ld;
typedef pair<ll, ll> pll;

const ll mod = 1e9 + 7;
const ll r2 = (mod + 1) / 2;

void add(ll& a, ll b)
{
    a += b;
    if (a >= mod) {
        a -= mod;
    }
}

void sub(ll& a, ll b)
{
    a -= b;
    if (a < 0) {
        a += mod;
    }
}

void mul(ll& a, ll b)
{
    a *= b;
    if (a >= mod) {
        a %= mod;
    }
}

const int rmax = 33;

ll sum[2][2][2][rmax];
ll cnt[2][2][2][rmax];

bool bit(ll mask, int pos)
{
    return (mask >> pos) & 1;
}

vector<ll> degs(50);

ll solve(ll xmax, ll ymax, ll k)
{
    memset(sum, 0, sizeof(sum));
    memset(cnt, 0, sizeof(cnt));
    sum[1][1][1][0] = 0;
    cnt[1][1][1][0] = 1;

    for (int r = 1; r < rmax; ++r) {
        int pos = rmax - r - 1;
        for (int a = 0; a < 2; ++a) {
            int dx = 1;
            if (a == 0 || bit(xmax, pos)) {
                dx = 2;
            }
            for (int b = 0; b < 2; ++b) {
                int dy = 1;
                if (b == 0 || bit(ymax, pos)) {
                    dy = 2;
                }
                for (int c = 0; c < 2; ++c) {
                    for (int xx = 0; xx < dx; ++xx) {
                        int newX = 0;
                        if (a == 1 && xx == bit(xmax, pos)) {
                            newX = 1;
                        }
                        for (int yy = 0; yy < dy; ++yy) {
                            int newY = 0;
                            if (b == 1 && yy == bit(ymax, pos)) {
                                newY = 1;
                            }
                            int kk = (xx ^ yy);
                            if (c == 1 && kk > bit(k, pos)) {
                                continue;
                            }
                            int newK = 0;
                            if (c == 1 && kk == bit(k, pos)) {
                                newK = 1;
                            }

                            ll dcnt = cnt[a][b][c][r - 1];
                            add(cnt[newX][newY][newK][r], dcnt);
                            ll val = dcnt;
                            mul(val, degs[pos]);
                            if (kk) {
                                add(sum[newX][newY][newK][r], val);
                            }
                            add(sum[newX][newY][newK][r], sum[a][b][c][r - 1]);
                        }
                    }
                }
            }
        }
    }

    ll ans = 0;
    for (int a = 0; a < 2; ++a) {
        for (int b = 0; b < 2; ++b) {
            for (int c = 0; c < 2; ++c) {
                //cout << a << " " << b << " " << c << " " << sum[a][b][c][rmax - 1] << "\n";
                add(ans, sum[a][b][c][rmax - 1]);
                add(ans, cnt[a][b][c][rmax - 1]);
            }
        }
    }
    return ans;
}

int main()
{

    //ifstream cin("input.txt");
	//ofstream cout("output.txt");
    //freopen("input.txt", "r", stdin);

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    degs[0] = 1;
    for (int i = 1; i < 50; ++i) {
        degs[i] = degs[i - 1];
        mul(degs[i], 2);
    }

    int tt;
    cin >> tt;

    while (tt--) {
        ll x1, y1, x2, y2, k;
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        --x1, --y1, --x2, --y2;
        //cout << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";
        --k;
        ll ans = solve(x2, y2, k);
        if (x1) {
            sub(ans, solve(x1 - 1, y2, k));
        }
        if (y1) {
            sub(ans, solve(x2, y1 - 1, k));
        }
        if (x1 && y1) {
            add(ans, solve(x1 - 1, y1 - 1, k));
        }
        cout << ans << "\n";
    }

}

#include <bits/stdc++.h>

#define B 30
#define MOD 1000000007

using namespace std;

int mark[B + 1][2][2][2];
pair<int, int> dp[B + 1][2][2][2];

int apply(int &x, int y) {
	x += y;
	if (x >= MOD) {
		x -= MOD;
	}
	if (x < 0) {
		x += MOD;
	}
}

pair<int, int> calc(int b, int x, int less_x, int y, int less_y, int k, int less_k) {
	if (b < 0) {
		return make_pair(0, less_x & less_y & less_k);
	}

	if (!mark[b][less_x][less_y][less_k]) {
		int bx = (x >> b) & 1;
		int by = (y >> b) & 1;
		int bk = (k >> b) & 1;
		for (int vx = 0; vx < 2; vx++) {
			if (less_x || vx <= bx) {
				int nless_x = less_x | (vx < bx);
				for (int vy = 0; vy < 2; vy++) {
					if (less_y || vy <= by) {
						int nless_y = less_y | (vy < by);
						if (less_k || ((vx ^ vy) <= bk)) {
							pair<int, int> ret = calc(b - 1, x, nless_x, y, nless_y, k, less_k | ((vx ^ vy) < bk));
							apply(dp[b][less_x][less_y][less_k].first, 1LL * ret.second * ((vx ^ vy) << b) % MOD);
							apply(dp[b][less_x][less_y][less_k].first, ret.first);
							apply(dp[b][less_x][less_y][less_k].second, ret.second);
						}
					}
				}
			}
		}
		mark[b][less_x][less_y][less_k] = 1;
	}
	return dp[b][less_x][less_y][less_k];
}

int calc(int x, int y, int k) {
	memset(dp, 0, sizeof dp);
	memset(mark, 0, sizeof mark);
	pair<int, int> ret = calc(B, x, 0, y, 0, k, 0);
	return (ret.first + ret.second) % MOD;
}

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		int x, xx, y, yy, k;
		scanf("%d %d %d %d %d", &x, &y, &xx, &yy, &k);
		int ans = 0;
		apply(ans, calc(xx, yy, k));
		apply(ans, -calc(xx, y - 1, k));
		apply(ans, -calc(x - 1, yy, k));
		apply(ans, calc(x - 1, y - 1, k));
		printf("%d\n", ans);
	}
}

//PRZEMYSL ASSERTY

//SPRAWDZ CORNER CASE'Y, MINIMALNE I MAKSYMALNE WEJŚCIE I WYJŚCIE

//MODULO = 1

//while (clock()<=69*CLOCKS_PER_SEC)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

const long long mod=1000*1000*1000+7;

map < pair < pair <int,int> , int> , pair <long long, long long> > mapa;

inline void dziel(long long &v)
{
	if(v&1)
		v+=mod;
	v/=2;
}

inline int naj(int v)
{
	int r=1;
	while(r*2<v)
		r*=2;
	return r;
}

inline pair <long long,long long> dod(pair <long long,long long> a, pair <long long,long long> b)
{
	a.first+=b.first;
	a.second+=b.second;
	return a;
}

pair <long long,long long> licz(int a, int b, int k)
{
	if (a>b)
		swap(a, b);
	int r=naj(b);
	k=min(k, 2*r);
	if (a<=0 || b<=0 || k<=0)
		return {0, 0};
	pair <long long,long long> ret={0, 0};
	if (a==b && __builtin_popcount(a)==1)//kwadrat
	{
		k=min(k, a);
		ret.first=a;
		ret.first*=k;
		ret.first%=mod;
		ret.first*=(k+1);
		ret.first%=mod;
		dziel(ret.first);

		ret.second=a;
		ret.second*=k;
		ret.second%=mod;
		return ret;
	}
	if (mapa.count({{a, b}, k}))
		return mapa[{{a, b}, k}];

	ret=dod(licz(min(a, r), min(b, r), k), licz(a-r, b-r, k));

	auto ret2=dod(licz(min(a, r), b-r, k-r), licz(a-r, min(b, r), k-r));

	ret2.first+=ret2.second*r;
	ret2.first%=mod;

	ret=dod(ret, ret2);

	ret.first%=mod;
	ret.second%=mod;

	mapa[{{a, b}, k}]=ret;
	return ret;
}

int main()
{
	int q;
	scanf("%d", &q);
	while(q--)
	{
		mapa.clear();
		int a, b, x, y, k;
		scanf("%d%d%d%d%d", &a, &b, &x, &y, &k);
		long long wyn=licz(x, y, k).first-licz(x, b-1, k).first-licz(a-1, y, k).first+licz(a-1, b-1, k).first;
		wyn%=mod;
		wyn+=mod;
		wyn%=mod;
		printf("%lld\n", wyn);
	}
	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef  long long ll;
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef pair<int, int> pii;
typedef long double ld;
typedef unsigned int uint;
#define MAX 105
bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"YES" : "NO");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD) {
    x+=y; if (x>=mod) x-=mod;
    assert(x>=0 && x<mod);
}
void et() {
    puts("-1"); exit(0);
}

int cnt[33][2][2][2], ss[33][2][2][2];
int cal(int X, int Y, int K) {
    if (X<0||Y<0) return 0;
    memset(cnt, 0, sizeof cnt);
    memset(ss, 0, sizeof ss);
    cnt[31][1][1][1] = 1;
    for(int i=30;i>=0;i--) {
        REP(a,2) REP(b,2) REP(c,2) {
            REP(x,2) REP(y,2) {
                int z = x^y;
                if (a==1 && x==1 && ((X>>i)&1) ==0) continue;
                if (b==1 && y==1 && ((Y>>i)&1) ==0) continue;
                if (c==1 && z==1 && ((K>>i)&1) ==0) continue;
                addmod(cnt[i][a&(((X>>i)&1)==x)][b&(((Y>>i)&1)==y)][c&(((K>>i)&1)==z)], cnt[i+1][a][b][c]);
                addmod(ss[i][a&(((X>>i)&1)==x)][b&(((Y>>i)&1)==y)][c&(((K>>i)&1)==z)], ss[i+1][a][b][c]);
                addmod(ss[i][a&(((X>>i)&1)==x)][b&(((Y>>i)&1)==y)][c&(((K>>i)&1)==z)], (ll)cnt[i+1][a][b][c] * (z<<i) % MOD);
            }
        }
    }
    int ans = 0;
    REP(a,2) REP(b,2) REP(c,2) {
        addmod(ans, ss[0][a][b][c]);
        addmod(ans, cnt[0][a][b][c]);
    }
    return ans;
}
void fmain(int tid) {
    scanf("%d",&n);
    REQ(i,n) {
        int x1,x2,y1,y2;
        scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
        x1--;y1--;x2--;y2--;k--;
        int ans=0;
        addmod(ans, cal(x2, y2, k));
        addmod(ans, cal(x1-1, y1-1, k));
        addmod(ans, MOD-cal(x1-1, y2, k));
        addmod(ans, MOD-cal(x2, y1-1, k));
        printf("%d\n", ans);
    }
}

int main() {
    int t=1;
//    scanf("%d", &t);
    REQ(i,t) {
        fmain(i);
    }
    return 0;
}

#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <set>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << endl
#define _ << " _ " <<

typedef long long llint;

const int B = 32;

const int mod = 1e9 + 7;

inline int add(int a, int b) {
  return a+b >= mod ? a+b-mod : a+b;
}

inline int sub(int a, int b) {
  return a >= b ? a-b : a-b+mod;
}

inline int mul(int a, int b) {
  return llint(a)*b % mod;
}

int fc[B+1][2][2][2];
int fs[B+1][2][2][2];

int f(llint x, llint y, llint k) {
  if (x < 0 || y < 0) return 0;

  // i <= x, j <= y, i^j <= k

  vector<int> X(B), Y(B), K(B);
  REP(i, B) {
    X[i] = (x >> (B-i-1)) & 1;
    Y[i] = (y >> (B-i-1)) & 1;
    K[i] = (k >> (B-i-1)) & 1;
  }

  REP(i, B+1) REP(gx, 2) REP(gy, 2) REP(gk, 2) {
    fc[i][gx][gy][gk] = 0;
    fs[i][gx][gy][gk] = 0;
  }

  fc[0][0][0][0] = 1;
  fs[0][0][0][0] = 0;
  REP(i, B) REP(gx, 2) REP(gy, 2) REP(gk, 2) {
    int c = fc[i][gx][gy][gk];
    int s = fs[i][gx][gy][gk];

    REP(bx, 2) REP(by, 2) {
      if (gx || (bx <= X[i])) {
        if (gy || (by <= Y[i])) {
          int bk = bx ^ by;
          if (gk || bk <= K[i]) {
            int ngx = gx || bx < X[i];
            int ngy = gy || by < Y[i];
            int ngk = gk || bk < K[i];
            fc[i+1][ngx][ngy][ngk] = add(fc[i+1][ngx][ngy][ngk], c);
            fs[i+1][ngx][ngy][ngk] = add(fs[i+1][ngx][ngy][ngk], add(s, mul(c, ((bk * 1LL) << (B-i-1)) % mod)));
          }
        }
      }
    }
  }

  int ret = 0;
  REP(gx, 2) REP(gy, 2) REP(gk, 2) {
    ret = add(ret, fc[B][gx][gy][gk]);
    ret = add(ret, fs[B][gx][gy][gk]);
  }
  return ret;
}

int main(void) {
  int q;
  scanf("%d", &q);
  while (q--) {
    int x1, y1, x2, y2, k;
    scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
    --x1, --y1, --x2, --y2, --k;

    int ans = 0;
    ans = add(ans, f(x2, y2, k));
    ans = sub(ans, f(x1-1, y2, k));
    ans = sub(ans, f(x2, y1-1, k));
    ans = add(ans, f(x1-1, y1-1, k));
    printf("%d\n", ans);
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mod 1000000007

inline LL read() {
	LL x = 0, f = 1; char a = getchar();
	while(a < '0' || a > '9') { if(a == '-') f = -1; a = getchar(); }
	while(a >= '0' && a <= '9') x = x * 10 + a - '0', a = getchar();
	return x * f;
}

int solve(int x, int y, int k, int add) {
	if(x > y) swap(x, y);
	if(!x) return 0;
	if(k <= 0) return 0;
	int z = 1 << (31 - __builtin_clz(y)), kk = min(k, z), ret = 0;
	if(x < z) {
		ret += (((1LL * (kk + 1) * kk / 2 % mod) * x) % mod + ((1LL * kk * add ) % mod * x) % mod) % mod;
		(ret += solve(x, y - z, k - z, add + z)) %= mod;
	} else {
		ret += (((1LL * (kk + 1) * kk / 2 % mod) * z) % mod + ((1LL * kk * add ) % mod * z) % mod) % mod;
		(ret += solve(z, y - z, k - z, add + z)) %= mod;
		(ret += solve(x - z, z, k - z, add + z)) %= mod;
		(ret += solve(x - z, y - z, k, add)) %= mod;
	}
	return ret;
}

int main() {
	int q, x1, x2, y1, y2, k, ans;
	q = read();
	while(q--) {
		x1 = read() - 1; y1 = read() - 1;
		x2 = read(); y2 = read();
		k = read();
		ans = 0;
		(ans += (solve(x1, y1, k, 0) + solve(x2, y2, k, 0)) % mod) %= mod;
		(ans -= (solve(x1, y2, k, 0) + solve(x2, y1, k, 0)) % mod) %= mod;
		printf("%d\n", (ans + mod) % mod);
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
const int K = 40;
const long long md = 1e9 + 7;

pair<long long, long long> f[K][2][2][2];
bool us[K][2][2][2];

bool X[K], Y[K], KK[K];

pair<long long, long long> calc_prefix(long long t, bool eq_x, bool eq_y, bool eq_k) {
    if (t == 0)
        return {1, 0};
    auto& p_f = f[t][eq_x][eq_y][eq_k];
    if (us[t][eq_x][eq_y][eq_k])
        return p_f;

    p_f = {0, 0};
    us[t][eq_x][eq_y][eq_k] = true;

    long long t_sz = 1LL << (long long)t;
    long long tp_sz = 1LL << ((long long)t - 1);

    for (int i = 0; i < 2; i++) {
        if (eq_x && i > X[t - 1])
            continue;
        for (int j = 0; j < 2; j++) {
            if (eq_y && j > Y[t - 1])
                continue;

            bool bit = i ^ j;
            if (eq_k && bit > KK[t - 1])
                continue;

            pair<long long, long long> pz{0, 0};
            pz = calc_prefix(t - 1, eq_x && (i == X[t - 1]), eq_y && (j == Y[t - 1]), eq_k && (bit == KK[t - 1]));
            if (bit) {
                pz.second += pz.first * tp_sz;
                pz.second %= md;
            }
            p_f.first += pz.first;
            p_f.second += pz.second;
        }
    }

    p_f.first %= md;
    p_f.second %= md;

//    cerr << "calc_prefix " << t << " " << eq_x << " " << eq_y << " " << eq_k << " => " << p_f.first << " " << p_f.second << "\n";

    return p_f;
}

long long calc(long long x, long long y, long long k) {
    if (x < 0 || y < 0 || k < 0)
        return 0;

    fill_n((bool*)us, sizeof(us) / sizeof(bool), false);
    for (long long i = 0; i < K; i++) {
        X[i] = (x >> i) & 1LL;
        Y[i] = (y >> i) & 1LL;
        KK[i] = (k >> i) & 1LL;
    }

    auto answer = calc_prefix(K - 1, 1, 1, 1);

    return (answer.second + answer.first) % md;

}

long long calc(long long lx, long long rx, long long ly, long long ry, long long k) {
    --lx;
    --rx;
    --ly;
    --ry;
    --k;

    long long answer =
        calc(rx, ry, k) +
        (-1) * calc(lx - 1, ry, k) +
        (-1) * calc(rx, ly - 1, k) +
        calc(lx - 1, ly - 1, k);
    answer += 10 * md;
    return answer % md;

}

int main() {
    ios::sync_with_stdio(false);

#ifdef VK_LOCAL
//    freopen("output.txt", "w", stderr);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // VK_LOCAL

    int q;
    cin >> q;

    while (q--) {
        int lx, rx, ly, ry, k;
        cin >> lx >> ly >> rx >> ry >> k;

        long long answer = calc(lx, rx, ly, ry, k);
        cout << answer << "\n";
//        cerr << "answer = " << answer << "\n";
    }

    return 0;
}

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<bitset>
#include<string>
#include<cstdio>
#include<cctype>
#include<cassert>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>

#define For(i,x,y) for (int i=x;i<y;i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

#define dprintf(...) fprintf(stderr,__VA_ARGS__)
using namespace std;

typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> Vi;

int IN(){
	int c,f,x;
	while (!isdigit(c=getchar())&&c!='-');c=='-'?(f=1,x=0):(f=0,x=c-'0');
	while (isdigit(c=getchar())) x=(x<<1)+(x<<3)+c-'0';return !f?x:-x;
}

const int p=1e9+7;

int cal(int x,int a,int k){
	if (a/x>k/x) return 0;
	if (a/x<k/x){
		return (1ll*(a/x)*x*x+1ll*x*(x+1)/2)%p;
	}
	k%=x;
	return (1ll*(a/x)*x*(k+1)+1ll*(k+1)*(k+2)/2)%p;
}
int calc(int lx,int rx,int ly,int ry,int k){
	if ((rx^lx)>(ry^ly)) swap(lx,ly),swap(rx,ry);
	return 1ll*(rx^lx)*cal(ry^ly,lx^ly,k)%p;
}
int calc(int x,int y,int k){
	int lx=0,res=0;
	for (int i=30;~i;i--)
		if (x>>i&1){
			int ly=0;
			for (int j=30;~j;j--)
				if (y>>j&1){
					res=(res+calc(lx,lx|1<<i,ly,ly|1<<j,k))%p;
					ly^=1<<j;
				}
			lx^=1<<i;
		}
	return res;
}

int X1,Y1,X2,Y2,k,res;

int main(){
	for (int q=IN();q--;){
		X1=IN(),Y1=IN(),X2=IN(),Y2=IN(),k=IN()-1;
		res=0;
		res=(res+calc(X2,Y2,k))%p;
		res=(res-calc(X2,Y1-1,k)+p)%p;
		res=(res-calc(X1-1,Y2,k)+p)%p;
		res=(res+calc(X1-1,Y1-1,k))%p;
		printf("%d\n",res);
	}
}

#define DBG 1
// No progress
#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>

#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define dbg(x) if (DBG) cerr << #x << " = " << x << '\n';

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef pair<int, int> pii;

//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int MOD = (int)1e9 + 7;

int addMod(int a, int b, int m = MOD) {
    a += b;

    if (m <= a) {
        a -= m;
    }

    return a;
}

int mulMod(int a, int b, int m = MOD) {
    return a * 1ll * b % m;
}

int dp[32][2][2][2], dp2[32][2][2][2];

bool bit(int x, int p) {
    return x & (1 << p);
}

int calc(int n, int m, int k) {
    memset(dp, 0, sizeof dp);
    memset(dp2, 0, sizeof dp2);
    dp[31][0][0][0] = 1;

    for (int i = 30; i >= 0; --i) {
        for (int kl = 0; kl < 2; ++kl) {
            for (int nl = 0; nl < 2; ++nl) {
                for (int ml = 0; ml < 2; ++ml) {
                    for (int x = 0; x < 2; ++x) {
                        for (int y = 0; y < 2; ++y) {
                            if (nl == 0 && x > bit(n, i)) {
                                continue;
                            }

                            if (ml == 0 && y > bit(m, i)) {
                                continue;
                            }

                            int z = (x ^ y);

                            if (kl == 0 && z > bit(k, i)) {
                                continue;
                            }

                            int nnl = (nl | (x < bit(n, i)));
                            int nml = (ml | (y < bit(m, i)));
                            int nkl = (kl | (z < bit(k, i)));
                            dp[i][nkl][nnl][nml] = addMod(dp[i][nkl][nnl][nml], dp[i + 1][kl][nl][ml]);
                            dp2[i][nkl][nnl][nml] = addMod(dp2[i][nkl][nnl][nml], dp2[i + 1][kl][nl][ml]);
                            dp2[i][nkl][nnl][nml] = addMod(dp2[i][nkl][nnl][nml], mulMod(z << i, dp[i + 1][kl][nl][ml]));
                        }
                    }
                }
            }
        }
    }

    int ret = 0;

    for (int i = 0; i < 8; ++i) {
        ret = addMod(ret, dp[0][bit(i, 0)][bit(i, 1)][bit(i, 2)]);
        ret = addMod(ret, dp2[0][bit(i, 0)][bit(i, 1)][bit(i, 2)]);
    }

    return ret;
}

void solve() {
    int x1, y1, x2, y2, k;
    scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
    --x1;
    --y1;
    --x2;
    --y2;
    --k;

    int ans = calc(x2, y2, k);

    if (x1 > 0) {
        ans = addMod(ans, MOD - calc(x1 - 1, y2, k));
    }

    if (y1 > 0) {
        ans = addMod(ans, MOD - calc(x2, y1 - 1, k));
    }

    if (x1 > 0 && y1 > 0) {
        ans = addMod(ans, calc(x1 - 1, y1 - 1, k));
    }

    printf("%d\n", ans);
}

int main() {
    int tt;
    scanf("%d", &tt);

    while (tt--) {
        solve();
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
int q,dp[31][8],sum[30][8],k;
void add(int &a,int b){
	a+=b;
	if(a >= M)a-=M;
}
void sub(int &a,int b){
	a-=b;
	if(a < 0)a+=M;
}
int find(int x,int y){
	if(x < 0 || y < 0)return 0;
	int ret = 0;
	memset(dp,0,sizeof dp);
	memset(sum,0,sizeof sum);
	dp[30][0] = 1;
	for(int i = 30;i;i--)
		for(int j = 0;j < 8;j++)
			if(dp[i][j])
			for(int at = 0;at < 4;at++){
				bool bitx = at&1,bity = at&2;
				if(bitx && !(x&(1<<i)) && !(j&4))continue;
				if(bity && !(y&(1<<i)) && !(j&2))continue;
				if((bitx^bity) && !(k&(1<<i)) && !(j&1))continue;
				int to = j;
				to|= 4*(bitx < !!(x&(1<<i)));
				to|= 2*(bity < !!(y&(1<<i)));
				to|= (bitx^bity) < !!(k&(1<<i));
				add(dp[i - 1][to],dp[i][j]);
				add(sum[i - 1][to],sum[i][j]);
				add(sum[i - 1][to],((bitx^bity)<<i)*1LL*dp[i][j]%M);
			}
	int i = 0;
	for(int j = 0;j < 8;j++)
		for(int at = 0;at < 4;at++){
			bool bitx = at&1,bity = at&2;
			if(bitx && !(x&(1<<i)) && !(j&4))continue;
			if(bity && !(y&(1<<i)) && !(j&2))continue;
			if((bitx^bity) && !(k&(1<<i)) && !(j&1))continue;
			add(ret,(bitx^bity)*dp[i][j]);
			add(ret,dp[i][j]);
			add(ret,sum[i][j]);
		}
	return ret;
}
int main(){
// 	freopen("input.txt","r",stdin);
	scanf("%d",&q);
	while(q--){
		int x,x1,y,y1;
		scanf("%d%d%d%d%d",&x,&y,&x1,&y1,&k);
		x--;y--;x1--;y1--;k--;
		int ans = 0;
		add(ans,find(x1,y1));
		sub(ans,find(x - 1,y1));
		sub(ans,find(x1,y - 1));
		add(ans,find(x - 1,y - 1));
		printf("%d\n",ans);
	}
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = (1e9)+7;

pair<ll, ll> query(ll x, ll y, ll k, ll lim) {
	if(x <= 0 || y <= 0 || k < 0 || lim <= 0) return {0, 0};
	ll pw = 1 << k;
	ll cnt = 0, sum = 0;
	if(x >= pw || y >= pw) {
		ll val_cnt = min(pw, lim), node_cnt = min(pw, min(x, y));
		cnt = val_cnt * node_cnt % MOD;
		sum = (val_cnt * (val_cnt + 1) / 2 % MOD) * node_cnt % MOD;
	} else {
		pw >>= 1;
		pair<ll, ll> res[2][2] = {
			query(x, y, k-1, lim),
			query(x - pw, y, k-1, lim - pw),
			query(x, y - pw, k-1, lim - pw),
			query(x - pw, y - pw, k-1, lim)
		};
		cnt = (res[0][0].second + res[0][1].second + res[1][0].second + res[1][1].second) % MOD;
		sum = (res[0][0].first + res[0][1].first + res[1][0].first + res[1][1].first) % MOD;
		sum = (sum + (res[0][1].second + res[1][0].second) * pw) % MOD;
	}
	assert(sum >= 0 && cnt >= 0);
	return {sum, cnt};
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while(q--) {
		ll x1, y1, x2, y2, lim;
		cin >> x1 >> y1 >> x2 >> y2 >> lim;
		ll k = 0;
		while(1ll << k <= max(x2, y2)) ++k;
		ll res = query(x2, y2, k, lim).first
					 - query(x1 - 1, y2, k, lim).first
					 - query(x2, y1 - 1, k, lim).first
					 + query(x1 - 1, y1 - 1, k, lim).first;
		res %= MOD;
		cout << (res + MOD) % MOD << '\n';
	}
}

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <queue>

using namespace std;

#define TRACE(x) cerr << #x << " " << x << endl
#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<

#define fst first
#define snd second

typedef long long llint;
typedef pair<int, int> pii;

const int B = 27397, MOD = 1e9 + 7;
const int B1 = 33941, MOD1 = 1e9 + 9;

int q, r, s, k;
int memo_cnt[32][2][2][2], memo_sum[32][2][2][2];

inline int add(int a, int b) {
  a += b;
  while (a < 0) a += MOD;
  return a % MOD;
}

inline int mul(const int &a, const int &b) {
  return (llint) a*b % MOD;
}

inline void init() {
  memset(memo_cnt, -1, sizeof memo_cnt);
  memset(memo_sum, -1, sizeof memo_sum);
}

int get_cnt(int bit, bool flag_r, bool flag_s, bool flag_k) {
  int &ref = memo_cnt[bit][flag_r][flag_s][flag_k];
  if (ref != -1) return ref;
  if (bit == 0) return ref = 1;

  bool r_bit = (r & (1 << (bit - 1))) != 0;
  bool s_bit = (s & (1 << (bit - 1))) != 0;
  bool k_bit = (k & (1 << (bit - 1))) != 0;

  ref = 0;

  for (int i = 0; i < 2; ++i) {
    if (flag_r && i > r_bit) continue;
    for (int j = 0; j < 2; ++j) {
      if (flag_s && j > s_bit) continue;
      if (flag_k && (i ^ j) > k_bit) continue;
      ref = add(ref, get_cnt(bit - 1, flag_r && (i == r_bit), flag_s && (j == s_bit), flag_k && ((i ^ j) == k_bit)));
    }
  }

  return ref;
}

int get_sum(int bit, bool flag_r, bool flag_s, bool flag_k) {
  int &ref = memo_sum[bit][flag_r][flag_s][flag_k];
  if (ref != -1) return ref;
  if (bit == 0) return ref = 1;

  bool r_bit = (r & (1 << (bit - 1))) != 0;
  bool s_bit = (s & (1 << (bit - 1))) != 0;
  bool k_bit = (k & (1 << (bit - 1))) != 0;

  ref = 0;

  for (int i = 0; i < 2; ++i) {
    if (flag_r && i > r_bit) continue;
    for (int j = 0; j < 2; ++j) {
      if (flag_s && j > s_bit) continue;
      if (flag_k && (i ^ j) > k_bit) continue;
      int cnt = get_cnt(bit - 1, flag_r && (i == r_bit), flag_s && (j == s_bit), flag_k && ((i ^ j) == k_bit));
      if (i ^ j)
        ref = add(ref, mul(cnt, 1 << (bit - 1)));
      ref = add(ref, get_sum(bit - 1, flag_r && (i == r_bit), flag_s && (j == s_bit), flag_k && ((i ^ j) == k_bit)));
    }
  }

  return ref;
}

int pref(int rr, int ss) {
  r = rr; s = ss;
  if (r < 0 || s < 0) return 0;
  init();
  return get_sum(31, true, true, true);
}

int solve(int r1, int s1, int r2, int s2) {
  return add(pref(r2, s2), add(pref(r1 - 1, s1 - 1), add(-pref(r2, s1 - 1), -pref(r1 - 1, s2))));
}

int main(void) {
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    int r1, s1, r2, s2;
    scanf("%d%d%d%d%d", &r1, &s1, &r2, &s2, &k);
    --r1; --r2; --s1; --s2; --k;
    printf("%d\n", solve(r1, s1, r2, s2));
  }
  return 0;
}

#pragma comment(linker, "/STACK:32000000")
#include <bits/stdc++.h>

const int nil = -1;
#define REP(i, n) for (int i = 0, _= (n); i < _; ++i)
#define DWN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define RE1(i, n) for (int i = 1, _= (n); i <= _; ++i)
#define DW1(i, n) for (int i = (n); i > 0; --i)
#define FOR(i, l, r) for (int i = (l), _ = (r); i < _; ++i)
#define EDGE(u, v, e) for (int e = head[u], v; e != nil && (v = to[e], true); e = next[e])

using namespace std;
typedef long long int64;
inline bool read(int& val)    { return scanf("%d",    &val) != -1;}
inline bool read(int64& val)  { return scanf("%I64d", &val) != -1;}
inline bool read(double& val) { return scanf("%lf",   &val) != -1;}
inline bool read(char* val)   { return scanf("%s",     val) != -1;}
template<class T1, class T2>
inline bool read(T1& a, T2& b)
{ return read(a) && read(b); }
template<class T1, class T2, class T3>
inline bool read(T1& a, T2& b, T3& c)
{ return read(a) && read(b) && read(c); }
template<class T1, class T2, class T3, class T4>
inline bool read(T1& a, T2& b, T3& c, T4& d)
{ return read(a) && read(b) && read(c) && read(d); }
template<class T1, class T2, class T3, class T4, class T5>
inline bool read(T1& a, T2& b, T3& c, T4& d, T5& e)
{ return read(a) && read(b) && read(c) && read(d) && read(e); }

const int N = 1111111;
const int64 mod = 1000000007;

void add(int64& a, int64 b, int64 mod) {
	a += b;
	if (a >= mod) a -= mod;
	if (a < 0)	a += mod;
}
template<class T> T gcd(T a, T b, T& x, T& y) {
	T xa = 1, xb = 0;
	T ya = 0, yb = 1;
	while (b != 0) {
		x = xa - a / b * xb; xa = xb; xb = x;
		y = ya - a / b * yb; ya = yb; yb = y;
		T r = a % b; a = b; b = r;
	}
	x = xa; y = ya; return a;
}
template<class T> T rev(T a, T mod) {
	T x, y;
	gcd(a, mod, x, y);
	return (x + mod) % mod;
}
int64 n2(int64 n) {
	return (1 + n) * n % mod * rev(2ll, mod) % mod;
}
pair<int64, int64> get(int64 a, int64 b, int64 k) {
	if (a == 0 || b == 0 || k == 0) return make_pair(0, 0);
	if (a < b) swap(a, b);
	int64 n = 1; while ((n << 1) <= a) n <<= 1;
	pair<int64, int64> ret;
	int k0 = min(k, n);
	int k1 = min(k - k0, n);
	if (b <= n) {
		ret = make_pair(b * k0 % mod, b * n2(k0) % mod);
		pair<int64, int64> tmp = get(a - n, b, k - k0);
		add(ret.first, tmp.first, mod);
		add(ret.second, tmp.second, mod);
		add(ret.second, n * tmp.first % mod, mod);
	} else {
		ret = make_pair(n * k0 % mod, n * n2(k0) % mod);
		add(ret.first,  (a - n + b - n) * k1 % mod, mod);
		add(ret.second, (a - n + b - n) * k1 % mod * n % mod, mod);
		add(ret.second, (a - n + b - n) * n2(k1) % mod, mod);
		pair<int64, int64> tmp = get(a - n, b - n, k);
		add(ret.first, tmp.first, mod);
		add(ret.second, tmp.second, mod);
	}
	return ret;
}
int main() {
	//freopen("test.txt", "r", stdin);
	int q; read(q);
	REP (i, q) {
		int x1, y1, x2, y2, k; read(x1, y1, x2, y2, k);
		x1--; y1--;
		int64 ans = 0;
		ans += get(x2, y2, k).second;
		ans += get(x1, y1, k).second;
		ans += mod - get(x1, y2, k).second;
		ans += mod - get(x2, y1, k).second;
		cout << ans % mod << endl;
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MOD = 1'000'000'007;

ll sm(ll x) {
	return x*(x-1) / 2 % MOD;
}

ll sumar(ll lo, ll hi) {
	return (sm(hi) - sm(lo) + MOD) % MOD;
}

ll solve(ll x0, ll y0, ll k) {
	/*
	ll sum = 0;
	for (ll bit=33; bit>=0; bit--) {
		ll e = 1ll << bit;
		if (e & k) {
			ll em = e % MOD;
			sum += em*em%MOD*(em+MOD-1)%MOD * 500'000'004 % MOD;

		}
	}
	return (sum + x*y) % MOD;
	*/
	ll z = 0;
	ll pojav = 0;

	ll x = x0;
	for (ll u=0; u<=30; u++) {
		if ((1ll << u) & x0) {
			x -= (1ll << u);

			ll y = y0;

			for (ll v=0; v<=30; v++) {
				if ((1ll << v) & y0) {

					y -= (1ll << v);

					ll a = y;
					ll c = x;

					ll baza = a ^ c;
					baza >>= max(u, v);
					baza <<= max(u, v);

					ll granica = baza + max(1ll << u, 1ll << v);

					ll kolicina = min(1ll << u, 1ll << v);

					//cerr << u << ' ' << v << ' ' << k << ' ' <<
					//	baza << ' ' << kolicina << ' ' << granica << '\n';

					// suma svih koji su MANJI od k
					if (k <= baza) {
						z += 0;
					} else if (baza <= k && k < granica) {
						z += sumar(baza, k) * kolicina % MOD;
						pojav += (k - baza) * kolicina % MOD;
					} else {
						z += sumar(baza, granica) * kolicina % MOD;
						pojav += (granica - baza) * kolicina % MOD;
					}
					z %= MOD;
				}
			}
		}
	}
	return (z + pojav) % MOD;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);

	int q;
	cin >> q;
	while (q--) {
		ll x1, y1, x2, y2, k;
		cin >> x1 >> y1 >> x2 >> y2 >> k;
		x1--;
		y1--;

		ll sol = solve(x2, y2, k) + solve(x1, y1, k) -
			solve(x1, y2, k) - solve(x2, y1, k);

		cout << (sol + 2*MOD) % MOD << '\n';
	}
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <array>
#include <list>
#include <stack>
#include <valarray>
#include <fstream>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef long long ll;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
typedef vector<pl> vpl;
typedef vector<vl> vvl;
typedef vector<vpl> vvpl;

const int INF = 1001001001;
const ll INFL = 1001001001001001001LL;

template<typename T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
int in() { int x; cin >> x; return x; }
double fin() { double x; cin >> x; return x; }
string sin() { string x; cin >> x; return x; }
ll lin() { ll x; cin >> x; return x; }

ll mod = 1000000007;

ll solve(ll n, ll x1, ll y1, ll x2, ll y2, ll k, ll plus) {
    if (0 > x2 || 0 > y2 || n - 1 < x1 || n - 1 < y1 || plus >= k) return 0;

    if (x1 <= 0 && n - 1 <= x2 && y1 <= 0 && n - 1 <= y2) {
        ll a = min(plus + n, k);
        ll b = plus + 1;
        ll sol = (a + b) * (a - b + 1) / 2 % mod * n % mod;
        return sol;
    } else if (x1 <= 0 && n - 1 <= x2) {
        ll a = min(plus + n, k);
        ll b = plus + 1;
        ll my1 = max(0LL, y1);
        ll my2 = min(n - 1, y2);
        ll sol = (a + b) * (a - b + 1) / 2 % mod * (my2 - my1 + 1) % mod;
        return sol;
    } else if (y1 <= 0 && n - 1 <= y2) {
        ll a = min(plus + n, k);
        ll b = plus + 1;
        ll mx1 = max(0LL, x1);
        ll mx2 = min(n - 1, x2);
        ll sol = (a + b) * (a - b + 1) / 2 % mod * (mx2 - mx1 + 1) % mod;
        return sol;
    } else {
        ll m = n / 2;
        ll sol1 = solve(m, x1, y1, x2, y2, k, plus);
        ll sol2 = solve(m, x1 - m, y1, x2 - m, y2, k, plus + m);
        ll sol3 = solve(m, x1, y1 - m, x2, y2 - m, k, plus + m);
        ll sol4 = solve(m, x1 - m, y1 - m, x2 - m, y2 - m, k, plus);

        return (sol1 + sol2 + sol3 + sol4) % mod;
    }
}

int main() {
#ifdef LOCAL_ENV
    freopen("sol.in", "r", stdin);
    freopen("sol.out", "w", stdout);
#else
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif

    int q = in();
    forn (cs, q) {
        int x1, y1, x2, y2, k;
        cin >> x1 >> y1 >> x2 >> y2 >> k;

        x1--; y1--; x2--; y2--;

        ll cur = 1;
        while (cur <= x2 || cur <= y2) cur <<= 1;
        cout << solve(cur, x1, y1, x2, y2, k, 0) << '\n';
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int maxn = 200005;
int a[31], b[31], c[31];
ll dp[31][2][2][2];
ll sum[31][2][2][2];
pair<ll,ll> dfs(int pos, int ba, int bb, int bc) {
	if (pos == -1) {
		return {1,0};
	}
	if (~dp[pos][ba][bb][bc]) {
		return {dp[pos][ba][bb][bc], sum[pos][ba][bb][bc]};
	}
	int upa = ba ? a[pos] : 1;
	int upb = bb ? b[pos] : 1;
	int upx = bc ? c[pos] : 1;
	ll cnt = 0;
	ll ans = 0;
	for (int i = 0 ; i <= upa ; i++) {
		for (int j = 0 ; j <= upb ; j++) {
			if ((i ^ j) > upx)continue;
			auto x = dfs(pos - 1, ba && (i == upa), bb && (j == upb), bc && (i ^ j) == upx);
			cnt += x.F;
			ans += x.S + (i^j) * (1LL<<pos) * x.F % mod;
			ans %= mod;
			cnt %= mod;
		}
	}
	dp[pos][ba][bb][bc] = cnt;
	sum[pos][ba][bb][bc] = ans;
	return {cnt, ans};
}
ll solve(int x, int y, int k) {
	if (x < 0 || y < 0 || k < 0) {
		return 0;
	}
	memset(dp, -1 ,sizeof(dp));
	memset(sum, 0 ,sizeof(sum));
	for (int i = 0 ; i < 31 ; i++) {
		a[i] = x & 1;
		x >>= 1;
		b[i] = y & 1;
		y >>= 1;
		c[i] = k & 1;
		k >>= 1;
	}
	auto ret = dfs(30,1,1,1);
	//cout << ret.F <<' '<<ret.S<<endl;
	return (ret.F + ret.S) % mod;
}
void go() {
	int q;
	cin >> q;
	while (q--) {
		int x1, x2 ,y2, y1, k;
		cin >> x1 >> y1 >> x2 >> y2 >> k;
		x1--, x2--, y1--, y2--;
		k--;
		cout << ((solve(x2, y2, k) - solve(x1 - 1, y2, k) - solve(x2, y1 - 1, k) + solve(x1-1,y1-1,k)) % mod + mod) % mod << '\n';
	}
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int c = 0;
    int t;
    if (!c) {
        t = 1;
    }
    else {
        cin >> t;
    }
    while (t--) {
        go();
    }

}

#pragma region Macros
#pragma GCC optimize("O3")

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=a;i>=b;--i)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vec vector<int>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define overload2(_1,_2,name,...) name
#define vv(a,b) vector<vector<int>>(a,vector<int>(b))
#define vv2(a,b,c) vector<vector<int>>(a,vector<int>(b,c))
#define vvl(a,b) vector<vector<ll>>(a,vector<ll>(b))
#define vvl2(a,b,c) vector<vector<ll>>(a,vector<ll>(b,c))
#define vvv(a,b,c) vector<vv(b,c)>(a)
#define vvv2(a,b,c,d) vector<vv(b,c,d)>(a)
#define vvvl(a,b,c) vector<vvl(b,c)>(a)
#define vvvl2(a,b,c,d) vector<vvl(b,c,d)>(a)
#define fi first
#define se second
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance((c).begin(),lower_bound(all(c),(x)))
#define ub(c,x) distance((c).begin(),upper_bound(all(c),(x)))
using namespace std;
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
#define Size(c) (int)(c).size()
#define INT(...) int __VA_ARGS__;IN(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;IN(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;IN(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;IN(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;IN(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;IN(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;IN(__VA_ARGS__)
#define VEC(type,name,size) vector<type> name(size);IN(name)
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));IN(name)
int scan(){ return getchar(); }
void scan(int& a){ cin>>a; }
void scan(long long& a){ cin>>a; }
void scan(char &a){cin>>a;}
void scan(double &a){ cin>>a; }
void scan(long double& a){ cin>>a; }
void scan(char a[]){ scanf("%s", a); }
void scan(string& a){ cin >> a; }
template<class T> void scan(vector<T>&);
template<class T, size_t size> void scan(array<T, size>&);
template<class T, class L> void scan(pair<T, L>&);
template<class T, size_t size> void scan(T(&)[size]);
template<class T> void scan(vector<T>& a){ for(auto& i : a) scan(i); }
template<class T> void scan(deque<T>& a){ for(auto& i : a) scan(i); }
template<class T, size_t size> void scan(array<T, size>& a){ for(auto& i : a) scan(i); }
template<class T, class L> void scan(pair<T, L>& p){ scan(p.first); scan(p.second); }
template<class T, size_t size> void scan(T (&a)[size]){ for(auto& i : a) scan(i); }
template<class T> void scan(T& a){ cin >> a; }
void IN(){}
template <class Head, class... Tail> void IN(Head& head, Tail&... tail){ scan(head); IN(tail...); }
string stin() {string s;cin>>s;return s;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
vi iota(int n){vi a(n);iota(all(a),0);return a;}
template<class T> void UNIQUE(vector<T> &x){sort(all(x));x.erase(unique(all(x)),x.end());}
int in() {int x;cin>>x;return x;}
ll lin() {unsigned long long x;cin>>x;return x;}
void print(){putchar(' ');}
void print(bool a){cout<<a;}
void print(int a){cout<<a;}
void print(long long a){cout<<a;}
void print(char a){cout<<a;}
void print(string &a){cout<<a;}
void print(double a){cout<<a;}
template<class T> void print(const vector<T>&);
template<class T, size_t size> void print(const array<T, size>&);
template<class T, class L> void print(const pair<T, L>& p);
template<class T, size_t size> void print(const T (&)[size]);
template<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout<<" "; print(*i); } cout<<endl;}
template<class T> void print(const deque<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout<<" "; print(*i); } }
template<class T, size_t size> void print(const array<T, size>& a){ print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout<<" "; print(*i); } }
template<class T, class L> void print(const pair<T, L>& p){ cout<<'(';print(p.first); cout<<","; print(p.second);cout<<')'; }
template<class T> void print(set<T> &x){for(auto e:x)print(e),cout<<" ";cout<<endl;}
template<class T, size_t size> void print(const T (&a)[size]){ print(a[0]); for(auto i = a; ++i != end(a); ){ cout<<" "; print(*i); } }
template<class T> void print(const T& a){ cout << a; }
int out(){ putchar('\n'); return 0; }
template<class T> int out(const T& t){ print(t); putchar('\n'); return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }
ll gcd(ll a, ll b){ while(b){ ll c = b; b = a % b; a = c; } return a; }
ll lcm(ll a, ll b){ if(!a || !b) return 0; return a * b / gcd(a, b); }
vector<pll> factor(ll x){ vector<pll> ans; for(ll i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
vector<int> divisor(int x){ vector<int> ans; for(int i=1;i*i<=x;i++)if(x%i==0){ans.pb(i);if(i*i!=x)ans.pb(x/i);} return ans;}
int popcount(ll x){return __builtin_popcountll(x);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){return uniform_int_distribution<int>(0, n-1)(rng);}
#define endl '\n'

#ifdef _LOCAL
    #undef endl
    #define debug(x) cout<<#x<<": ";print(x);cout<<endl;
    void err(){}
    template<class T> void err(const T& t){ print(t);  cout<<" ";}
    template<class Head, class... Tail> void err(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); }
#else
    #define debug(x)
    template<class... T> void err(const T&...){}
#endif
#pragma endregion

const ll MOD=1e9+7;
const int N=1100000;

template <ll Modulus> class modint {
  using u64 = ll;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(((x % Modulus) + Modulus)%Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};
using mint = modint<MOD>;
using vmint = vector<mint> ;
ostream& operator<<(ostream& os, mint a){
    os << a.a ;
    return os;
}
signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);cout<<fixed<<setprecision(15);
    INT(Q);
    while(Q--){
        LL(x1,y1,x2,y2,k);
        auto calc = [](ll a,ll b)->mint{if(b<a) return (mint)0;return (mint)((b+a)*(b-a+1)/2);};
        auto solve = [&](auto &&f,ll x,ll y,int car)->mint{
            if(!x or !y) return (mint)0;
            if(x < y) return f(f,y,x,car);
            int n = 0;
            while((1<<(n+1)) <= x) n++;
            int N = 1<<n;
            if((1<<n) <= y){
                mint res;
                res += (mint)(1<<n)*calc(1+car,min<ll>(N+car,k));
                res += (mint)(x+y-(1<<(n+1)))*calc(car+(1<<n)+1,min<ll>(N*2+car,k));
                res += f(f,x-(1<<n),y-(1<<n),car);
                return res;
            }
            else{
                mint res;
                res += (mint)(y)*calc(1+car,min<ll>(N+car,k));
                res += f(f,x-(1<<n),y,N+car);
                return res;
            }
        };
        // cout<<solve(solve,x2,y2,0) << endl;
        // cout<<solve(solve,x1-1,y2,0)<<endl;
        // cout<<solve(solve,x2,y1-1,0)<<endl;
        // cout<<solve(solve,x1-1,y1-1,0)<<endl;
        cout << solve(solve,x2,y2,0) - solve(solve,x1-1,y2,0) - solve(solve,x2,y1-1,0) + solve(solve,x1-1,y1-1,0) << "\n";
    }

}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

const long long mod = 1000000007;

long long product(vector<long long>vec) {
	long long r = 1; for (int i = 0; i < vec.size(); i++) { r *= (vec[i] % mod); r %= mod; }
	return r;
}

pair<long long, long long> solve(long long sizex, long long sizey, long long k, long long rem) {
	if (sizex == 0 || sizey == 0 || rem < 0) return make_pair(0LL, 0LL);

	long long Z1 = (sizex / k);
	long long Z2 = (sizey / k);

	long long sum = 0, ret = 0, B1 = min(k - 1, rem);
	sum += product({ Z1 * Z2 * k, (B1 + 2LL) * (B1 + 1LL) / 2LL });
	ret += product({ Z1 * Z2 * k, B1 + 1LL});

	if (Z1 == 0 || rem >= k) {
		long long B2 = min(k - 1, rem - Z1 * k);
		sum += product({ Z2, sizex - Z1 * k, (B2 + 2LL) * (B2 + 1LL) / 2LL }) + product({ Z2, Z1 * k, sizex - Z1 * k, B2 + 1LL });
		ret += product({ Z2, sizex - Z1 * k, B2 + 1LL });
	}
	if (Z2 == 0 || rem >= k) {
		long long B2 = min(k - 1, rem - Z2 * k);
		sum += product({ Z1, sizey - Z2 * k, (B2 + 2LL) * (B2 + 1LL) / 2LL }) + product({ Z1, Z2 * k, sizey - Z2 * k, B2 + 1LL });
		ret += product({ Z1, sizey - Z2 * k, B2 + 1LL });
	}

	if ((Z1 ^ Z2) == 0) {
		pair<long long, long long> E = solve(sizex - Z1 * k, sizey - Z2 * k, k >> 1, rem);
		sum += E.first;
		ret += E.second;
	}
	if ((Z1 ^ Z2) == 1) {
		pair<long long, long long> E = solve(sizex - Z1 * k, sizey - Z2 * k, k >> 1, rem - k);
		sum += E.first + product({ k, E.second });
		ret += E.second;
	}
	return make_pair(sum % mod, ret % mod);
}

int main() {
	long long Q, ax, ay, bx, by, ca;
	scanf("%lld", &Q);

	for (int i = 1; i <= Q; i++) {
		scanf("%lld%lld%lld%lld%lld", &ax, &ay, &bx, &by, &ca); ca--;
		long long V1 = solve(ax - 1, ay - 1, (1LL << 30), ca).first;
		long long V2 = solve(bx - 0, ay - 1, (1LL << 30), ca).first;
		long long V3 = solve(ax - 1, by - 0, (1LL << 30), ca).first;
		long long V4 = solve(bx - 0, by - 0, (1LL << 30), ca).first;
		printf("%lld\n", (V1 + V4 - V2 - V3 + mod * mod) % mod);
	}
	return 0;
}

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int bitmaks=30;
const LL mods=1000000007LL;
#define x1 uvuvwe
#define x2 onyete
#define y1 onyetve
#define y2 ugmewuben
int q,target,batas1,batas2;
LL jumlah[bitmaks+5][5][5][5],ans,dp[bitmaks+5][5][5][5];
LL modulo(LL angka){
	if(angka>=mods||angka<=-mods)
		angka%=mods;
	if(angka<0)
		angka+=mods;
	return angka;
}
int bitke(int angka,int idx){
	return (angka&(1<<idx))?1:0;
}
LL topdown(int digitnow,bool sama1,bool sama2,bool samatarget){
	if(digitnow==-1)
		return 1;
	LL &ret=dp[digitnow][sama1][sama2][samatarget];
	if(ret!=-1)
		return ret;
	ret=0;
	int loop1,loop2,digittarget;
	loop1=(sama1)?bitke(batas1,digitnow):1;
	loop2=(sama2)?bitke(batas2,digitnow):1;
	digittarget=(samatarget)?bitke(target,digitnow):1;
	//cout<<"batas loopnya "<<loop1<<" "<<loop2<<endl;
	for(int i=0;i<=loop1;i++)
	{
		for(int j=0;j<=loop2;j++)
		{
			if((i^j)<=digittarget)
			{
				bool nextsama1=(sama1)?(i==loop1):false;
				bool nextsama2=(sama2)?(j==loop2):false;
				bool nextsamatarget=(samatarget)?((i^j)==digittarget):false;
				LL banyak=topdown(digitnow-1,nextsama1,nextsama2,nextsamatarget);
				ret+=banyak;
				jumlah[digitnow][sama1][sama2][samatarget]+=modulo(modulo(banyak)*modulo(LL((i^j)<<digitnow)));
				if(digitnow!=0)
					jumlah[digitnow][sama1][sama2][samatarget]+=jumlah[digitnow-1][nextsama1][nextsama2][nextsamatarget];
				jumlah[digitnow][sama1][sama2][samatarget]=modulo(jumlah[digitnow][sama1][sama2][samatarget]);
			}
		}
	}
	return ret;
}
LL total(int angka1,int angka2){
	if(angka1<0||angka2<0)
		return 0;
	batas1=angka1;
	batas2=angka2;
	memset(dp,-1,sizeof(dp));
	memset(jumlah,0,sizeof(jumlah));
	LL temp=topdown(bitmaks,true,true,true);
	return modulo((LL) jumlah[bitmaks][1][1][1]+temp);
}
int main()
{
	scanf("%d",&q);
	for(int i=1;i<=q;i++)
	{
		int x1,x2,y1,y2;
		scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&target);
		x1--,y1--,x2--,y2--,target--;
		//cout<<"soalnya itu dari range "<<x1<<" "<<x2<<endl;
		//cout<<"dan range "<<y1<<" "<<y2<<endl;
		//cout<<"targetnya "<<target<<endl;
		ans=modulo(total(x2,y2)-total(x1-1,y2)+total(x1-1,y1-1)-total(x2,y1-1));
		printf("%lld\n",ans);
	}
}

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ";
	dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
	return os<<vc<t>(all(a));
}

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

template<class t>
void print(t x,int suc=1){
	cout<<x;
	if(suc==1)
		cout<<"\n";
	if(suc==2)
		cout<<" ";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

pi readpi(int off=0){
	int a,b;cin>>a>>b;
	return pi(a+off,b+off);
}

template<class T>
void print(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print(v[i],i==int(v.size())-1?suc:2);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

//#define CAPITAL
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<"\n";
	#else
	cout<<"Yes"<<"\n";
	#endif
	if(ex)exit(0);
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<"\n";
	#else
	cout<<"No"<<"\n";
	#endif
	if(ex)exit(0);
}
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<"\n";
	#else
	cout<<"Possible"<<"\n";
	#endif
	if(ex)exit(0);
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<"\n";
	#else
	cout<<"Impossible"<<"\n";
	#endif
	if(ex)exit(0);
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
ll mask(int i){
	return (ll(1)<<i)-1;
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
	#ifdef LOCAL
	static mt19937_64 gen;
	#else
	static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	return uniform_int_distribution<ll>(l, r)(gen);
}

template<class t>
void myshuffle(vc<t>&a){
	rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}

template<class t>
int lwb(const vc<t>&v,const t&a){
	return lower_bound(all(v),a)-v.bg;
}

struct modinfo{uint mod,root;};
template<modinfo const&ref>
struct modular{
	static constexpr uint const &mod=ref.mod;
	static modular root(){return modular(ref.root);}
	uint v;
	//modular(initializer_list<uint>ls):v(*ls.bg){}
	modular(ll vv=0){s(vv%mod+mod);}
	modular& s(uint vv){
		v=vv<mod?vv:vv-mod;
		return *this;
	}
	modular operator-()const{return modular()-*this;}
	modular& operator+=(const modular&rhs){return s(v+rhs.v);}
	modular&operator-=(const modular&rhs){return s(v+mod-rhs.v);}
	modular&operator*=(const modular&rhs){
		v=ull(v)*rhs.v%mod;
		return *this;
	}
	modular&operator/=(const modular&rhs){return *this*=rhs.inv();}
	modular operator+(const modular&rhs)const{return modular(*this)+=rhs;}
	modular operator-(const modular&rhs)const{return modular(*this)-=rhs;}
	modular operator*(const modular&rhs)const{return modular(*this)*=rhs;}
	modular operator/(const modular&rhs)const{return modular(*this)/=rhs;}
	modular pow(int n)const{
		modular res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modular inv()const{return pow(mod-2);}
	/*modular inv()const{
		int x,y;
		int g=extgcd<ll>(v,mod,x,y);
		assert(g==1);
		if(x<0)x+=mod;
		return modular(x);
	}*/
	friend modular operator+(int x,const modular&y){
		return modular(x)+y;
	}
	friend modular operator-(int x,const modular&y){
		return modular(x)-y;
	}
	friend modular operator*(int x,const modular&y){
		return modular(x)*y;
	}
	friend modular operator/(int x,const modular&y){
		return modular(x)/y;
	}
	friend ostream& operator<<(ostream&os,const modular&m){
		return os<<m.v;
	}
	friend istream& operator>>(istream&is,modular&m){
		ll x;is>>x;
		m=modular(x);
		return is;
	}
	bool operator<(const modular&r)const{return v<r.v;}
	bool operator==(const modular&r)const{return v==r.v;}
	bool operator!=(const modular&r)const{return v!=r.v;}
	explicit operator bool()const{
		return v;
	}
};

//extern constexpr modinfo base{998244353,3};
extern constexpr modinfo base{1000000007,0};
//modinfo base{1,0};
using mint=modular<base>;

const int vmax=(1<<21)+10;
mint fact[vmax],finv[vmax],invs[vmax];
void initfact(){
	fact[0]=1;
	rng(i,1,vmax){
		fact[i]=fact[i-1]*i;
	}
	finv[vmax-1]=fact[vmax-1].inv();
	for(int i=vmax-2;i>=0;i--){
		finv[i]=finv[i+1]*(i+1);
	}
	for(int i=vmax-1;i>=1;i--){
		invs[i]=finv[i]*fact[i-1];
	}
}
mint choose(int n,int k){
	return fact[n]*finv[n-k]*finv[k];
}
mint binom(int a,int b){
	return fact[a+b]*finv[a]*finv[b];
}
mint catalan(int n){
	return binom(n,n)-(n-1>=0?binom(n-1,n+1):0);
}

/*
const int vmax=110;
mint binbuf[vmax][vmax];
mint choose(int n,int k){
	return binbuf[n-k][k];
}
mint binom(int a,int b){
	return binbuf[a][b];
}
void initfact(){
	binbuf[0][0]=1;
	rep(i,vmax)rep(j,vmax){
		if(i)binbuf[i][j]+=binbuf[i-1][j];
		if(j)binbuf[i][j]+=binbuf[i][j-1];
	}
}
*/

const int L=32;
int p2[L];

void dfs(int b,int e,int l,int r,int lv,vc<pi>&dst){
	if(b<=l&&r<=e){
		dst.eb((l>>lv)<<lv,lv);
		return;
	}
	if(e<=l||r<=b)return;
	dfs(b,e,l,(l+r)/2,lv-1,dst);
	dfs(b,e,(l+r)/2,r,lv-1,dst);
}

vc<pi> waf(int l,int r){
	vc<pi> dst;
	dfs(l,r,0,1LL<<L,L,dst);
	return dst;
}

void slv(){
	int a,b,c,d;cin>>a>>b>>c>>d;
	auto xs=waf(a-1,c);
	auto ys=waf(b-1,d);
	dmp(xs);
	dmp(ys);
	int k;cin>>k;k--;
	mint ans;
	for(auto x:xs)for(auto y:ys){
		int lv=max(x.b,y.b);
		int cur=(x.a^y.a)>>lv;
		int tar=(k)>>lv;
		dmp2(lv,cur,tar);
		int num=0;
		if(cur<tar){
			num=p2[lv];
		}else if(cur==tar){
			num=(k&mask(lv))+1;
		}
		int lw=cur<<lv,up=lw+num;
		dmp2(lw,up,p2[min(x.b,y.b)]);
		ans+=(lw+up+1)*(up-lw)/2*mint(p2[min(x.b,y.b)]);
	}
	print(ans);
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);

	p2[0]=1;
	rng(i,1,L)p2[i]=p2[i-1]*2;

	int q;cin>>q;
	rep(_,q)slv();
}

#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007
typedef long long int LLI;

#define L 0
#define H 1
#define W 2
#define HW 3

#define LEFT 0
#define MAXNUM 1

int q;
LLI x1;
LLI my1;
LLI x2;
LLI y2;
LLI k;
LLI stage[32];
LLI width[32];
LLI height[32];
LLI dp[32][2][4];
LLI num[32][2][4];
bool exist[32][2][4];

LLI calc(int x, int y) {
  if (x == 0 || y == 0) return 0;
  int t = max(x, y);
  int cnt = 1;
  while (t > 0) {
    cnt++;
    t /= 2;
  }

  LLI k_ = k;
  int cnt2 = 1;
  while (1) {
    k_ = k_%2 ? (k_+1)/2 : k_/2;
    cnt2++;
    if (k_ == 1) break;
  }
  cnt = max(cnt, cnt2);

  width[cnt-1] = x;
  height[cnt-1] = y;
  stage[cnt-1] = k;
  for (int i=cnt-2; i>=0; i--) {
    stage[i] = stage[i+1]%2 ? (stage[i+1]+1)/2 : stage[i+1]/2;
    width[i] = width[i+1]/2 + width[i+1]%2;
    height[i] = height[i+1]/2 + height[i+1]%2;
  }

  fill(num[0][0], num[cnt][0], 0);
  fill(dp[0][0], dp[cnt][0], 0);
  fill(exist[0][0], exist[cnt][0], false);

  num[0][MAXNUM][HW] = 1;
  dp[0][MAXNUM][HW] = 1;
  exist[0][MAXNUM][HW] = true;
  for (int i=0; i<cnt-1; i++) {
    for (int j=0; j<2; j++) {
      for (int k=0; k<4; k++) {
        if (!exist[i][j][k]) continue;
        for (int s=0; s<2; s++) {
          for (int t=0; t<2; t++) {
            LLI p = (s+t)%2;
            LLI v = (MOD + ((dp[i][j][k]*2+(p-1)*num[i][j][k])%MOD))%MOD;
            LLI n = num[i][j][k];
            if (j == MAXNUM && p && stage[i]*2 > stage[i+1]) continue;
            if ((k&W) && s && width[i]*2 > width[i+1]) continue;
            if ((k&H) && t && height[i]*2 > height[i+1]) continue;

            int nj = LEFT;
            if (j == MAXNUM && stage[i]*2-1+p == stage[i+1]) nj = MAXNUM;

            int nk = L;
            if ((k&H) && height[i]*2-1+t == height[i+1]) nk += H;
            if ((k&W) && width[i]*2-1+s == width[i+1]) nk += W;

            exist[i+1][nj][nk] = true;
            dp[i+1][nj][nk] = (dp[i+1][nj][nk]+v)%MOD;
            num[i+1][nj][nk] = (num[i+1][nj][nk]+n)%MOD;
          }
        }
      }
    }
  }

  /*
  for (int i=0; i<cnt; i++) {
    printf("cnt: %d stage: %d width: %d height: %d\n", i, stage[i], width[i], height[i]);
    printf("LEFT:");
    for (int j=0; j<4; j++) {
      printf(" %I64d ", dp[i][LEFT][j]);
    }puts("");
    for (int j=0; j<4; j++) {
      printf(" %I64d ", num[i][LEFT][j]);
    }puts("");
    printf("maxnum:");
    for (int j=0; j<4; j++) {
      printf(" %I64d ", dp[i][MAXNUM][j]);
    }puts("");
    for (int j=0; j<4; j++) {
      printf(" %I64d ", num[i][MAXNUM][j]);
    }puts("");
  }*/

  LLI ret = 0;
  for (int i=0; i<4; i++) {
    ret += dp[cnt-1][LEFT][i];
    ret += dp[cnt-1][MAXNUM][i];
    ret %= MOD;
  }
  //printf("ret: %I64d\n", ret);
  return ret;
}

int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%I64d%I64d%I64d%I64d%I64d", &x1, &my1, &x2, &y2, &k);
    LLI ans = calc(x2, y2) - calc(x2, my1-1) - calc(x1-1, y2) + calc(x1-1, my1-1);
    printf("%I64d\n", (MOD+ans%MOD)%MOD);
  }
}

#include<cstdio>
int n;
typedef long long ll;
const ll mod=1e9+7;
struct cp
{ll sm,k;cp(ll _s=0,ll _k=0){sm=_s,k=_k;}};
inline cp g(ll x,ll y,ll k,ll r)
{
	if(!x)return cp();
	if(!y)return cp();
	if(k<0)return cp();
	//printf("%lld %lld %lld %lld\n",x,y,r);
	if(x<y)x^=y,y^=x,x^=y;
	ll p=1ll<<r;
	while(p>x)p>>=1,r--;
	if(x==p)
	{
		if(p<=k)return cp((y*(((p*(p+1))/2)%mod))%mod,(p*y)%mod);
		else return cp((y*(((k*(k+1))/2)%mod))%mod,(k*y)%mod);
	}while(p>=x)p>>=1,r--;
	if(p<y)
	{
		cp d1=g(p,p,k,r),d2=g(x-p,p,k-p,r),
		d3=g(p,y-p,k-p,r),d4=g(x-p,y-p,k,r);
		return cp((d1.sm+d2.sm+d3.sm+d4.sm+(d2.k+d3.k)*p)%mod
		,(d1.k+d2.k+d3.k+d4.k)%mod);
	}
	else
	{
		cp d1=g(p,y,k,r),d2=g(x-p,y,k-p,r);
		return cp((d1.sm+d2.sm+d2.k*p)%mod,(d1.k+d2.k)%mod);
	}
}
inline ll f(ll x,ll y,ll k,ll r=30)
{
	if(!x)return 0;
	if(!y)return 0;
	if(x<y)x^=y,y^=x,x^=y;
	ll p=1ll<<r;
	while(p>x)p>>=1,r--;
	return g(x,y,k,r).sm;
}
int main()
{
	//freopen("c.in","r",stdin);
	//freopen("c.out","w",stdout);
	scanf("%d",&n);
	register int i;
	for(i=1;i<=n;i++)
	{
		int x1,y1,x2,y2,k;
		scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
		//printf("%lld %lld %lld %lld\n",f(x2,y2,k),f(x1-1,y2,k)
		//,f(x2,y1-1,k),f(x1-1,y1-1,k));
		printf("%I64d\n",(f(x2,y2,k)-f(x1-1,y2,k)
		-f(x2,y1-1,k)+f(x1-1,y1-1,k)+mod*10)%mod);
	}
}

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MOD = 1000000007;
void add(int &x, int y)
{
	x += y;
	if (x >= MOD)
		x -= MOD;
}
inline int read()
{
	int x = 0;
	bool t = false;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-')
		t = true, ch = getchar();
	while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
	return t ? -x : x;
}
int f[32][2][2][2], g[32][2][2][2];
int Calc(int L, int R, int K)
{
	if (L < 0 || R < 0)
		return 0;
	memset(f, 0, sizeof(f));
	memset(g, 0, sizeof(g));
	f[31][0][0][0] = 1;
	for (int i = 30; ~i; -- i)
		for (int a = 0; a <= 1; ++ a)
			for (int b = 0; b <= 1; ++ b)
				for (int k = 0; k <= 1; ++ k)
				{
					if (!f[i + 1][a][b][k])
						continue;
					for (int A = 0; A <= 1; ++ A)
						for (int B = 0; B <= 1; ++ B)
						{
							if (!a && A && !(L & (1 << i)))
								continue;
							if (!b && B && !(R & (1 << i)))
								continue;
							if (!k && !(K & (1 << i)) && (A ^ B))
								continue;
							int na = a, nb = b, nk = k;
							if (!A && (L & (1 << i)))
								na |= 1;
							if (!B && (R & (1 << i)))
								nb |= 1;
							if (!(A ^ B) && (K & (1 << i)))
								nk |= 1;
							add(f[i][na][nb][nk], f[i + 1][a][b][k]);
							add(g[i][na][nb][nk], g[i + 1][a][b][k]);
							if (A ^ B)
								add(g[i][na][nb][nk], 1ll * (1 << i) * f[i + 1][a][b][k] % MOD);
						}
				}
	int ret = 0;
	for (int a = 0; a <= 1; ++a)
		for (int b = 0; b <= 1; ++b)
			for (int k = 0; k <= 1; ++k) add(ret, g[0][a][b][k]), add(ret, f[0][a][b][k]);
	return ret;
}
int main()
{
	int Q = read();
	while (Q --)
	{
		int x1 = read() - 1, x2 = read() - 1, y1 = read() - 1, y2 = read() - 1, k = read() - 1;
		int ans1 = (Calc(y1, y2, k) + Calc(x1 - 1, x2 - 1, k)) % MOD;
		int ans2 = (Calc(x1 - 1, y2, k) + Calc(y1, x2 - 1, k)) % MOD;
		int ans = (ans1 + MOD - ans2) % MOD;
		printf("%d\n", ans);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9+7;

int Count(int x, int y, int k) {
    if(!x or !y) return 0;
    int ans = 0;
    for(int kx = 29; kx >= 0; kx--) {
        if(!((x>>kx)&1)) continue;
        for(int ky = 29; ky >= 0; ky--) {
            if(!((y>>ky)&1)) continue;
            int same = max(kx, ky);
            int x_ = x^(1<<kx);
            int y_ = y^(1<<ky);
            x_ = (x_>>same)<<same;
            y_ = (y_>>same)<<same;
            int Xor = x_^y_;
            if(k < Xor) continue;
            int baki = k-Xor;
            int m = baki+1;
            if(baki >= (1<<same)) m = (1<<same);
            long long sum = 1ll*(1ll*2*Xor+m-1)*m/2;
            sum %= mod;
            sum += m;
            sum %= mod;
            int cur = 1ll*sum*(1<<min(kx,ky))%mod;
            ans += cur;
            if(ans >= mod) ans -= mod;
        }
    }
    return ans;
}

int solve(int x1, int y1, int x2, int y2, int k) {
    int ret = Count(x2, y2, k) + Count(x1-1, y1-1, k);
    ret %= mod;
    ret += mod-Count(x1-1, y2, k);
    ret %= mod;
    ret += mod-Count(x2, y1-1, k);
    ret %= mod;
    return ret;
}

int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin >> q;
    while(q--) {
        int x1, x2, y1, y2, k;
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        k--;
        cout << solve(x1, y1, x2, y2, k) << "\n";
    }
    return 0;
}

#include<bits/stdc++.h>

const int N = 100;
const int moder = 1e9 + 7;

int power[N];
int test;

int powermod(int a, int exp){
	int ret = 1;
	for ( ; exp; exp >>= 1){
		if (exp & 1){
			ret = 1ll * ret * a % moder;
		}
		a = 1ll * a * a % moder;
	}
	return ret;
}

int sum(int x){
	return 1ll * x * (x + 1) / 2 % moder;
}

int solve(int x, int y, int k){
	int ret = 0;
	for (int i = 0; i < 30; ++ i){
		for (int j = 0; j < 30; ++ j){
			if ((x >> i & 1) && (y >> j & 1)){
				int x1 = (x >> i) - 1, y1 = (y >> j) - 1;
				if (i < j){
					x1 >>= j - i;
				}
				else{
					y1 >>= i - j;
				}
				int z1 = x1 ^ y1, z2 = z1, max = std::max(i, j);
				for (int u = 0; u < max; ++ u){
					z1 = z1 << 1;
					z2 = z2 << 1 | 1;
				}
				if (k < z1){
					continue;
				}
				int kk = std::min(z2, k);
				ret = (ret + 1ll * power[std::min(i, j)] * (sum(kk) - sum(z1 - 1) + moder)) % moder;
				ret = (ret + 1ll * power[std::min(i, j)] * (kk - z1 + 1 + moder)) % moder;
			}
		}
	}
	return ret;
}

int main(){
	power[0] = 1;
	for (int i = 1; i < N; ++ i){
		power[i] = 2 * power[i - 1] % moder;
	}
	scanf("%d", &test);
	while (test --){
		int x1, x2, y1, y2, k;
		scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
		-- k;
		int ans = (solve(x2, y2, k) + solve(x1 - 1, y1 - 1, k)) % moder;
		ans = (ans - solve(x2, y1 - 1, k) + moder) % moder;
		ans = (ans - solve(x1 - 1, y2, k) + moder) % moder;
		printf("%d\n", ans);
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

const int md = (int) 1e9 + 7;

inline void add(int &a, int b) {
  a += b;
  if (a >= md) a -= md;
}

inline void sub(int &a, int b) {
  a -= b;
  if (a < 0) a += md;
}

inline int mul(int a, int b) {
#if !defined(_WIN32) || defined(_WIN64)
  return (int) ((long long) a * b % md);
#endif
  unsigned long long x = (long long) a * b;
  unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m;
  asm(
    "divl %4; \n\t"
    : "=a" (d), "=d" (m)
    : "d" (xh), "a" (xl), "r" (md)
  );
  return m;
}

inline int power(int a, long long b) {
  int res = 1;
  while (b > 0) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

inline int inv(int a) {
  a %= md;
  if (a < 0) a += md;
  int b = md, u = 0, v = 1;
  while (a) {
    int t = b / a;
    b -= t * a; swap(a, b);
    u -= t * v; swap(u, v);
  }
  assert(b == 1);
  if (u < 0) u += md;
  return u;
}

const int n = 32;
vector<int> p(n << 1);

int solve(int a, int b, int k) {
  if(a == -1 || b == -1) return 0;
  int res = 0;
  a++; b++;
  range(x, 0, n) {
    if(!((a >> x) & 1)) continue;
    range(y, 0, n) {
      if(!((b >> y) & 1)) continue;
      int sa = a ^ (1 << x), sb = b ^ (1 << y);
      int s = max(x, y);
      int u = (sa ^ sb) >> s;
      int cnt = x + y;
      if(u > (k >> s)) continue;
      if(u < (k >> s)) {
        add(res, p[cnt]);
        add(res, mul((u << s) % md, p[cnt]));
        if(x + y) add(res, mul(p[s] - 1, p[x + y - 1]));
        continue;
      }
      husk(z, s - 1, -1) {
        cnt--;
        if(!((k >> z) & 1)) continue;
        add(res, p[cnt]);
        int v = ((k >> z) ^ 1) << z;
        add(res, mul(v % md, p[cnt]));
        if(cnt) add(res, mul(p[z] - 1, p[cnt - 1]));
      }
    }
  }
  return res;
}

int q;

int main() {
  range(i, 0, n << 1) p[i] = power(2, i);
  cin >> q;
  while(q--) {
    int x1, y1, x2, y2, k;
    scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
    x1--; y1--; x2--; y2--;
    int res = solve(x2, y2, k);
    sub(res, solve(x1 - 1, y2, k));
    sub(res, solve(x2, y1 - 1, k));
    add(res, solve(x1 - 1, y1 - 1, k));
    printf("%d\n", res);
  }
  return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
int q,x_1,y_1,x_2,y_2,k,len,a[35],b[35],c[35];
pair<int,int>dp[35][2][2][2];
int Add(int a,int b){
	return a+b>=mod?a+b-mod:a+b;
}
int Minus(int a,int b){
	return a<b?a-b+mod:a-b;
}
pair<int,int>operator+(pair<int,int>a,pair<int,int>b){
	return make_pair(Add(a.first,b.first),Add(a.second,b.second));
}
pair<int,int>dfs(int now,int f1,int f2,int f3){
	if(now==-1)
		return make_pair(1,0);
	if(~dp[now][f1][f2][f3].first)
		return dp[now][f1][f2][f3];
	pair<int,int>ret=make_pair(0,0);
	for(int i=0;i<=(f1?a[now]:1);i++)
		for(int j=0;j<=(f2?b[now]:1);j++)
			if((i^j)<=(f3?c[now]:1)){
				pair<int,int>tmp=dfs(now-1,f1&&i==a[now],f2&&j==b[now],f3&&(i^j)==c[now]);
				ret=ret+tmp;
				if(i^j)
					ret.second=(ret.second+(1ll<<now)*tmp.first)%mod;
			}
	dp[now][f1][f2][f3]=ret;
	return ret;
}
int calc(int x,int y,int k){
	if(x<0||y<0||k<0)
		return 0;
	memset(dp,-1,sizeof(dp));
	len=0;
	while(x||y||k){
		a[len]=(x&1);
		b[len]=(y&1);
		c[len++]=(k&1);
		x>>=1;
		y>>=1;
		k>>=1;
	}
	pair<int,int>ret=dfs(len-1,1,1,1);
	return Add(ret.first,ret.second);
}
int main(){
	scanf("%d",&q);
	while(q--){
		scanf("%d%d%d%d%d",&x_1,&y_1,&x_2,&y_2,&k);
		printf("%d\n",Minus(Add(calc(x_2-1,y_2-1,k-1),calc(x_1-2,y_1-2,k-1)),Add(calc(x_1-2,y_2-1,k-1),calc(x_2-1,y_1-2,k-1))));
	}
	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>

#include <cstdlib>
#include <cstring>

#define mod (int)(1e9 + 7)

using namespace std;

static long long dp[32][2][2][2];
static long long sum[32][2][2][2];
static long long pot[32];

void add(long long &x, long long y)
{
	x += y;
	if (x >= mod)
	{
		x -= mod;
	}
}

void sub(long long &x, long long y)
{
	x -= y;
	if (x < 0)
	{
		x += mod;
	}
}

long long mul(long long x, long long y)
{
	return x * y % mod;
}

long long solve(int x, int y, int z)
{
	if (x < 0 || y < 0 || z < 0)
	{
		return 0;
	}

	memset(dp, 0, sizeof(dp));
	memset(sum, 0, sizeof(sum));

	vector<int> A, B, C;

	for (int i = 0; i < 31; i++)
	{
		A.push_back(x % 2);
		x /= 2;
		B.push_back(y % 2);
		y /= 2;
		C.push_back(z % 2);
		z /= 2;
	}
	reverse(A.begin(), A.end());
	reverse(B.begin(), B.end());
	reverse(C.begin(), C.end());

	dp[0][1][1][1] = 1;
	sum[0][1][1][1] = 0;

	for (int i = 0; i < 31; i++)
	{
		for (int a = 0; a < 2; a++)
		{
			for (int b = 0; b < 2; b++)
			{
				for (int c = 0; c < 2; c++)
				{
					for (int x = 0; x < 2; x++)
					{
						for (int y = 0; y < 2; y++)
						{
							int z = x ^ y;

							if (a == 1 && A[i] == 0 && x == 1)
							{
								continue;
							}

							if (b == 1 && B[i] == 0 && y == 1)
							{
								continue;
							}

							if (c == 1 && C[i] == 0 && z == 1)
							{
								continue;
							}

							add(dp[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)], dp[i][a][b][c]);
							add(sum[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)], sum[i][a][b][c]);
							add(sum[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)], mul(z << (30 - i), dp[i][a][b][c]));
						}
					}
				}
			}
		}
	}

	long long res = 0;

	for (int a = 0; a < 2; a++)
	{
		for (int b = 0; b < 2; b++)
		{
			for (int c = 0; c < 2; c++)
			{
				add(res, sum[31][a][b][c]);
				add(res, dp[31][a][b][c]);
			}
		}
	}
	return res;
}

int main(int argc, const char *argv[])
{

	int test_case = 0;

	cin >> test_case;

	for (int test = 0; test < test_case; test++)
	{
		int x1 = 0, y1 = 0, x2 = 0, y2 = 0, k = 0;
		cin >> x1 >> y1 >> x2 >> y2 >> k;

		x1--;
		x2--;
		y1--;
		y2--;
		k--;

		long long res = 0;

		add(res, solve(x2, y2, k));
		sub(res, solve(x2, y1 - 1, k));
		sub(res, solve(x1 - 1, y2, k));
		add(res, solve(x1 - 1, y1 - 1, k));

		cout << res << endl;
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef double DB;

#define pb push_back
#define mset(a, b) memset(a, b, sizeof a)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1 << (x))
#define bitl(x) (1LL << (x))
#define sqr(x) ((x) * (x))
#define sz(x) ((int)(x.size()))
#define cnti(x) (__builtin_popcount(x))
#define cntl(x) (__builtin_popcountll(x))
#define clzi(x) (__builtin_clz(x))
#define clzl(x) (__builtin_clzll(x))
#define ctzi(x) (__builtin_ctz(x))
#define ctzl(x) (__builtin_ctzll(x))

#define X first
#define Y second

#define Error(x) cout << #x << " = " << x << endl

template <typename T, typename U>
inline void chkmax(T& x, U y) {
	if (x < y) x = y;
}

template <typename T, typename U>
inline void chkmin(T& x, U y) {
	if (y < x) x = y;
}

const int MOD = 1e9 + 7;

int dp[2][4][2], sum[2][4][2];
int x[2], y[2];

void add(int &x, int y) {
	x += y;
	if (x >= MOD) x -= MOD;
}

int calc(int x, int y, int m) {
	if (x == 0 || y == 0) return 0;
	x--, y--, m--;
	int n = 32 - clzi(max(max(x, y), m));
	memset(dp, 0, sizeof dp);
	memset(sum, 0, sizeof sum);
	dp[0][0][0] = 1;
	int now = 0;
	for (int i = n; i >= 0; i--) {
		now ^= 1;
		memset(dp[now], 0, sizeof dp[now]);
		memset(sum[now], 0, sizeof sum[now]);
		int a[2], b, ma[2], mb;
		a[0] = x >> i & 1, a[1] = y >> i & 1, b = m >> i & 1;
		for (int j = 0; j < 4; j++) {
			for (int k = 0; k < 2; k++) {
				if (j & 1) {
					ma[0] = 2;
				} else {
					ma[0] = a[0]+1;
				}
				if (j & 2) {
					ma[1] = 2;
				} else {
					ma[1] = a[1]+1;
				}
				if (k) mb = 2;
				else mb = b+1;
				for (int l = 0; l < ma[0]; l++) {
					for (int ll = 0; ll < ma[1]; ll++) {
						if ((l ^ ll) >= mb) continue;
						int nj1 = !!(((j & 1) || (l < a[0])));
						int nj2 = !!((((j & 2) > 0) || (ll < a[1])));
						int nk = !!((k || ((l ^ ll) < b)));
						add(dp[now][nj2 * 2 + nj1][nk], dp[!now][j][k]);
						add(sum[now][nj2 * 2 + nj1][nk], sum[!now][j][k] * 2 % MOD);
						if (l != ll) add(sum[now][nj2 * 2 + nj1][nk], dp[!now][j][k]);
					}
				}
			}
		}
	}
	int ret = 0;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 2; j++) {
			add(ret, sum[now][i][j]);
			add(ret, dp[now][i][j]);
		}
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int ncase;
	for (scanf("%d", &ncase); ncase--; ) {
		for (int i = 0; i < 2; i++) scanf("%d%d", &x[i], &y[i]); x[0]--, y[0]--;
		int m; scanf("%d", &m);
		int ans = 0;
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 2; j++) {
				if (i == j) ans = (ans + calc(x[i], y[j], m)) % MOD;
				else ans = (ans + MOD - calc(x[i], y[j], m)) % MOD;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
const int N = (int)5e5 + 50;
const int MOD = (int)1e9 + 7;

struct mi {
    typedef decay<decltype(MOD)>::type T;
    /// don't silently convert to T
    T v; explicit operator T() const { return v; }
    mi() { v = 0; }
    mi(ll _v) {
        v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
        if (v < 0) v += MOD;
    }
    friend bool operator==(const mi& a, const mi& b) {
        return a.v == b.v; }
    friend bool operator!=(const mi& a, const mi& b) {
        return !(a == b); }
    friend bool operator<(const mi& a, const mi& b) {
        return a.v < b.v; }
//    friend void re(mi& a) { ll x; re(x); a = mi(x); }
//    friend str ts(mi a) { return ts(a.v); }

    mi& operator+=(const mi& m) {
        if ((v += m.v) >= MOD) v -= MOD;
        return *this; }
    mi& operator-=(const mi& m) {
        if ((v -= m.v) < 0) v += MOD;
        return *this; }
    mi& operator*=(const mi& m) {
        v = (ll)v*m.v%MOD; return *this; }
    mi& operator/=(const mi& m) { return (*this) *= inv(m); }
    friend mi pow(mi a, ll p) {
        mi ans = 1; assert(p >= 0);
        for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans;
    }
    friend mi inv(const mi& a) { assert(a.v != 0);
        return pow(a,MOD-2); }

    mi operator-() const { return mi(-v); }
    mi& operator++() { return *this += 1; }
    mi& operator--() { return *this -= 1; }
    friend mi operator+(mi a, const mi& b) { return a += b; }
    friend mi operator-(mi a, const mi& b) { return a -= b; }
    friend mi operator*(mi a, const mi& b) { return a *= b; }
    friend mi operator/(mi a, const mi& b) { return a /= b; }
};

typedef pair<mi, mi> pmi;

int a, b, k;
bool vis[31][2][2][2];
mi dp[31][2][2][2], sdp[31][2][2][2];
mi tres = 0;

pmi solve(int bt, int ae, int be, int ke) {
    if(bt == -1) return {1, 0};
    if(vis[bt][ae][be][ke]) return {dp[bt][ae][be][ke], sdp[bt][ae][be][ke]};
    dp[bt][ae][be][ke] = sdp[bt][ae][be][ke] = 0;
    vis[bt][ae][be][ke] = true;
    int u1 = (ae && !(a >> bt & 1)) ? 0 : 1;
    int u2 = (be && !(b >> bt & 1)) ? 0 : 1;
    for(int b1 = 0; b1 <= u1; b1++) {
        for(int b2 = 0; b2 <= u2; b2++) {
            int b3 = (b1 ^ b2);
            int u3 = (k >> bt & 1);
            if(!ke || b3 <= u3) {
                pmi nxt = solve(bt - 1, ae && b1 == u1, be && b2 == u2, ke && b3 == u3);
                dp[bt][ae][be][ke] += nxt.first;
                sdp[bt][ae][be][ke] += nxt.first * (1 << bt) * b3 + nxt.second;
            }

        }
    }
    return {dp[bt][ae][be][ke], sdp[bt][ae][be][ke]};
}

mi calc(int x, int y) {
    if(x < 0 || y < 0) return 0;
//    mi res = 0;
//    for(int i = 0; i <= x; i++) {
//        for(int j = 0; j <= y; j++) if((i ^ j) <= k) res += (i ^ j) + 1;
//    }
//    return res;
    a = x, b = y;
    memset(vis, 0, sizeof(vis));
    return solve(30, 1, 1, 1).second + solve(30, 1, 1, 1).first;
}

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

    int q;
    cin >> q;
    while(q--) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        x1--, y1--, x2--, y2--, k--;
        mi res = calc(x2, y2) - calc(x2, y1 - 1) - calc(x1 - 1, y2) + calc(x1 - 1, y1 - 1);
        cout << int(res) << '\n';
    }

}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define ll long long
const int mod = 1e9 + 7;
#define pll pair<ll, ll>
#define MP make_pair

int bitx[32], bity[32], bitk[32];
ll dp[32][2][2][2], dp1[32][2][2][2], f[32];
pll dfs(int pos, int limit1, int limit2, int limit3) {
  if(pos == 0) return MP(1, 0);
  if(dp[pos][limit1][limit2][limit3] != -1) return MP(dp[pos][limit1][limit2][limit3], dp1[pos][limit1][limit2][limit3]);
  int x = limit1 ? bitx[pos] : 1;
  int y = limit2 ? bity[pos] : 1;
  ll ret = 0;
  ll ret1 = 0;
  for(int i = 0; i <= x; ++i) {
    for(int j = 0; j <= y; ++j) {
      if(limit3 && (i ^ j) > bitk[pos]) continue;
      pll t = dfs(pos - 1, limit1 && (i == x), limit2 && (j == y), limit3 && ((i ^ j) == bitk[pos]));
      (ret += t.first) %= mod;
      (ret1 += t.second + ((i ^ j) ? f[pos - 1] : 0) * t.first % mod) %= mod;
    }
  }
  dp[pos][limit1][limit2][limit3] = ret, dp1[pos][limit1][limit2][limit3] = ret1;
  return MP(ret, ret1);
}
ll solve(ll x, ll y, ll k) {
  memset(dp, -1, sizeof(dp));
  if(x < 0 || y < 0) return 0;
  memset(bitx, 0, sizeof(bitx));
  memset(bity, 0, sizeof(bity));
  memset(bitk, 0, sizeof(bitk));
  int lenx = 0;
  while(x) {
    bitx[++lenx] = x & 1;
    x >>= 1;
  }
  int leny = 0;
  while(y) {
    bity[++leny] = y & 1;
    y >>= 1;
  }
  int lenk = 0;
  while(k) {
    bitk[++lenk] = k & 1;
    k >>= 1;
  }
  pll t = dfs(31, 1, 1, 1);
  return  (t.first + t.second) % mod;
}
int main() {
  f[0] = 1;
  for(int i = 1; i < 31; ++i) f[i] = f[i - 1] * 2 % mod;
  int T, x1, y1, x2, y2, k;
  scanf("%d", &T);
  memset(dp, -1, sizeof(dp));
  while(T--) {
    scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
    x1--;y1--;x2--;y2--;k--;
    printf("%I64d\n", ((solve(x2, y2, k) - solve(x1 - 1, y2, k) - solve(x2, y1 -1, k) + solve(x1 - 1, y1 - 1, k)) % mod + mod) % mod);
  }
  return 0;
}

#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <bitset>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <iostream>
#include <algorithm>
#define REP(i,a,b) for(int i=(a);i<=(b);i++)
#define PER(i,a,b) for(int i=(a);i>=(b);i--)
#define RVC(i,S) for(int i=0;i<(S).size();i++)
#define RAL(i,u) for(int i=fr[u];i!=-1;i=e[i].next)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

template<class T> inline
void read(T& num) {
    bool start=false,neg=false;
    char c;
    num=0;
    while((c=getchar())!=EOF) {
        if(c=='-') start=neg=true;
        else if(c>='0' && c<='9') {
            start=true;
            num=num*10+c-'0';
        } else if(start) break;
    }
    if(neg) num=-num;
}
/*============ Header Template ============*/

const int mo=(int)(1e9)+7;

int N,M,K;

inline void add(int& x,int v) {
    x+=v;if(x>=mo) x-=mo;
}

int vi[32][2][2][2];
int su[32][2][2][2];
int cnt[32][2][2][2];

void G(int c,int lN,int lM,int lK) {
    if(vi[c][lN][lM][lK]) return;
    vi[c][lN][lM][lK]=1;cnt[c][lN][lM][lK]=su[c][lN][lM][lK]=0;

    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++) {
            int tari=(N>>c)&1,tarj=(M>>c)&1,tark=(K>>c)&1;
            int nlN=lN,nlM=lM,nlK=lK;
            if(i>tari && nlN) continue;
            if(j>tarj && nlM) continue;
            if((i^j)>tark && nlK) continue;
            //printf("vis %d %d %d %d %d %d\n",c,lN,lM,lK,i,j);
            if(i<tari) nlN=0;if(j<tarj) nlM=0;if((i^j)<tark) nlK=0;
            int C=1,S=0;
            if(c) {
                G(c-1,nlN,nlM,nlK);
                C=cnt[c-1][nlN][nlM][nlK];
                S=su[c-1][nlN][nlM][nlK];
            }
            add(cnt[c][lN][lM][lK],C);
            add(su[c][lN][lM][lK],(S+1LL*(i^j)*(1<<c)*C)%mo);
        }
    //printf("%d %d %d %d : cnt = %d, su = %d\n",c,lN,lM,lK,cnt[c][lN][lM][lK],su[c][lN][lM][lK]);
}

inline int calc(int n,int m) {
    if(!n || !m) return 0;
    N=n;M=m;N--;M--;
    memset(vi,0,sizeof(vi));
    G(31,1,1,1);int res=0;
    add(res,su[31][1][1][1]);
    add(res,cnt[31][1][1][1]);
    return res;
}

void solve() {
    int lX,lY,rX,rY;
    read(lX);read(lY);
    read(rX);read(rY);
    read(K);K--;
    int res=0;
    add(res,calc(rX,rY));
    add(res,mo-calc(rX,lY-1));
    add(res,mo-calc(lX-1,rY));
    add(res,calc(lX-1,lY-1));
    printf("%d\n",res);
}

int main() {
    int T;
    read(T);
    while(T--) solve();
    return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
using namespace std;

#define sz(x) (int)(x.size())
#define rep(i,a,b) for(int i=a;i<b;++i)
#define pb push_back
////////////////

int const MOD = 1e9 + 7;
int DIV_TWO;

int x11, y11, x22, y22, k;

int sum(int a, int b){
	return (a + b) % MOD;
}

void add(int &a, int b){
	a += b;
	if(a >= MOD) a -= MOD;
	if(a < 0) a += MOD;
}

int mul(int a, int b){
	return a * 1LL * b % MOD;
}

int bp(int x, int d){
	if(!d) return 1;
	if(d&1) return mul(x, bp(x,d-1));
	int r = bp(x,d/2);
	return mul(r, r);
}

int asum(int n, int cnt, int add){
	int f = sum(add, 1);
	int l = sum(add, n);
	int ret = mul(n, sum(f, l));
	ret = mul(ret, DIV_TWO);
	ret = mul(ret, cnt);
	return ret;
}

int max(int a, int b, int c, int d){
	int max1 = max(a, b);
	int max2 = max(c, d);
	return max(max1, max2);
}

int rec(int s, int w, int h, int k, int addv = 0){
	if(k <= 0) return 0;
	if(w <= 0 || h <= 0) return 0;
	if(w == s){
		return asum(min(k, s), h, addv);
	}
	if(h == s){
		return asum(min(k, s), w, addv);
	}
	int ret = 0;
	int ns = s / 2;
	add(ret, rec(ns, min(ns, w), min(ns, h), k, addv));
	add(ret, rec(ns, w - ns, min(ns, h), k - ns, addv + ns));
	add(ret, rec(ns, min(ns, w), h - ns, k - ns, addv + ns));
	add(ret, rec(ns, w - ns, h - ns, k, addv));

	return ret;
}

int const N = 30;
int ans[N][N];

int get(int col, int row){
	vector<int> v;
	v.pb(0);
	rep(i, 1, row) v.pb(ans[i][col]);
	rep(i, 1, col) v.pb(ans[row][i]);

	sort(v.begin(), v.end());
	rep(i, 1, sz(v)) if(v[i] - v[i-1] != 1) return v[i-1] + 1;
	return v.back() + 1;
}

void brute(){
	rep(i, 1, N){
		rep(j, 1, N){
			int mini = get(j, i);
			ans[i][j] = mini;
		}
	}
}

int getBrute(int s, int w, int h, int k){
	int ret = 0;
	rep(y, 1, h+1) rep(x, 1, w+1) if(ans[y][x] <= k) ret += ans[y][x];
	return ret;
}

int solve(){

	cin >> x11 >> y11 >> x22 >> y22 >> k;
	int ans = 0;
	int maxi = max(x11, y11, x22, y22);
	int s = 1;
	while(s < maxi){
		s *= 2;
	}

	add(ans, rec(s, s, s, k));
	//assert(getBrute(s, s, s, k) == rec(s, s, s, k));

	add(ans, -rec(s, s, y11-1, k));
	//assert(getBrute(s, s, y11-1, k) == rec(s, s, y11-1, k));

	add(ans, -rec(s, x11-1, s, k));
	//assert(getBrute(s, x11-1, s, k) == rec(s, x11-1, s, k));
	add(ans, -rec(s, s-x22, s, k));
	//assert(getBrute(s, s-x22, s, k) == rec(s, s-x22, s, k));
	add(ans, -rec(s, s, s-y22, k));
	//assert(getBrute(s, s, s-y22, k) == rec(s, s, s-y22, k));

	add(ans, rec(s, x11-1, y11-1, k));
	//assert(getBrute(s, x11-1, y11-1, k) == rec(s, x11-1, y11-1, k));

	add(ans, rec(s, s, y11-1, k));
	//assert(getBrute(s, s, y11-1, k) == rec(s, s, y11-1, k));
	add(ans, -rec(s, x22, y11-1, k));
	//assert(getBrute(s, x22, y11-1, k) == rec(s, x22, y11-1, k));

	add(ans, rec(s, x11-1,  s, k));
	//assert(getBrute(s, x11-1,  s, k) == rec(s, x11-1,  s, k));
	add(ans, -rec(s, x11-1, y22, k));
	//assert(getBrute(s, x11-1, y22, k) == rec(s, x11-1, y22, k));

	add(ans, rec(s, s, s, k));
	//assert(getBrute(s, s, s, k) == rec(s, s, s, k));
	add(ans, -rec(s, x22, s, k));
	//assert(getBrute(s, x22, s, k) == rec(s, x22, s, k));
	add(ans, -rec(s, s, y22, k));
	//assert(getBrute(s, s, y22, k) == rec(s, s, y22, k));
	add(ans, rec(s, x22, y22, k));
	//assert(getBrute(s, x22, y22, k) == rec(s, x22, y22, k));

	return ans;
}

int main(){
#ifdef _DEBUG
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
	DIV_TWO = bp(2, MOD - 2);
	//brute();
	int test;

	cin >> test;
	while(test--){
		cout << solve() << endl;
	}

	return 0;
}

#include <bits/stdc++.h>

#define MOD 1000000007

using namespace std;

typedef long long INT;

typedef pair <int, int> pii;

INT calc_cnt(INT x, INT y, INT k){

	if(!x || !y || !k) return 0;

	if(x>y) swap(x, y);

	int b=63-__builtin_clzll(x);

	int c=63-__builtin_clzll(y);

	INT z=1LL<<c;

	if(b==c){

		if(z<=k) return (z*z%MOD+calc_cnt(x-z, y-z, k)+calc_cnt(z, y-z, k-z)+calc_cnt(x-z, z, k-z))%MOD;

		return (z%MOD*k%MOD+calc_cnt(x-z, y-z, k))%MOD;

	}

	if(z<=k) return (x*z%MOD+calc_cnt(x, y-z, k-z))%MOD;

	return x*k%MOD;

}

INT calc(INT x, INT y, INT k){

	if(!x || !y || !k) return 0;

	if(x>y) swap(x, y);

	int b=63-__builtin_clzll(x);

	int c=63-__builtin_clzll(y);

	INT z=1LL<<c;

	if(b==c){

		if(z<=k) return (z*(z*(z+1)/2%MOD)%MOD+calc(x-z, y-z, k)+calc(z, y-z, k-z)+calc_cnt(z, y-z, k-z)*z%MOD+calc(x-z, z, k-z)+calc_cnt(x-z, z, k-z)*z%MOD)%MOD;

		return (z%MOD*(k*(k+1)/2%MOD)%MOD+calc(x-z, y-z, k))%MOD;

	}

	if(z<=k) return (x*(z*(z+1)/2%MOD)%MOD+calc(x, y-z, k-z)+calc_cnt(x, y-z, k-z)*z%MOD)%MOD;

	return x*(k*(k+1)/2%MOD)%MOD;

}

int main (){

	int q;

	INT x1, x2, y1, y2, k;

	cin >> q;

	while(q--){

		cin >> x1 >> y1 >> x2 >> y2 >> k;

		x1--, y1--;

		cout << (calc(x2, y2, k)+calc(x1, y1, k)-calc(x1, y2, k)-calc(x2, y1, k)+MOD+MOD)%MOD << endl;

	}

	return 0;

}

#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef pair <int, int> ii;

struct pos {
	int r, c, val;
	pos(int r = 0, int c = 0, int val = 0): r(r), c(c), val(val) {}
	bool operator <(const pos &u) const {
		if (r != u.r) return r < u.r;
		if (c != u.c) return c < u.c;
		return val < u.val;
	}
};

const int mod = 1000000007;
const int Maxd = 31;

int sum[Maxd];
map <pos, ii> M;

ii Get(pos p)
{
	if (p.r <= 0 || p.c <= 0 || p.val <= 0) return ii(0, 0);
	int sz = 0;
	while (1 << sz < p.r || 1 << sz < p.c) sz++;
	if (1 << sz <= p.val) p.val = 1 << sz;
	map <pos, ii>::iterator it = M.find(p);
	if (it == M.end()) {
		int sz = 0;
		while (1 << sz < p.r || 1 << sz < p.c) sz++;
		ii res = ii(0, 0);
		if (1 << sz <= p.val) p.val = 1 << sz;
		if ((1 << sz == p.r) && (1 << sz == p.c) && (1 << sz) <= p.val) res = ii(sum[sz], ll(1 << sz) * ll(1 << sz) % mod);
		else {
			sz--;
			int r1 = min(p.r, 1 << sz), c1 = min(p.c, 1 << sz);
			ii add = Get(pos(r1, c1, p.val));
			res.first = (res.first + add.first) % mod;
			res.second = (res.second + add.second) % mod;

			r1 = p.r - (1 << sz);
			add = Get(pos(r1, c1, p.val - (1 << sz)));
			res.first = (res.first + ll(add.first) + ll(add.second) * ll(1 << sz)) % mod;
			res.second = (res.second + add.second) % mod;

			r1 = min(p.r, 1 << sz), c1 = p.c - (1 << sz);
			add = Get(pos(r1, c1, p.val - (1 << sz)));
			res.first = (res.first + ll(add.first) + ll(add.second) * ll(1 << sz)) % mod;
			res.second = (res.second + add.second) % mod;

			r1 = p.r - (1 << sz);
			add = Get(pos(r1, c1, p.val));
			res.first = (res.first + add.first) % mod;
			res.second = (res.second + add.second) % mod;
		}
		M[p] = res;
		return res;
	}
	return it->second;
}

int main()
{
	sum[0] = 1;
	for (int i = 1; i < Maxd; i++)
		sum[i] = (4ll * ll(sum[i - 1]) + 2ll * ll(1 << i - 1) * ll(1 << i - 1) % mod * ll(1 << i - 1) % mod) % mod;
	int q; scanf("%d", &q);
	while (q--) {
		int X1, Y1, X2, Y2, k; scanf("%d %d %d %d %d", &X1, &Y1, &X2, &Y2, &k);
		int res = (Get(pos(X2, Y2, k)).first - Get(pos(X1 - 1, Y2, k)).first + mod) % mod;
		res = (res - Get(pos(X2, Y1 - 1, k)).first + mod) % mod;
		res = (res + Get(pos(X1 - 1, Y1 - 1, k)).first) % mod;
		printf("%d\n", res);
	}
	return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod = 1e9+7;
inline int read(){
	int sum = 0, t = 1; char ch = getchar();
	while(ch < '0' || ch > '9'){ if(ch == '-') t = -1; ch = getchar(); }
	while(ch >= '0' && ch <= '9'){ sum = sum * 10 + ch - '0'; ch = getchar(); }
	return sum * t;
}

int f[32][2][2][2], g[32][2][2][2], q;

void add(int &x, int y){ x += y; if(x >= mod) x -= mod; }

int solve(int n, int m, int k){
	if(n < 0 || m < 0 || k < 0) return 0;
	memset(f, 0, sizeof(f));
	memset(g, 0, sizeof(g));
	f[31][1][1][1] = 1;
	for(int i = 30; ~i ; i--)
		for(int a = 0; a < 2; a++)
			for(int b = 0; b < 2; b++)
				for(int c = 0; c < 2; c++) if(f[i + 1][a][b][c] || g[i + 1][a][b][c])
					for(int x = 0; x < 2; x++)
					for(int y = 0; y < 2; y++){
						int z = x ^ y;
						int ni = n >> i & 1, mi = m >> i & 1, ki = k >> i & 1;
						if(a && !ni && x) continue ;
						if(b && !mi && y) continue ;
						if(c && !ki && z) continue ;
						add(f[i][a & (ni == x)][b & (mi == y)][c & (ki == z)], f[i + 1][a][b][c]);
						add(g[i][a & (ni == x)][b & (mi == y)][c & (ki == z)], g[i + 1][a][b][c]);
						add(g[i][a & (ni == x)][b & (mi == y)][c & (ki == z)], 1LL * (z << i) * f[i + 1][a][b][c] % mod);
					}
	int res = 0;
	for(int a = 0; a < 2; a++)
		for(int b = 0; b < 2; b++)
			for(int c = 0; c < 2; c++)
				add(res, f[0][a][b][c]), add(res, g[0][a][b][c]);
	return res;
}

int main(){
	q = read();
	for(; q ; q--){
		int x1 = read() - 1, y1 = read() - 1, x2 = read() - 1, y2 = read() - 1, k = read() - 1;
		int ans = solve(x2, y2, k);
		add(ans, mod - solve(x2, y1 - 1, k));
		add(ans, mod - solve(x1 - 1, y2, k));
		add(ans, solve(x1 - 1, y1 - 1, k));
		printf("%d\n", ans);
	}
}

#include <cstdio>
#include <algorithm>
#include <string.h>
#include <assert.h>
#include <list>
#include <map>
#include <vector>
#include <iostream>
using namespace std;

#define P 1000000007

int sum(int l, int r) {
	return 1LL * (0LL + l + r) * (r - l + 1) / 2 % P;
}

int solve(int x, int y, int k) {
	if (!x || !y)
		return 0;
	k --;
	int ans = 0;
	// int ans = 1LL * x * y % P;
	// printf("?? %d\n", ans);
	vector <pair<int, int> > X, Y;
	// for (int i = )
	for (int i = 0; i <= 30; i++)
		if (x & (1 << i)) {
			x -= (1 << i);
			X.push_back(make_pair(x, i));
		}
	for (int i = 0; i <= 30; i++)
		if (y & (1 << i)) {
			y -= (1 << i);
			Y.push_back(make_pair(y, i));
		}
	for (int i = 0; i < (int) X.size(); i++)
		for (int j = 0; j < (int) Y.size(); j++) {
			pair<int, int> xx = X[i], yy = Y[j];
			if (xx.second > yy.second)
				swap(xx, yy);

			int head = ((xx.first ^ yy.first) >> yy.second) << yy.second;
			// printf("?? %d %d\n", xx.first, yy.first);
			// printf("?? %d %d %d\n", xx.first, yy.first, head);
			if (head < ((k >> yy.second) << yy.second)) {
				ans = (ans + 1LL * head * (1 << yy.second) % P * (1 << xx.second)) % P;
				ans = (ans + 1LL * sum(0, (1 << yy.second) - 1) * (1 << xx.second)) % P;
				ans = (ans + 1LL * (1 << yy.second) * (1 << xx.second)) % P;
			}else if (head == ((k >> yy.second) << yy.second)) {
				ans = (ans + 1LL * head * (k - ((k >> yy.second) << yy.second) + 1) % P * (1 << xx.second)) % P;
				ans = (ans + 1LL * sum(0, k - ((k >> yy.second) << yy.second)) * (1 << xx.second)) % P;
				ans = (ans + 1LL * (k - ((k >> yy.second) << yy.second) + 1) * (1 << xx.second)) % P;
			}
		}
	// printf("?? %d\n", ans);
	return ans;
}

int main() {
	int q, x11, y11, x22, y22, k;
	scanf("%d", &q);
	while (q--) {
		scanf("%d%d%d%d%d", &x11, &y11, &x22, &y22, &k);
		long long ans = solve(x22, y22, k) - solve(x11 - 1, y22, k) - solve(x22, y11 - 1, k) + solve(x11 - 1, y11 - 1, k);
		ans %= P;
		ans = (ans + P) % P;
		printf("%d\n", (int) ans);
	}
}

#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <utility>
#include <string>
#include <algorithm>
#include <time.h>
#include <assert.h>

using namespace::std;

int64_t mod = 1000000007;

int ri()
{
	int x;
	scanf("%d", &x);

	return x;
}

void wi(int x)
{
	printf("%d\n", x);
}

void get_partition(int a, int b, int s, int e, int w, vector<pair<int, int> > &res)
{
	//cout << "xxx: " << s << "  " << e << "  " << w << endl;
	if (a > e || b < s)
		return;

	if (s >= a && e <= b)
	{
		res.push_back(make_pair(s, w));
		return;
	}

	int mid = (s + e)/2;
	get_partition(a, b, s, mid, w - 1, res);
	get_partition(a, b, mid + 1, e, w - 1, res);
}

void get_partition(int a, int b, vector<pair<int, int> > &res)
{
	get_partition(a, b, 0, (1 << 30) - 1, 30, res);
}

int main()
{
	/*
	int N = 40;
	vector<vector<int> > A(1 + N, vector<int>(1 + N, 0));

	for (int i = 0; i <= N; i++)
	{
		for (int j = 0; j <= N; j++)
		{
			if (i == 0 && j == 0)
				A[i][j] = 0;
			else
			{
				set<int> S;
				for (int k = 0; k < j; k++)
					S.insert(A[i][k]);

				for (int k = 0; k < i; k++)
					S.insert(A[k][j]);

				for (int k = 0; ;k++)
				{
					if (S.find(k) == S.end())
					{
						A[i][j] = k;
						break;
					}
				}
			}

			if (A[i][j] != (i ^ j))
			{
				cout << "bd" << endl;

			}

			cout << A[i][j] << " ";
		}
		cout << endl;
	}
	*/

	int Q = ri();

	for (int q = 0; q < Q; q++)
	{

	int x1 = ri();
	int y1 = ri();
	int x2 = ri();
	int y2 = ri();
	int k = ri();

	x1--;
	y1--;

	x2--;
	y2--;

	k--;

	vector<pair<int, int> > A;
	vector<pair<int, int> >	B;

	get_partition(x1, x2, A);
	get_partition(y1, y2, B);

	//cout << "hey " << A.size() << "  " << B.size() << endl;

	uint64_t ans = 0;
	for (int i = 0; i < A.size(); i++)
	{
		for (int j = 0; j < B.size(); j++)
		{
			int w1 = A[i].second;
			int w2 = B[j].second;

			// The max(w1, w2) LSB's can be turned to anything
			// in exactly 2^min(w1, w2) ways

			int u = max(w1, w2);
			int v = min(w1, w2);

			int R = (A[i].first >> u) ^ (B[j].first >> u);
			R <<= u;

			if (R > k)
				continue;

			// number of possible values in the range
			uint64_t num = min(k - R + 1, 1 << u);

			// [R, R + num - 1]
			// [R + 1, R + num]

			uint64_t a = (uint64_t) R * num + (num * (num + 1))/2;
			a %= mod;

			a <<= v;
			a %= mod;

			ans += a;
			if (ans >= mod)
				ans -= mod;
		}
	}

	wi((int) ans);
	}

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define mo 1000000007
int K;
int Solve(int x,int y,int Bas){
	if (Bas>K){
		return 0;
	}
	if (x<y){
		swap(x,y);
	}
	if (x==(x&-x)){
		int l=Bas,r=min(K,Bas+x-1);
		return 1LL*(l+r)*(r-l+1)/2%mo*y%mo;
	}
	int t=(1<<(int)log2(x-1));
	int res=0;
	if (y<=t){
		res=(res+Solve(t,y,Bas))%mo;
		res=(res+Solve(x-t,y,Bas+t))%mo;
	}else{
		res=(res+Solve(t,t,Bas))%mo;
		res=(res+Solve(x-t,t,Bas+t))%mo;
		res=(res+Solve(t,y-t,Bas+t))%mo;
		res=(res+Solve(x-t,y-t,Bas))%mo;
	}
	return res;
}
int main(){
	int q;
	scanf("%d",&q);
	while (q--){
		int x1,y1,x2,y2;
		scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&K);
		int Ans=0;
		Ans=(Ans+Solve(x2,y2,1))%mo;
		Ans=(Ans-Solve(x1-1,y2,1)+mo)%mo;
		Ans=(Ans-Solve(x2,y1-1,1)+mo)%mo;
		Ans=(Ans+Solve(x1-1,y1-1,1))%mo;
		printf("%d\n",Ans);
	}
	return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
using namespace std;
typedef long long LL;
int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}
const int mod=1e9+7;
int n,m,lim;
int f[31][2][2][2],g[31][2][2][2];
int cnt;
inline pair<int,int> dfs(int k,bool on,bool om,bool olim) {
	if (k==-1) return make_pair(0,1);
	if (f[k][on][om][olim]!=-1)
		return make_pair(f[k][on][om][olim],g[k][on][om][olim]);
	int sum=0,num=0,i,j;pair<int,int>p;
	for (i=on?n>>k&1:1;i>=0;i--)
		for (j=om?m>>k&1:1;j>=0;j--) {
			if (olim&&(i^j)&&!(lim>>k&1)) continue;
			p=dfs(k-1,on&&(i==(n>>k&1)),om&&(j==(m>>k&1)),olim&&((i^j)==(lim>>k&1)));
			if (i^j)
				sum=(sum+((LL)p.second<<k))%mod;
			(sum+=p.first)%=mod;
			(num+=p.second)%=mod;
		}
	return make_pair(f[k][on][om][olim]=sum,g[k][on][om][olim]=num);
}
inline int dp(int _n,int _m) {
	if (_n==-1||_m==-1) return 0;
	memset(f,-1,sizeof(f));
	n=_n;m=_m;
	pair<int,int>p=dfs(30,true,true,true);
	return (p.first+p.second)%mod;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("CF809C.in","r",stdin);
	freopen("CF809C.out","w",stdout);
#endif
	int Q=gi(),x1,x2,y1,y2,ans;
	while (Q--) {
		x1=gi()-2,y1=gi()-2,x2=gi()-1,y2=gi()-1;lim=gi()-1;
		ans=dp(x2,y2);
		(ans-=dp(x1,y2))%=mod;
		(ans-=dp(x2,y1))%=mod;
		(ans+=dp(x1,y1))%=mod;
		printf("%d\n",(ans+mod)%mod);
	}
	return 0;
}

#include "vector"
#include "string"
#include "set"
#include "map"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"
#include "numeric"
#include "fstream"
#include "queue"
#include <functional>
#include <climits>
#include <cstring>
#include <ctime>
#include <list>
#include <iomanip>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <limits>
#include <complex>

#define int64 long long

#include <iostream>

using namespace std;

int64 M = 1000000007;

int64 S(int64 n, int64 k) {
    int64 mi = min(n, k);
    return (mi * (mi + 1) / 2) % M;
}

/*
 1
 3 2 5 4 5
 */

int64 Q2(int64 y, int64 x, int64 k, int64 delta) {
    if (k - delta <= 0) return 0;
    if (x > y) swap(x, y);
    if (x == 0 || y == 0) return  0;
    assert(y >= x);
    assert(x >= 0);
    assert(y >= 0);
    int64 p = 1;
    while (p * 2 <= y) p *= 2;
    int64 ret = min(p, x) * ((S(p + delta, k) - S(delta, k) + M) % M) % M;
    int64 newX = x - min(p, x);
    int64 newY = y - p;
    ret = (ret + Q2(newY, x - newX, k, delta + p) + Q2(y - newY, newX, k, delta + p) + Q2(newY, newX, k, delta)) % M;
    return ret;
}

int64 Q1(int64 y1, int64 x1, int64 y2, int64 x2, int64 k) {
    int64 ret = (Q2(y2, x2, k, 0) + Q2(y1 - 1, x1 - 1, k, 0)) % M;
    int64 ret2 = (Q2(y1 - 1, x2, k, 0) + Q2(y2, x1 - 1, k, 0)) % M;
    return (ret - ret2 + M) % M;
}

int main() {
    std::ios::sync_with_stdio(false);
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int64 y1, x1, y2, x2, k;
        cin >> y1 >> x1 >> y2 >> x2 >> k;
        cout << Q1(y1, x1, y2, x2, k) << endl;
    }
    return 0;
}

///FUCKOUT
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")

#include "bits/stdc++.h"
using namespace std;

#define pb push_back
#define F first
#define S second
#define f(i,a,b)  for(int i = a; i < b; i++)
//#define endl '\n'

using ll = long long;
using db = long double;
using row = vector<int>;
using ii = pair<ll, ll>;

const int N = 2e5+5, M = 1e6 + 5, LG = 17, MOD = 1e9 + 7;
const int BLOCK = 55;
const int BLOCKN = N / BLOCK + 1;
const long double EPS = 1e-7;
using ull = unsigned long long;
int q;
int x1, y1, x2, y2, k;
ll dp[1<<5][1<<5];
int sumMemo[1<<5][1<<5];
pair<int, ll> solve(int bit, int mask){
  if(bit == 31){
    if(mask == 31)
      return make_pair(0,1);
    return make_pair(0,0);
  }
  ll &ret = dp[bit][mask];
  int &sum = sumMemo[bit][mask];
  if(~ret)
    return make_pair(sum, ret);
  ret=sum = 0;
  f(b1,0,2)
    f(b2,0,2){
      int nmask = 0;
      ///x
      int val = (x1 >> bit & 1);
      if(b1 > val)nmask ^= 1;
      else if(b1 == val)nmask ^= (mask & 1);
      val = (x2 >> bit & 1);
      if(b2 > val)nmask ^= 2;
      else if(b2 == val)nmask ^= (mask & 2);
      ///y
      val = (y1 >> bit & 1);
      if(b1 < val)nmask ^= 4;
      else if(b1 == val)nmask ^= (mask & 4);
      val = (y2 >> bit & 1);
      if(b2 < val)nmask ^= 8;
      else if(b2 == val)nmask ^= (mask & 8);
      ///k
      val = (k >> bit & 1);
      if((b1^b2) < val)nmask ^= 16;
      else if((b1^b2) == val)nmask ^= (mask & 16);
      auto res = solve(bit+1,nmask);
      ret += res.S;
      sum = (sum + 1ll * res.S * ((b1^b2) << (bit)) + res.F) % MOD;
    }
  return make_pair(sum, ret);
}
int32_t main(){
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif // ONLINE_JUDGE

  cin >> q;

  while(q--){

    memset(dp,-1,sizeof dp);
    cin >> x1 >> x2 >> y1 >> y2 >> k; --x1,--x2;
    /// >= x1, >= x2, < y1, < y2, < k
//    cerr<<solve(0,3).S<<endl;
    cout << (solve(0,3).F+solve(0,3).S)%MOD << '\n';

  }

  return 0;
}
//hellobig_tq

#include <bits/stdc++.h>

using namespace std;
using namespace placeholders;

template <class T> void mini(T &l, T r) {l = min(l, r);}
template <class T> void maxi(T &l, T r) {l = max(l, r);}

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class ...TA> void _dbg(const char *sdbg, TH h, TA ...a) {
  while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, a...);
}
template <class T> ostream &operator<<(ostream &os, vector <T> v) {
  os << "[";
  for (auto vv : v) os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair <L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}

#define eput(...) //_dbg(#__VA_ARGS__, __VA_ARGS__)

using llint = long long;

const int B = 1e9 + 7;

int a[40][40];

void add(llint &a, llint b) {
  a = (a + b) % B;
  if (a < 0) a += B;
}

llint calc(llint a, llint b) {
  a %= B, b %= B;
  return ((a + b) * (b - a + 1) / 2) % B;
}

pair <llint, llint> get(int x, int y, int k, int d) {
  llint ret = 0;
  int sz = 1 << d;
  if (x < y) swap(x, y);
  if (x <= 0 || y <= 0 || k <= 0) return make_pair(0, 0);
  if (x >= sz) {
    //y * (1..t1)
    llint a = min(k, sz);
    llint b = min(y, sz);
    ret = calc(1, a) * b % B;
    eput(x, y, sz, k, d, ret, (b * a));
    return make_pair(b * calc(1, a) % B, b * a % B);
  }
  if (d == 0) return make_pair(1 <= k, 1 <= k);

  int t = 1 << (d - 1);
  auto t1 = get(x, y, k, d - 1);
  auto t2 = get(x - t, y, k - t, d - 1);
  auto t3 = get(y, y - t, k - t, d - 1);
  auto t4 = get(x - t, y - t, k, d - 1);
  eput(t1, t2, t3, t4);
  add(ret, t1.first);
  add(ret, t2.first + t2.second * t % B);
  add(ret, t3.first + t3.second * t % B);
  add(ret, t4.first);
  llint c = 0;
  add(c, t1.second);
  add(c, t2.second);
  add(c, t3.second);
  add(c, t4.second);
  eput(x, y, sz, k, d, ret, c);
  return make_pair(ret, c);
}

llint go(int x1, int y1, int x2, int y2, int k) {
  llint ret = 0;
  add(ret, get(x2, y2, k, 30).first);
  add(ret, -get(x1 - 1, y2, k, 30).first);
  add(ret, -get(x2, y1 - 1, k, 30).first);
  add(ret, get(x1 - 1, y1 - 1, k, 30).first);
  return ret;
}

void run() {
  eput(get(9,6,16,5));
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    int x1, y1, x2, y2, k;
    scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
    printf("%lld\n", go(x1, y1, x2, y2, k));
  }
}

int main() {
  run();
  return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;

int read()
{
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

pii dp[40][2][2][2];
int a[N], c[N], b[N];
pii dfs(int pos, int u, int v, int f)
{

    if (pos == -1)
        return mk(1, 0);
    if (dp[pos][u][v][f].first != -1)
        return dp[pos][u][v][f];
    int la = u ? a[pos] : 1;
    int lb = v ? b[pos] : 1;
    pii res = mk(0, 0);
    for (int i = 0; i <= la; i++)
    {
        for (int j = 0; j <= lb; j++)

        {

            if (f == 1 && (i ^ j) > c[pos])
                continue;

            pii now = dfs(pos - 1, u && i == la, v && j == lb, ((i ^ j) == c[pos]) && f);
            res.first = (res.first + now.first) % mod;

            if (i ^ j)
            {

                res.second = (res.second + 1ll * now.first * (1 << pos) % mod) % mod;
            }
            res.second = (res.second + now.second) % mod;
        }
    }
    return dp[pos][u][v][f] = res;
}

int solve(int x, int y, int k)
{
    if (x < 0 || y < 0)
        return 0;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < 32; i++)
    {
        a[i] = b[i] = c[i] = 0;
        if ((1 << i) & x)
            a[i] = 1;
        if ((1 << i) & y)
            b[i] = 1;
        if ((1 << i) & k)
            c[i] = 1;
    }
    pii ans = dfs(32, 1, 1, 1);
    return (ans.first + ans.second)%mod;
}

int main()
{
    int q = read();

    for (int i = 1; i <= q; i++)
    {
        int x1 = read(), y1 = read(), x2 = read(), y2 = read(), k = read();
        x1--, y2--;
        x2--, y1--;
        k--;
        int ans = (((solve(x2, y2, k) - solve(x2, y1 - 1, k) + mod) % mod - solve(x1 - 1, y2, k) + mod) % mod + solve(x1 - 1, y1 - 1, k)) % mod;
        cout << (ans + mod) % mod << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef complex<ld> cd;

const ll mod = 1000000007;

ll large(ll x){
    for(int i = 30; i >= 0; i--){
        if((x>>i)&1)return (1LL<<i);
    }
}

ll comp(ll x, ll k){
    ll ret = k*(k+1)/2;
    ret %= mod;
    ret *= x;
    return ret%mod;
}

ll func(ll x, ll y, ll k, ll off = 0){
    if(!x || !y)return 0;
    if(x > y)return func(y, x, k, off);
    ll m = large(y);
    if(k <= m){
        ll ret = (off*min(x, m)%mod)*k%mod;
        ret += comp(min(x, m), k);
        if(x > m){
            ret += func(x - m, y - m, k, off);
        }
        ret %= mod;
//         cout<<x<<" "<<y<<" "<<k<<" "<<off<<" "<<m<<" ";
//         cout<<ret<<" "<<1<<endl;
        return ret;
    }
    if(x <= m){
        ll ret = func(x, y - m, k - m, off^m);
        ret += comp(x, m);
        ret += (off*x%mod)*m%mod;
        ret %= mod;
//         cout<<x<<" "<<y<<" "<<k<<" "<<off<<" "<<m<<" ";
//         cout<<ret<<endl;
        return ret;
    }
    ll ret;
    if(off&m)ret = func(m, y - m, k + m, off - m);
    else ret = func(m, y - m, k - m, off + m);

    ret += func(x - m, y - m, k, off);

    ret += comp(m, m);
    ret += (off*m%mod)*m%mod;

    ll off2 = off^m;
    ret += comp(x-m, min(m, k-m));
    ret += (off2*(x-m)%mod)*min(m, k-m)%mod;
    ret %= mod;

//     cout<<x<<" "<<y<<" "<<k<<" "<<off<<" "<<m<<" "<<off2<<" "<<comp(x-m,min(m,k-m))<<" "<<(off2*(x-m)%mod)*min(m, k-m)%mod<<" ";
//     cout<<ret<<endl;
    return ret;
}

inline ll query(ll x1, ll y1, ll x2, ll y2, ll k){
    ll ret = func(x2, y2, k) - func(x1 - 1, y2, k) - func(x2, y1 - 1, k) + func(x1 - 1, y1 - 1, k) + mod + mod;
    return ret%mod;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cout<<setprecision(30);

//     for(int i = 1; i <= 20; i++){
//         for(int j = 1; j <= 20; j++){
//             cout<<query(i, j, i, j, 1000)<<" ";
//         }
//         cout<<endl;
//     }

    int q;
    ll x1, x2, y1, y2, k;
    cin>>q;
    while(q--){
        cin>>x1>>y1>>x2>>y2>>k;
        cout<<query(x1, y1, x2, y2, k)<<endl;
    }

}

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <utility>
#include <cstdlib>
#include <memory>
#include <queue>
#include <cassert>
#include <cmath>
#include <ctime>
#include <complex>
#include <bitset>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <numeric>

using namespace std;

#define ws ws_____________________
#define y1 y1_____________________
#define y0 y0_____________________
#define left left_________________
#define right right_______________
#define next next_________________
#define prev prev_________________
#define hash hash_________________

#define pb push_back
#define fst first
#define snd second
#define mp make_pair
#define sz(C) ((int) (C).size())
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define ford(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define all(C) begin(C), end(C)

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef long double ld;
typedef complex<double> cd;

#define FILE_NAME "a"

#define TEST 0

const int MOD = 1e9 + 7;

void add(int& x, int y) {
	((x += y) >= MOD) && (x -= MOD);
}

int mul(int x, int y) {
	return x * 1ll * y % MOD;
}

int q;

bool read() {
#if !TEST
	if  (scanf("%d", &q) < 1) {
		return 0;
	}
#else
	q = 1;
#endif

	return 1;
}

const int LEN = 32;

vi repr(int num) {
	vi res;
	forn(it, LEN) {
		res.pb(num % 2);
		num /= 2;
	}
	reverse(all(res));
	return res;
}

int brut_solve(int x1, int y1, int x2, int y2, int k) {
	int ans = 0;
	for (int x = x1; x <= x2; ++x) {
		for (int y = y1; y <= y2; ++y) {
			int val = ((x - 1) ^ (y - 1)) + 1;
			if  (val <= k) {
				add(ans, val);
			}
		}
	}
	return ans;
}

int brut_ask(int x, int y, int lim) {
	--x;
	--y;

	if (x < 0 || y < 0) {
		return 0;
	}

	int res = 0;
	forn(i, x + 1) forn(j, y + 1) {
		int val = i ^ j;
		if  (val <= lim) {
			add(res, val + 1);
		}
	}
	return res;
}

int ask(int x, int y, int lim) {
	--x;
	--y;
	--lim;

	if (x < 0 || y < 0) {
		return 0;
	}

	vector<vvi> sum(2, vvi(2, vi(2, 0)));
	vector<vvi> cnt(2, vvi(2, vi(2, 0)));
	cnt[0][0][0] = 1;

	auto X = repr(x);
	auto Y = repr(y);
	auto LIM = repr(lim);

	forn(pos, LEN) {
		vector<vvi> new_sum(2, vvi(2, vi(2, 0)));
		vector<vvi> new_cnt(2, vvi(2, vi(2, 0)));

		forn(lx, 2) forn(ly, 2) forn(lk, 2) {

#define OLD [lx][ly][lk]

			if  (!cnt OLD && !sum OLD) {
				continue;
			}

			// if  (cnt OLD) {
			// 	printf("cnt %d %d %d -> %d\n", lx, ly, lk, cnt OLD);
			// }

			forn(dx, 2) forn(dy, 2) {
				int dk = dx ^ dy;

				if  (!lx && dx > X[pos]) {
					continue;
				}
				if  (!ly && dy > Y[pos]) {
					continue;
				}
				if  (!lk && dk > LIM[pos]) {
					continue;
				}

				int new_lx = lx || dx < X[pos];
				int new_ly = ly || dy < Y[pos];
				int new_lk = lk || dk < LIM[pos];

#define NEW [new_lx][new_ly][new_lk]

				add(new_cnt NEW, cnt OLD);

				int nsum = sum OLD;
				int pw2 = 1 << (LEN - 1 - pos);
				add(nsum, mul((cnt OLD), mul(dk, pw2)));

				add(new_sum NEW, nsum);
#undef NEW
			}

#undef OLD

		}

		sum.swap(new_sum);
		cnt.swap(new_cnt);
	}

	int ans = 0;
	forn(lx, 2) forn(ly, 2) forn(lk, 2) {
		add(ans, sum[lx][ly][lk]);
		add(ans, cnt[lx][ly][lk]);
	}

	// printf("x=%d, y=%d, lim=%d --> %d\n", x, y, lim, ans);

	return ans;
}

void solve() {
	forn(it, q) {
		int x1, y1, x2, y2;
		int k;

#if !TEST
		scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
#else
		x1 = 1 + rand() % 4;
		x2 = 1 + rand() % 4;
		if  (x1 > x2) {
			swap(x1, x2);
		}

		y1 = 1 + rand() % 4;
		y2 = 1 + rand() % 4;
		if  (y1 > y2) {
			swap(y1, y2);
		}
		k = rand() % int(1e9);
#endif

		int ans = ask(x2, y2, k);
		add(ans, -ask(x1 - 1, y2, k) + MOD);
		add(ans, -ask(x2, y1 - 1, k) + MOD);
		add(ans, ask(x1 - 1, y1 - 1, k));

		// int ans = brut_ask(x2, y2, k);
		// add(ans, -brut_ask(x1 - 1, y2, k) + MOD);
		// add(ans, -brut_ask(x2, y1 - 1, k) + MOD);
		// add(ans, brut_ask(x1 - 1, y1 - 1, k));

#if TEST
		int br = brut_solve(x1, y1, x2, y2, k);
		if  (ans != br) {
			printf("ans=%d, br=%d\n", ans, br);
			printf("x1=%d, y1=%d, x2=%d, y2=%d\n", x1, y1, x2, y2);
			exit(0);
		} else {
			printf("OK %d %d\n", ans, br);
		}
#else
		printf("%d\n", ans);
#endif

		// break;
	}
}

int main() {
#ifdef LOCAL
	freopen(FILE_NAME ".in", "r", stdin);
	// freopen(FILE_NAME ".out", "w", stdout);
#endif

	while (read()) {
		solve();

		// break;
	}

#ifdef LOCAL
	cerr.precision(5);
	cerr << "Time: " << fixed << (double) clock() / CLOCKS_PER_SEC << endl;
#endif
	return 0;
}

#include <cstdio>
#include <utility>
#include <map>
#include <unordered_map>
#include <algorithm>
using namespace std;

const int M = 1e9 + 7;
map<long long, pair<int, int> > mp[33];

int sub(int a, int b) {
    a -= b;
    return a < 0 ? a + M : a;
}

int add(int a, int b) {
    a += b;
    return a >= M ? a - M : a;
}

int mul(long long a, int b) {
    a *= b;
    return a >= M ? a % M : a;
}

pair<int, int> get(int t, long long a, int b) {
    a = (a << 32) | b;
    if (mp[t].count(a) > 0) {
        return mp[t][a];
    }
    return make_pair(0, 0);
}

void add(int t, long long a, int b, pair<int, int> v) {
    mp[t][(a << 32) | b] = v;
}

unordered_map<int, pair<int, int> > mpp[40];
pair<int, int> getval(int t, int k) {
    if (k <= 0) {
        return make_pair(0, 0);
    }
    if (t == 0) {
        return make_pair(1, 1);
    }

    long long l = 1LL << t;
    k = min((long long)k, l);
    if (mpp[t].count(k)) {
        return mpp[t][k];
    }
    long long hl = l >> 1;

    pair<int, int> ans;
    pair<int, int> tmp[2];
    tmp[0] = getval(t - 1, k);
    tmp[1] = getval(t - 1, k - hl);
    ans.first = mul(2, add(tmp[0].first, tmp[1].first));
    ans.first = add(ans.first, mul(hl, mul(2, tmp[1].second)));
    ans.second = mul(2, add(tmp[0].second, tmp[1].second));
    mpp[t][k] = ans;

    return ans;
}

pair<int, int> dfs(int t, long long l, long long x, long long y, int k) {
    if (k <= 0 || x <= 0 || y <= 0) {
        return make_pair(0, 0);
    }

    if (l == 1) {
        return make_pair(1, 1);
    }

    if (x < y) {
        swap(x, y);
    }

    x = min(x, l);
    y = min(y, l);
    k = min((long long)k, l);

    if (x >= l) {
        pair<int, int> tmp = get(t, y, k);
        if (tmp.first || tmp.second) {
            return tmp;
        }
    }

    long long hl = l >> 1;

    pair<int, int> res;
    int ans = 0, num = 0;

    if (x >= l && y >= l) {
        res = getval(t, k);
        ans = res.first;
        num = res.second;
    } else {
        res = dfs(t - 1, hl, x, y, k);
        ans = res.first;
        num = res.second;

        res = dfs(t - 1, hl, x - hl, y, k - hl);
        ans = add(ans, add(res.first, mul(hl, res.second)));
        num = add(num, res.second);

        res = dfs(t - 1, hl, x, y - hl, k - hl);
        ans = add(ans, add(res.first, mul(hl, res.second)));
        num = add(num, res.second);

        res = dfs(t - 1, hl, x - hl, y - hl, k);
        ans = add(ans, res.first);
        num = add(num, res.second);
    }

    if (x >= l) {
        add(t, y, k, make_pair(ans, num));
    }
    return make_pair(ans, num);
}

int solve(int x, int y, int k) {
    return dfs(32, 1LL << 32, x, y, k).first;
}

int main() {
    int q;
    scanf("%d", &q);
    for (int i = 0; i < q; ++i) {
        int x1, y1, x2, y2, k;
        scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
        int ans = solve(x2, y2, k);
        --x1, --y1;
        ans = sub(ans, solve(x1, y2, k));
        ans = sub(ans, solve(x2, y1, k));
        ans = add(ans, solve(x1, y1, k));
        printf("%d\n", ans);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

struct Mod
{
    static const auto P = int(1e9+7);
    long long k;

    Mod() = default;
    Mod(const long long k) : k(k%P >= 0 ? k%P : k%P+P) {}
};

Mod operator+(const Mod a, const Mod b) { return a.k + b.k; }
Mod operator-(const Mod a, const Mod b) { return a.k - b.k; }
Mod operator*(const Mod a, const Mod b) { return a.k * b.k; }

struct S { Mod count, total; };
S operator+(const S a, const S b)
{
    return {a.count + b.count, a.total + b.total};
}
S operator-(const S a, const S b)
{
    return {a.count - b.count, a.total - b.total};
}

// c is a power of 2
// r <= c
S solve_2power(const int r, const int c, const int k)
{
    const auto x = min(c, k);
    return {Mod(r)*x, Mod(r)*(ll(x)*(x+1)/2)};
}

// c is a power of 2
// r <= c
// add offset to the entire grid
S solve_2power_offset(const int r, const int c, const int k, const int offset)
{
    if (k <= offset) return {0, 0};
    const auto s = solve_2power(r, c, k-offset);
    return {s.count, s.count*offset + s.total};
}

S solve(int r, int c, const int k);

S solve_offset(const int r, const int c, const int k, const int offset)
{
    if (k <= offset) return {0, 0};
    const auto s = solve(r, c, k-offset);
    return {s.count, s.count*offset + s.total};
}

S solve(int r, int c, const int k)
{
    if (r == 0 || c == 0) return {0, 0};

    if (r > c) swap(r, c);

    auto n = 1;
    while (2*n <= c) n *= 2;

    if (r <= n)
    {
        const auto A = solve_2power(r, n, k);
        const auto B = solve_offset(r, c-n, k, n);
        return A + B;
    }
    else
    {
        const auto A = solve_2power(n, n, k);
        const auto B = solve_2power_offset(c-n, n, k, n);
        const auto C = solve_2power_offset(r-n, n, k, n);
        const auto D = solve(r-n, c-n, k);
        return A + B + C + D;
    }
}

int main()
{
    ios::sync_with_stdio(false);

    int q; cin >> q;
    for (auto iterations = 0; iterations < q; iterations++)
    {
        int r1, c1, r2, c2, k;
        cin >> r1 >> c1 >> r2 >> c2 >> k;
        cout << (solve(r2, c2, k) -
                (solve(r1-1, c2, k) + solve(r2, c1-1, k)) +
                solve(r1-1, c1-1, k)).total.k << "\n";
    }
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define int ll
const int mod = 1000000007;

int pot [40];
int prod ( int a, int b ) {
    return int( (ll(a)*ll(b)) % ll(mod) );
}
struct ans{
    int n, s;
    void add( ans val ){
        n += val.n;
        n %= mod;
        s += val.s;
        s %= mod;
    }
    void mult ( ans val , int i ){
        n += val.n;
        n %= mod;
        s += (val.s + prod ( val.n , pot[i] ) ) ;
        s %= mod;
        //cout << " mult " << endl ;
    }
};

ans mem [35][2][2][2];
int vis [35][2][2][2];
int h, w, k, cont = 0;

int is_one( int x, int i ) {
    return (x>>i)&1;
}

ans dp ( int i , int same_h , int same_w , int same_k ){
    if( i == -1 ){
        ans r = ans{1,0};
        return r;
    }
    ans &ret = mem [i][same_h][same_w][same_k];
    if( vis[i][same_h][same_w][same_k] == cont ){
        return ret;
    }
    ret.n = ret.s = 0;
    /// Colocar 0 en x y 0 en y
    ret.add( dp( i-1 , same_h & (1^is_one(h,i)), same_w & (1^is_one(w,i)), same_k & (1^is_one(k,i)) ) );
    ///Colocar ( 1 , 1 )
    if( (!same_h or is_one(h,i)) and (!same_w or is_one(w,i)) ){
        ret.add( dp( i-1 , same_h & is_one(h,i) , same_w & is_one(w,i) , same_k & (1^is_one(k,i)) ) );
    }
    ///Colocar ( 1 , 0 )
    if( (!same_h or is_one(h,i)) and ( !same_k or is_one(k,i) ) ){
        ret.mult( dp( i-1 , same_h & is_one(h,i) , same_w & (1^is_one(w,i)) , same_k & is_one(k,i) ), i );
    }
    ///Colocar ( 0 , 1 )
    if( (!same_w or is_one(w,i)) and ( !same_k or is_one(k,i) ) ){
        ret.mult( dp( i-1 , same_h & (1^is_one(h,i)) , same_w & is_one(w,i) , same_k & is_one(k,i) ), i );
    }
    vis [i][same_h][same_w][same_k] = cont;
    return ret;
}

int count ( int x, int y ) {
    if ( x < 0 || y < 0 ) return 0;
    h = x;
    w = y;
    cont ++;
    ans a = dp( 31 , 1 , 1 , 1 );
    return (  a.s + a.n ) % mod;
}

main(){
    pot[0] = 1;
    for( int i = 1 ; i < 35 ; i ++ )
        pot[i] = (2*pot[i-1]) % mod ;

    int q, x1, y1, x2, y2, res;
    scanf("%I64d",&q);
    for( int i = 0 ; i < q ; i ++ ){
        scanf( "%I64d%I64d%I64d%I64d%I64d" , &x1 , &y1 , &x2 , &y2 , &k );
        x1--;
        x2--;
        y2--;
        y1--;
        k--;

        res = count(x2,y2) - count(x2,y1-1) - count(x1-1,y2) + count(x1-1,y1-1);
        res = ( res % mod + mod ) % mod;
        printf("%I64d\n",res);
    }
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mo=1e9+7;
#define y1 ovo
int pw[64],k,T,x1,y1,x2,y2;
int solve(int x,int y,int f)
{
	if(x==0||y==0||f>k)return 0;
	if(x<y)swap(x,y);
	if(x==(x&-x))
	{
		int l=f,r=min(k,f+x-1);
		return (l+r)*(r-l+1)/2%mo*y%mo;
	}
	int d=1,res=0;
	while((d<<1)<x)d<<=1;
	if(y<=d)
	{
		res+=solve(d,y,f)+solve(x-d,y,f+d);res%=mo;
	}
    else
	{
		res+=((solve(d,d,f)+solve(x-d,d,f+d))%mo+solve(d,y-d,f+d)+solve(x-d,y-d,f))%mo;res%=mo;
    }
    return res;
}
signed main()
{
    cin>>T;
    pw[0]=1;
    for(int i=1;i<=60;i++)pw[i]=pw[i-1]<<1,pw[i]%=mo;
    while(T--)
    {
        cin>>x1>>y1>>x2>>y2>>k;
        cout<<((solve(x2,y2,1)-solve(x1-1,y2,1)+mo)%mo-solve(x2,y1-1,1)+solve(x1-1,y1-1,1)+mo)%mo<<endl;
    }
}

#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
ll MOD = 1000000007LL;

ll sumRange(ll lo, ll hi, ll k, ll shift) {
	assert(lo <= hi);
	lo += shift; assert(lo < 2e9 + 123);
	hi += shift; assert(hi < 2e9 + 123);
	if (k < lo) return 0;
	hi = min(hi, k);
	return ((lo + hi) * (hi - lo + 1) / 2) % MOD;
}
ll largestPowOf2(ll hi) {
	ll p = 1;
	while (p*2 <= hi) p *= 2;
	return p;
}

ll solve(ll D, ll R, ll k, ll shift) {
	if (D < R) swap(D, R);
	if (R == 0) return 0;
	if (R == 1) return sumRange(1, D, k, shift);

	ll r = largestPowOf2(R);
	ll d = largestPowOf2(D);
	if (r == d) {
		ll S = sumRange(1, r, k, shift) * r % MOD;
		S += (D + R - 2*r) * sumRange(1, r, k, r + shift);
		S %= MOD;
		S += solve(D-r, R-r, k, shift);
		S %= MOD;
		return S;
	}
	else {
		ll S = sumRange(1, d, k, shift) * R % MOD;
		S += solve(D - d, R, k, shift + d);
		S %= MOD;
		return S;
	}
}

int main() {
	int tc; cin >> tc;
	while (tc--) {
		int x1, y1, x2, y2, k; cin >> x1 >> y1 >> x2 >> y2 >> k;
		x1--, y1--;

		ll ans = 0;
		ans += solve(x2, y2, k, 0);
		ans += solve(x1, y1, k, 0);
		ans -= solve(x1, y2, k, 0);
		ans -= solve(x2, y1, k, 0);

		ans += 10 * MOD;
		ans %= MOD;
		cout << ans << endl;
	}

	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
ll MOD = 1000000007LL;

ll sumRange(ll lo, ll hi, ll k, ll shift) {
	assert(lo <= hi);
	lo += shift;
	hi += shift; assert(hi <= (1<<30));
	if (k < lo) return 0;
	hi = min(hi, k);
	return ((lo + hi) * (hi - lo + 1) / 2) % MOD;
}
ll largestPowOf2(ll hi) {
	ll p = 1;
	while (p*2 <= hi) p *= 2;
	return p;
}

ll solve(ll D, ll R, ll k, ll shift) {
	//printf("call %lld %lld %lld\n", D, R, shift);
	assert(D + shift < (1<<30));
	assert(R + shift < (1<<30));
	if (D < R) swap(D, R);
	if (R == 0) return 0;
	if (R == 1) return sumRange(1, D, k, shift);

	ll r = largestPowOf2(R);
	ll d = largestPowOf2(D);
	if (r == d) {
		ll S = sumRange(1, r, k, shift) * r % MOD;
		S += (D + R - 2*r) * sumRange(1, r, k, r + shift);
		S %= MOD;
		S += solve(D-r, R-r, k, shift);
		S %= MOD;
		return S;
	}
	else {
		ll S = sumRange(1, d, k, shift) * R % MOD;
		S += solve(D - d, R, k, shift + d);
		S %= MOD;
		return S;
	}
}

int main() {
	//solve(1e9, 1e9, 0, 0);

	int tc; cin >> tc;
	while (tc--) {
		int x1, y1, x2, y2, k; cin >> x1 >> y1 >> x2 >> y2 >> k;
		x1--, y1--;

		ll ans = 0;
		ans += solve(x2, y2, k, 0);
		ans += solve(x1, y1, k, 0);
		ans -= solve(x1, y2, k, 0);
		ans -= solve(x2, y1, k, 0);

		ans += 10 * MOD;
		ans %= MOD;
		cout << ans << endl;
	}

	return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for(int i=(a),i##_end_=(b);i<=i##_end_;i++)
#define DOR(i,a,b) for(int i=(a),i##_end_=(b);i>=i##_end_;i--)
#define M 35
#define P 1000000007
using namespace std;

int n;
int mark[M][2][2][2];
int A[M],B[M],C[M];

struct node{
	ll sum,cnt;
}dp[M][2][2][2];

node dfs(int pos,int l1,int l2,int l3){
	if(pos==-1)return (node){0,1};
	if(mark[pos][l1][l2][l3]) return dp[pos][l1][l2][l3];
	mark[pos][l1][l2][l3]=1;
	int limit=l3?C[pos]:1;
	node ans=(node){0,0};
	FOR(i,0,l1?A[pos]:1){
		FOR(j,0,l2?B[pos]:1){
			int x=i^j;
			if(x>limit)continue;
			node tmp=dfs(pos-1,l1&&(i==A[pos]),l2&&(j==B[pos]),l3&&(x==limit));
			ans.cnt=(ans.cnt+tmp.cnt)%P;
			ans.sum=(ans.sum+tmp.sum)%P;
			ans.sum=(ans.sum+(1<<pos)*x%P*tmp.cnt%P)%P;
		}
	}
	return dp[pos][l1][l2][l3]=ans;
}

ll solve(int n,int m,int k){
	memset(mark,0,sizeof(mark));
	n--;
	m--;
	k--;
	FOR(i,0,30){
		A[i]=(n>>i)&1;
		B[i]=(m>>i)&1;
		C[i]=(k>>i)&1;
	}
	node tmp=dfs(30,1,1,1);
	return (tmp.sum+tmp.cnt)%P;
}
int main() {
	scanf("%d",&n);
	FOR(i,1,n) {
		ll ans=0;
		int a,b,c,d,k;
		scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
		ans=solve(c,d,k);
		if(a>1) ans-=solve(a-1,d,k);
		if(b>1) ans-=solve(c,b-1,k);
		if(a>1&&b>1) ans+=solve(a-1,b-1,k);
		ans=(ans%P+P)%P;
		printf("%lld\n",ans);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = (1e9)+7;

pair<ll, ll> query(ll x, ll y, ll k, ll lim) {
	if(x <= 0 || y <= 0 || k < 0 || lim <= 0) return {0, 0};
	ll pw = 1 << k;
	ll cnt = 0, sum = 0;
	if(x >= pw || y >= pw) {
		ll val_cnt = min(pw, lim), node_cnt = min(pw, min(x, y));
		cnt = val_cnt * node_cnt % MOD;
		sum = (val_cnt * (val_cnt + 1) / 2 % MOD) * node_cnt % MOD;
	} else {
		pw >>= 1;
		pair<ll, ll> res[2][2] = {
			query(x, y, k-1, lim),
			query(x - pw, y, k-1, lim - pw),
			query(x, y - pw, k-1, lim - pw),
			query(x - pw, y - pw, k-1, lim)
		};
		cnt = (res[0][0].second + res[0][1].second + res[1][0].second + res[1][1].second) % MOD;
		sum = (res[0][0].first + res[0][1].first + res[1][0].first + res[1][1].first) % MOD;
		sum = (sum + (res[0][1].second + res[1][0].second) * pw) % MOD;
	}
	assert(sum >= 0 && cnt >= 0);
	return {sum, cnt};
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while(q--) {
		ll x1, y1, x2, y2, lim;
		cin >> x1 >> y1 >> x2 >> y2 >> lim;
		ll k = 0;
		while(1ll << k <= max(x2, y2)) ++k;
		ll res = query(x2, y2, k, lim).first
					 - query(x1 - 1, y2, k, lim).first
					 - query(x2, y1 - 1, k, lim).first
					 + query(x1 - 1, y1 - 1, k, lim).first;
		res %= MOD;
		cout << (res + MOD) % MOD << '\n';
	}
}

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef pair<int,int> pr;
const int Mod=1000000007;
int f[40][2][2][2],g[40][2][2][2];
bool vis[40][2][2][2];
int num1[40],num2[40],num3[40],cnt1,cnt2,cnt3;
int two[40];
pr dfs(int at,bool lim1,bool lim2,bool lim3){
	if(at==1)return make_pair(1,0);
	if(vis[at][lim1][lim2][lim3])return make_pair(f[at][lim1][lim2][lim3],g[at][lim1][lim2][lim3]);
	pr Ans;
	Ans.first=0;Ans.second=0;
	int up1=(lim1?num1[at-1]:1),up2=(lim2?num2[at-1]:1),up3=(lim3?num3[at-1]:1);
	for(int i=0;i<=up1;++i)
		for(int j=0;j<=up2;++j)
			if((i^j)<=up3){
				pr to=dfs(at-1,lim1&(i==up1),lim2&(j==up2),lim3&((i^j)==up3));
				Ans.first=(Ans.first+to.first)%Mod;
				Ans.second=(1ll*to.first*(i^j)*two[at-2]+Ans.second+to.second)%Mod;
			}
    vis[at][lim1][lim2][lim3]=true;
    f[at][lim1][lim2][lim3]=Ans.first;
    g[at][lim1][lim2][lim3]=Ans.second;
    return Ans;
}
inline int calc(int x,int y,int k){
	if(x<0||y<0||k<0)return 0;
	memset(vis,false,sizeof(vis));
	memset(num1,0,sizeof(num1));
	memset(num2,0,sizeof(num2));
	memset(num3,0,sizeof(num3));
	cnt1=0;cnt2=0;cnt3=0;
	while(x){
		num1[++cnt1]=(x&1);
		x>>=1;
	}
	while(y){
		num2[++cnt2]=(y&1);
		y>>=1;
	}
	while(k){
		num3[++cnt3]=(k&1);
		k>>=1;
	}
	pr Ans=dfs(max(max(cnt1,cnt2),cnt3)+1,1,1,1);
	return (Ans.first+Ans.second)%Mod;
}
int main(){
	two[0]=1;
	for(int i=1;i<40;++i)two[i]=(two[i-1]+two[i-1])%Mod;
	int q;
	scanf("%d",&q);
	int x1,x2,y1,y2,k;
	for(register int tt=1;tt<=q;++tt){
		scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
		x1--;y1--;x2--;y2--;k--;
		int Ans=(calc(x2,y2,k)+calc(x1-1,y1-1,k))%Mod;
		Ans=(Ans-calc(x2,y1-1,k)+Mod)%Mod;
		Ans=(Ans-calc(x1-1,y2,k)+Mod)%Mod;
		printf("%d\n",Ans);
	}
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
ll MOD = 1000000007LL;

ll sumRange(ll lo, ll hi, ll k, ll shift) {
	assert(lo <= hi);
	lo += shift; //assert(lo < 2e9 + 123);
	hi += shift; //assert(hi < 2e9 + 123);
	if (k < lo) return 0;
	hi = min(hi, k);
	return ((lo + hi) * (hi - lo + 1) / 2) % MOD;
}
ll largestPowOf2(ll hi) {
	ll p = 1;
	while (p*2 <= hi) p *= 2;
	return p;
}

ll solve(ll D, ll R, ll k, ll shift) {
	assert(D + shift < (1<<30));
	assert(R + shift < (1<<30));
	if (D < R) swap(D, R);
	if (R == 0) return 0;
	if (R == 1) return sumRange(1, D, k, shift);

	ll r = largestPowOf2(R);
	ll d = largestPowOf2(D);
	if (r == d) {
		ll S = sumRange(1, r, k, shift) * r % MOD;
		S += (D + R - 2*r) * sumRange(1, r, k, r + shift);
		S %= MOD;
		S += solve(D-r, R-r, k, shift);
		S %= MOD;
		return S;
	}
	else {
		ll S = sumRange(1, d, k, shift) * R % MOD;
		S += solve(D - d, R, k, shift + d);
		S %= MOD;
		return S;
	}
}

int main() {
	int tc; cin >> tc;
	while (tc--) {
		int x1, y1, x2, y2, k; cin >> x1 >> y1 >> x2 >> y2 >> k;
		x1--, y1--;

		ll ans = 0;
		ans += solve(x2, y2, k, 0);
		ans += solve(x1, y1, k, 0);
		ans -= solve(x1, y2, k, 0);
		ans -= solve(x2, y1, k, 0);

		ans += 10 * MOD;
		ans %= MOD;
		cout << ans << endl;
	}

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
typedef long double ld;
typedef long long ll;

const int mod = 1e9 + 7;

int add(int x) {
  return x;
}

template<typename... T>
int add(int x, T... y) {
  x += add(y...);
  if (x >= mod)
        x -= mod;
  return x;
}

template<typename... T>
int udd(int &x, T... y) {
  return x = add(x, y...);
}

template<typename... T>
int sub(int x, T... y) {
  return add(x, mod - add(y...));
}

int mul(int x) {
  return x;
}

template<typename... T>
int mul(int x, T... y) {
  return 1ll * x * mul(y...) % mod;
}

int bin(int x, int to) {
  int y = 1;
  while (to) {
        if (to & 1)
                  y = mul(x, y);
            x = mul(x, x);
                to >>= 1;
  }
  return y;
}

int inv(int x) {
  assert(x != 0);
  return bin(x, mod - 2);
}

int bit(int mask, int i) {
  assert(i >= 0);
  return (mask >> i) & 1;
}

const int M = 40;

int d[M][2][2][2];
int h[M][2][2][2];

int fun(int A, int B, int K) {
  for (int i = 0; i < M; ++i)
    for (int ea = 0; ea < 2; ++ea)
      for (int eb = 0; eb < 2; ++eb)
        for (int ek = 0; ek < 2; ++ek) {
          d[i][ea][eb][ek] = 0;
          h[i][ea][eb][ek] = 0;
        }

  const int n = 30;
  d[0][1][1][1] = 1;

  for (int i = 0; i <= n; ++i) {
    int no = n - i;
    for (int ea = 0; ea < 2; ++ea)
      for (int eb = 0; eb < 2; ++eb)
          for (int ek = 0; ek < 2; ++ek) {
            int cnt = d[i][ea][eb][ek];
            int sum = h[i][ea][eb][ek];
            for (int ba = 0; ba < 2; ++ba)
              for (int bb = 0; bb < 2; ++bb) {
                int bk = ba ^ bb;
                if (ea && ba && !bit(A, no))
                  continue;
                if (eb && bb && !bit(B, no))
                  continue;
                if (ek && bk && !bit(K, no))
                  continue;

                int na = ea & (ba == bit(A, no) ? 1 : 0);
                int nb = eb & (bb == bit(B, no) ? 1 : 0);
                int nk = ek & (bk == bit(K, no) ? 1 : 0);

                udd(d[i + 1][na][nb][nk], cnt);
                udd(h[i + 1][na][nb][nk], add(sum, bk ? mul(cnt, bin(2, no)) : 0));
              }
          }
  }

  //return h[n + 1][0][0][0];
  return add(h[n + 1][0][0][0], d[n + 1][0][0][0]);
}

int main() {
#ifdef LOCAL
  assert(freopen("c.in", "r", stdin));
#endif

  ios_base::sync_with_stdio(false);

  //int t;
  //cin >> t;
  //while (t--) {
    //int a, b, k;
    //cin >> a >> b >> k;
    //cerr << fun(a, b, k) << endl;
  //}
  //return 0;

  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int xl, xr, yl, yr, k;
    cin >> xl >> yl >> xr >> yr >> k;
    --xl, --yl;

    int ans = 0;
    udd(ans, fun(xr, yr, k));
    udd(ans, fun(xl, yl, k));
    ans = sub(ans, fun(xr, yl, k));
    ans = sub(ans, fun(xl, yr, k));
    cout << ans << "\n";
  }
}

#include <bits/stdc++.h>
#define LL long long
#define MP make_pair
using namespace std;

template<class T> inline
void read(T& x) {
	int f = 1; x = 0;
	char ch = getchar();
	while (ch < '0' || ch > '9')   {if (ch == '-') f = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
	x *= f;
}

/*============ Header Template ============*/

const int mo = 1000000007;

typedef pair<LL, LL> PI;

int Q;
LL N, M, K;
int vis[35][2][2][2];
PI f[35][2][2][2], ans;

PI operator + (const PI &a, const PI &b) {
	return MP((a.first + b.first) % mo, (a.second + b.second) % mo);
}

PI dfs(int d, int limx, int limy, int limk) {
	if (d == -1) return MP(1, 0);
	if (vis[d][limx][limy][limk]) return f[d][limx][limy][limk];
	vis[d][limx][limy][limk] = 1;
	int upx = limx ? ((N >> d) & 1) : 1;
	int upy = limy ? ((M >> d) & 1) : 1;
	int upk = limk ? ((K >> d) & 1) : 1;
	PI ans = MP(0, 0);
	for (int i = 0; i <= upx; i++) {
		for (int j = 0; j <= upy; j++) {
			if ((i ^ j) <= upk) {
				PI t = dfs(d - 1, limx && (i == upx), limy && (j == upy), limk && ((i ^ j) == upk));
				ans = ans + t;
				ans.second = (ans.second + t.first * ((LL)(i ^ j) << d) % mo) % mo;
			}
		}
	}
	// printf("f[%d][%d][%d][%d] = %lld %lld\n", d, limx, limy, limk, ans.first, ans.second);
	return f[d][limx][limy][limk] = ans;
}

PI solve(LL n, LL m, LL k) {
	if (n == -1 || m == -1 || k == -1) return MP(0, 0);
	N = n, M = m, K = k;
	memset(vis, 0, sizeof vis);
	return dfs(31, 1, 1, 1);
}

int main() {
	read(Q);
	for (int i = 1, a, b, c, d, k; i <= Q; i++) {
		read(a), read(c), read(b), read(d), read(k); a--, b--, c--, d--, k--;
		// printf("%lld\n", solve(b, d, k));
		PI tmp1 = solve(b, d, k) + solve(a - 1, c - 1, k);
		PI tmp2 = solve(b, c - 1, k) + solve(a - 1, d, k);
		printf("%lld\n", ((tmp1.first + tmp1.second - tmp2.first - tmp2.second) % mo + mo) % mo);
	}
	return 0;
}

#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")

#define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>

using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_set>
# include <unordered_map>
# else
# include <tr1/unordered_map>
# include <tr1/unordered_set>
using namespace tr1;
# endif
# else
# include <unordered_map>
# include <unordered_set>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n"
#define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n"

#ifdef _MSC_VER
#define ALIGN(x) __declspec(align(x))
#else
#define ALIGN(x) __attribute__((aligned(x)))
#endif

#ifdef LOCAL
#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
#else
#define CURTIME() ;
#endif
double __begin;
#define DTIME(ccc) __begin = clock(); ccc; cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef int itn;

template<class T1, class T2, class T3>
struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };
template<class T1, class T2, class T3>
bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;}
template<class T1, class T2, class T3>
bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;}
#define tri triple<int,int,int>
#define trll triple<ll,ll,ll>

#define FI(n) for(int i=0;i<n;++i)
#define FJ(n) for(int j=0;j<n;++j)
#define FK(n) for(int k=0;k<n;++k)
#define FL(n) for(int l=0;l<n;++l)
#define FQ(n) for(int q=0;q<n;++q)
#define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i)
#define all(a) std::begin(a), std::end(a)
#define reunique(v) v.resize(unique(v.begin(), v.end()) - v.begin())

#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(1.0 * (x))
#define pow(x, n) pow(1.0 * (x), n)

#define COMPARE(obj) [&](const std::decay<decltype(obj)>::type& a, const std::decay<decltype(obj)>::type& b)
#define COMPARE_BY(obj, field) [&](const std::decay<decltype(obj)>::type& a, const std::decay<decltype(obj)>::type& b) { return a.field < b.field; }

#define checkbit(n, b) (((n) >> (b)) & 1)
#define setbit(n, b) ((n) | (static_cast<decltype(n)>(1) << (b)))
#define removebit(n, b) (~setbit(~(n), (b)))
inline int bits_count(int v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int bits_count(ll v){int t=v>>32;int p=(v& ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
template<class T>
inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; }
inline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); }
unsigned long long rdtsc() {
    unsigned long long ret;
    __asm__ __volatile__("rdtsc" : "=A" (ret) : :);
    return ret;
}

//STL output *****************************************************************************************************
#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p);
TT1 ostream& operator << (ostream& os, const vector<T>& v);
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v);
TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }
TT1T2 void printarray(T1 a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }

//STL input *****************************************************************************************************
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t);
TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
TT1 inline istream& operator >> (istream& os, vector<T>& v) {
    if (v.size()) for (T& t : v) os >> t; else {
        string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;}
        stringstream ss(s); while (ss >> obj) v.push_back(obj);
    }
    return os;
}
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }

//Pair magic *****************************************************************************************************
#define PT1T2 pair<T1, T2>
TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }

#undef TT1
#undef TT1T2
#undef TT1T2T3

#define FREIN(FILE) freopen(FILE, "rt", stdin)
#define FREOUT(FILE) freopen(FILE, "wt", stdout)

template<class T> bool isPrime(T x) { if (x < 2) return 0; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return 0; return 1; }
inline void normmod(ll &x, ll m) { x %= m; if (x < 0) x += m; }
inline ll summodfast(ll x, ll y, ll m) { ll res = x + y; if (res >= m) res -= m; return res; }
inline int summodfast(int x, int y, int m) { int res = x + y; if (res >= m) res -= m; return res; }
inline void addmodfast(ll &x, ll y, ll m) { x += y; if (x >= m) x -= m; }
inline void addmodfast(int &x, int y, int m) { x += y; if (x >= m) x -= m; }
inline void submodfast(ll &x, ll y, ll m) { x -= y; if (x < 0) x += m; }
inline void submodfast(int &x, int y, int m) { x -= y; if (x < 0) x += m; }
inline ll mulmod(ll x, ll n, ll m){ ll res = 0; normmod(x, m); normmod(n, m); while (n){ if (n & 1) res = summodfast(res, x, m); x = summodfast(x, x, m); n >>= 1; } return res; }
inline ll powmod(ll x, ll n, ll m){ ll res = 1; while (n){ if (n & 1)res = (res*x) % m; x = (x*x) % m; n >>= 1; }return res; }
inline ll gcd(ll a, ll b){ ll t; while (b){ a = a%b; t = a; a = b; b = t; }return a; }
inline int gcd(int a, int b){ int t; while (b){ a = a%b; t = a; a = b; b = t; }return a; }
inline ll lcm(ll a, ll b){ return a / gcd(a, b)*b; }
inline ll gcd(ll a, ll b, ll c){ return gcd(gcd(a, b), c); }
inline int gcd(int a, int b, int c){ return gcd(gcd(a, b), c); }
ll gcdex(ll a, ll b, ll& x, ll& y) {
    if (!a) { x = 0; y = 1; return b; }
    ll y1;
    ll d = gcdex(b % a, a, y, y1);
    x = y1 - (b / a) * y;
    return d;
}

// Useful constants

//int some_primes[10] = {24443, 100271, 500179, 1000003, 1000333, 2000321, 5000321, 98765431, 1000000123};
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define EPS         (double)1e-10
#define mod         1000000007
#define PI          3.14159265358979323
#define link        asaxlajrewqwe
#define rank        wahayawehasdakw
//*************************************************************************************

void add(ll& x, ll y) {
    x += y;
    if (x >= mod) x -= mod;
}
void sub(ll& x, ll y) {
    x -= y;
    if (x < 0) x += mod;
}
ll pow2(ll v) {
    return 1LL << (63 - __builtin_clzll(v));
}
ll solve2(ll x, ll y, ll k, ll bonus = 0) {
    if (x <= 0 || y <= 0 || k <= 0) return 0;
    if (x < y) swap(x, y);
    ll ty = pow2(y);
    ll tx = pow2(x);
    ll res;
    ll txk = min(tx, k);
    if (tx == ty) {
        res = txk * ty % mod * (txk + 1 + bonus * 2) % mod;
        add(res, solve2(x - tx, tx, k - tx, bonus + tx));
        add(res, solve2(y - tx, tx, k - tx, bonus + tx));
        add(res, solve2(x - tx, y - tx, k, bonus));
    } else {
        res = txk * y % mod * (txk + 1 + bonus * 2) % mod;
        add(res, solve2(x - tx, y, k - tx, bonus + tx));
    }
    return res;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef LOCAL
    FREIN("input.txt");
//    FREOUT("output.txt");
#endif
    int q;
    cin >> q;
    while (q--) {
        int x1, y1, x2, y2, k;
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        ll ans = solve2(x2, y2, k) - solve2(x1 - 1, y2, k) - solve2(x2, y1 - 1, k) + solve2(x1 - 1, y1 - 1, k);
        ans %= mod;
        if (ans < 0) {
            ans += mod;
        }
        cout << ans * powmod(2LL, mod - 2, mod) % mod << endl;
    }
    CURTIME();
    return 0;
}

#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")

#define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>

using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_set>
# include <unordered_map>
# else
# include <tr1/unordered_map>
# include <tr1/unordered_set>
using namespace tr1;
# endif
# else
# include <unordered_map>
# include <unordered_set>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n"
#define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n"

#ifdef _MSC_VER
#define ALIGN(x) __declspec(align(x))
#else
#define ALIGN(x) __attribute__((aligned(x)))
#endif

#ifdef LOCAL
#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
#else
#define CURTIME() ;
#endif
double __begin;
#define DTIME(ccc) __begin = clock(); ccc; cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef int itn;

template<class T1, class T2, class T3>
struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };
template<class T1, class T2, class T3>
bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;}
template<class T1, class T2, class T3>
bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;}
#define tri triple<int,int,int>
#define trll triple<ll,ll,ll>

#define FI(n) for(int i=0;i<n;++i)
#define FJ(n) for(int j=0;j<n;++j)
#define FK(n) for(int k=0;k<n;++k)
#define FL(n) for(int l=0;l<n;++l)
#define FQ(n) for(int q=0;q<n;++q)
#define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i)
#define all(a) std::begin(a), std::end(a)
#define reunique(v) v.resize(unique(v.begin(), v.end()) - v.begin())

#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(1.0 * (x))
#define pow(x, n) pow(1.0 * (x), n)

#define COMPARE(obj) [&](const std::decay<decltype(obj)>::type& a, const std::decay<decltype(obj)>::type& b)
#define COMPARE_BY(obj, field) [&](const std::decay<decltype(obj)>::type& a, const std::decay<decltype(obj)>::type& b) { return a.field < b.field; }

#define checkbit(n, b) (((n) >> (b)) & 1)
#define setbit(n, b) ((n) | (static_cast<decltype(n)>(1) << (b)))
#define removebit(n, b) (~setbit(~(n), (b)))
inline int bits_count(int v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int bits_count(ll v){int t=v>>32;int p=(v& ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
template<class T>
inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; }
inline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); }
unsigned long long rdtsc() {
    unsigned long long ret;
    __asm__ __volatile__("rdtsc" : "=A" (ret) : :);
    return ret;
}

//STL output *****************************************************************************************************
#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p);
TT1 ostream& operator << (ostream& os, const vector<T>& v);
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v);
TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }
TT1T2 void printarray(T1 a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }

//STL input *****************************************************************************************************
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t);
TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
TT1 inline istream& operator >> (istream& os, vector<T>& v) {
    if (v.size()) for (T& t : v) os >> t; else {
        string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;}
        stringstream ss(s); while (ss >> obj) v.push_back(obj);
    }
    return os;
}
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }

//Pair magic *****************************************************************************************************
#define PT1T2 pair<T1, T2>
TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }

#undef TT1
#undef TT1T2
#undef TT1T2T3

#define FREIN(FILE) freopen(FILE, "rt", stdin)
#define FREOUT(FILE) freopen(FILE, "wt", stdout)

template<class T> bool isPrime(T x) { if (x < 2) return 0; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return 0; return 1; }
inline void normmod(ll &x, ll m) { x %= m; if (x < 0) x += m; }
inline ll summodfast(ll x, ll y, ll m) { ll res = x + y; if (res >= m) res -= m; return res; }
inline int summodfast(int x, int y, int m) { int res = x + y; if (res >= m) res -= m; return res; }
inline void addmodfast(ll &x, ll y, ll m) { x += y; if (x >= m) x -= m; }
inline void addmodfast(int &x, int y, int m) { x += y; if (x >= m) x -= m; }
inline void submodfast(ll &x, ll y, ll m) { x -= y; if (x < 0) x += m; }
inline void submodfast(int &x, int y, int m) { x -= y; if (x < 0) x += m; }
inline ll mulmod(ll x, ll n, ll m){ ll res = 0; normmod(x, m); normmod(n, m); while (n){ if (n & 1) res = summodfast(res, x, m); x = summodfast(x, x, m); n >>= 1; } return res; }
inline ll powmod(ll x, ll n, ll m){ ll res = 1; while (n){ if (n & 1)res = (res*x) % m; x = (x*x) % m; n >>= 1; }return res; }
inline ll gcd(ll a, ll b){ ll t; while (b){ a = a%b; t = a; a = b; b = t; }return a; }
inline int gcd(int a, int b){ int t; while (b){ a = a%b; t = a; a = b; b = t; }return a; }
inline ll lcm(ll a, ll b){ return a / gcd(a, b)*b; }
inline ll gcd(ll a, ll b, ll c){ return gcd(gcd(a, b), c); }
inline int gcd(int a, int b, int c){ return gcd(gcd(a, b), c); }
ll gcdex(ll a, ll b, ll& x, ll& y) {
    if (!a) { x = 0; y = 1; return b; }
    ll y1;
    ll d = gcdex(b % a, a, y, y1);
    x = y1 - (b / a) * y;
    return d;
}

// Useful constants

//int some_primes[10] = {24443, 100271, 500179, 1000003, 1000333, 2000321, 5000321, 98765431, 1000000123};
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define EPS         (double)1e-10
#define mod         1000000007
#define PI          3.14159265358979323
#define link        asaxlajrewqwe
#define rank        wahayawehasdakw
//*************************************************************************************

void add(ll& x, ll y) {
    x += y;
    if (x >= mod) x -= mod;
}
void sub(ll& x, ll y) {
    x -= y;
    if (x < 0) x += mod;
}
ll solve2(ll x, ll y, ll k, ll bonus = 0) {
    if (x <= 0 || y <= 0 || k <= 0) return 0;
    if (x == y && (x & (x - 1)) == 0) {
        k = min(k, x);
        return (k * (k + 1) % mod * x + bonus * k * 2 % mod * x) % mod;
    }
    if (x < y) swap(x, y);
    ll ty = 1;
    while (ty * 2 <= y) {
        ty *= 2;
    }
    ll tx = 1;
    while (tx * 2 <= x) {
        tx *= 2;
    }
    ll res;
    ll txk = min(tx, k);
    if (tx == ty) {
        res = txk * (txk + 1) % mod * ty + bonus * txk * 2 % mod * ty;
        res %= mod;
        add(res, solve2(x - tx, tx, k - tx, bonus + tx));
        add(res, solve2(y - tx, tx, k - tx, bonus + tx));
        add(res, solve2(x - tx, y - tx, k, bonus));
    } else {
        res = txk * (txk + 1) % mod * y + bonus * txk * 2 % mod * y;
        res %= mod;
        add(res, solve2(x - tx, y, k - tx, bonus + tx));
    }
    return res;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef LOCAL
    FREIN("input.txt");
//    FREOUT("output.txt");
#endif
    int q;
    cin >> q;
    while (q--) {
        int x1, y1, x2, y2, k;
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        ll ans = solve2(x2, y2, k) - solve2(x1 - 1, y2, k) - solve2(x2, y1 - 1, k) + solve2(x1 - 1, y1 - 1, k);
        ans %= mod;
        if (ans < 0) {
            ans += mod;
        }
        cout << ans * powmod(2LL, mod - 2, mod) % mod << endl;
    }
    CURTIME();
    return 0;
}

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string.h>

using namespace std;
typedef long long LL;
const LL mod = 1e9+7;

LL f[40][2][2][2];
LL g[40][2][2][2];
LL calc(int n,int m,int K)
{
	if (n<=0||m<=0||K<=0) return 0;
	memset(f,0,sizeof f);
	memset(g,0,sizeof g);

	f[31][0][0][0]=1;
	for (int i=30;i>=0;i--)
	{
		int ni=(n>>i)&1;
		int mi=(m>>i)&1;
		int Ki=(K>>i)&1;
		for (int j=0;j<=1;j++)
		for (int k=0;k<=1;k++)
		for (int l=0;l<=1;l++)
		if (LL c=f[i+1][j][k][l])
		{
			LL w=g[i+1][j][k][l];
			for (int ai=0;ai<=1;ai++)
			if (j||ai<=ni)
			for (int bi=0;bi<=1;bi++)
			if (k||bi<=mi)
			{
				int ci=ai^bi;
				if (l||ci<=Ki)
				{
					(f[i][j|(ai<ni)][k|(bi<mi)][l|(ci<Ki)]+=c)%=mod;
					(g[i][j|(ai<ni)][k|(bi<mi)][l|(ci<Ki)]+=w+c*(ci<<i))%=mod;
				}
			}
		}
	}
	return (f[0][1][1][1]+g[0][1][1][1])%mod;
}
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("car.in","r",stdin);
		freopen("car.out","w",stdout);
	#endif
	int T;for (scanf("%d",&T);T--;)
	{
		int x1,y1,x2,y2,K;LL ans=0;
		scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&K);
		ans+=calc(x2,y2,K)+calc(x1-1,y1-1,K);
		ans-=calc(x2,y1-1,K)+calc(x1-1,y2,K);
		ans%=mod;printf("%lld\n",ans<0?ans+mod:ans);
	}
	return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <set>
#include <iostream>
#define cl(a,b) memset(a,b,sizeof(a))
#define fr(a,b,c) for(int a = b; a < c; a++)
#define rp(a,b) fr(a,0,b)
#define sc(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d%d", &a, &b)
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define oo 0x3f3f3f3f

using namespace std;

typedef pair<int,int> pii;
typedef long long ll;

#define N 200010
#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<

#define MOD 1000000007LL

ll get(int i, int j, ll l, ll v, int a, int b, ll k) {
	if (v > k) return 0;
	if (i > a || j > b) return 0;
	if (i <= a && i+l-1 <= a) {
		ll tam = min(ll(b), j+l-1) - j + 1;
		ll v2 = min(k, v + l - 1);
		return (tam * (((v + v2) * (v2 - v + 1) / 2) % MOD)) % MOD;
	}
	if (j <= b && j+l-1 <= b) {
		ll tam = min(ll(a), i+l-1) - i + 1;
		ll v2 = min(k, v + l - 1);
		return (tam * (((v + v2) * (v2 - v + 1) / 2) % MOD)) % MOD;
	}

	ll l2 = l>>1LL;
	return 	(get(i, j, l2, v, a, b, k) +
			get(i, j+l2, l2, v+l2, a, b, k) +
			get(i+l2, j, l2, v+l2, a, b, k) +
			get(i+l2, j+l2, l2, v, a, b, k)) % MOD;
}

ll query(int i, int j, ll k) {
	if (i < 0 || j < 0) return 0;
	ll l = 1;
	while (l <= max(i,j)+1) l<<=1LL;
	return get(0, 0, l, 1, i, j, k) % MOD;
}

int main() {
	int n; sc(n);
	rp(i,n) {
		int i1, j1, i2, j2, k;
		sc2(i1,j1);
		sc2(i2,j2);
		i1--, j1--, i2--, j2--;
		sc(k);
		printf("%lld\n", (query(i2, j2, k) - query(i2, j1-1, k) - query(i1-1, j2, k) + query(i1-1, j1-1, k) + 4*MOD) % MOD);
	}
	return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const int N=100005,P=1e9+7;
int n,a[50],b[50],c[50],an,bn,cn,len;
typedef pair<int,int> pr;
pr f[50][2][2][2];

inline void read(int &i){
	i=0;char c=getchar();bool j=0;
	for(;!isdigit(c);c=getchar())if(c=='-')j=1;
	for(;isdigit(c);c=getchar())i=(i<<1)+(i<<3)+c-'0';i=j?-i:i;
}
inline void add(int &x,int y){(x+=y)>=P?x-=P:0;}

inline pr dfs(int p,int x,int y,int z){
	if(!p)return pr(0,1);
	pr &F=f[p][x][y][z];
	if(~F.first)return F;
	F=pr(0,0);
	int l1=x?a[p]:1;
	int l2=y?b[p]:1;
	for(int i=0;i<=l1;++i)
		for(int j=0;j<=l2;++j){
			if(z&&c[p]<(i^j))continue;
			pr t=dfs(p-1,x&&i==a[p],y&&j==b[p],z&&(i^j)==c[p]);
			add(F.first,t.first);
			add(F.second,t.second);
			if(i^j)add(F.first,(1ll<<(p-1))*t.second%P);
		}
	return F;
}

inline int calc(int x,int y,int k){
	if(!x||!y)return 0;
	for(int i=0;i<50;++i)for(int j=0;j<2;++j)for(int k=0;k<2;++k)
		for(int l=0;l<2;++l)f[i][j][k][l]=pr(-1,0);
	an=bn=cn=0;
	x--;y--;k--;
//	printf("%d %d %d\n",x,y,k);
	while(x)a[++an]=x&1,x>>=1;
	while(y)b[++bn]=y&1,y>>=1;
	while(k)c[++cn]=k&1,k>>=1;
	len=max(an,max(bn,cn));
	for(int i=an+1;i<=len;++i)a[i]=0;
	for(int i=bn+1;i<=len;++i)b[i]=0;
	for(int i=cn+1;i<=len;++i)c[i]=0;
//	for(int i=1;i<=len;++i)printf("%d",a[i]);puts("");
//	for(int i=1;i<=len;++i)printf("%d",b[i]);puts("");
//	for(int i=1;i<=len;++i)printf("%d",c[i]);puts("");
	pr p=dfs(len,1,1,1);
//	printf("%d\n",p.second);
	add(p.first,p.second);
	return p.first;
}

int main(){
	read(n);
	while(n--){
		int x1,x2,y1,y2,k;
		read(x1),read(y1),read(x2),read(y2),read(k);
		int ans=((ll)calc(x2,y2,k)-calc(x1-1,y2,k)-calc(x2,y1-1,k)+calc(x1-1,y1-1,k))%P;
		if(ans<0)ans+=P;
		printf("%d\n",ans);
	}
	return 0;
}

#pragma warning(disable:4996)

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <complex>
#include <iterator>
#include <random>
#include <time.h>
#include <tuple>
#include <functional>
#include <list>
#include <limits.h>
#define mp make_pair
#define ni(x) scanf("%d", &(x))
#define nii(x,y) scanf("%d%d",&(x),&(y))
#define mul(x,y) ((ll)(x)*(y)%mod)
#define mtp make_tuple
#define F(i,n) for(int i = 0; i < (n); i++)
#define FF(i,n) for(int i = 1; i <= (n); i++)
#define FE(i,n) for(int i = 0; i <= (n); i++)

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int mod = 1000000007;
const int inf = 2012345678;
const ll infl = 9012345678901234567;
const double pi = 3.1415926535897932384626433832795;
//----------------------------------------------------------------------------//

int sm(int k) {
	k %= mod;
	return (ll)k*(k + 1) / 2 % mod;
}

int Do(int i, int j, int k) {
	if (!i || !j)return 0;
	int ans = 0;
	int off = 0;
	for (int e = 29; e >= 0; e--) {
		int n = 1 << e;
		if (i < n && j < n) continue;
		else if (j < n)swap(i, j);
		if (i < n) {
			ans += mul(i, sm(min(k,n)));
			if (ans >= mod)ans -= mod;
			ans += mul(off, mul(i, min(k,n)));
			if (ans >= mod)ans -= mod;
			j -= n;
			k -= n;
			if (k <= 0)return ans;
			off += n;
		}
		else {
			ans += mul(n, sm(min(k,n)));
			if (ans >= mod)ans -= mod;
			ans += mul(off, mul(n, min(k,n)));
			if (ans >= mod)ans -= mod;
			i -= n;
			j -= n;
			if (k > n) {
				ans += mul(i, sm(min(k - n,n)));
				if (ans >= mod)ans -= mod;
				ans += mul(off + n, mul(i, min(k - n,n)));
				if (ans >= mod)ans -= mod;

				ans += mul(j, sm(min(k - n,n)));
				if (ans >= mod)ans -= mod;
				ans += mul(off + n, mul(j, min(k - n,n)));
				if (ans >= mod)ans -= mod;
			}
		}
	}
	return ans;
}

int main() {
#ifndef __GNUG__
	freopen("input.txt", "r", stdin);
#endif
	int q; ni(q);
	F(i, q) {
		int a, b, c, d, k; nii(a, b); nii(c, d); ni(k);
		a--; b--;
		int ans = Do(c, d, k) + Do(a, b, k);
		if (ans >= mod)ans -= mod;
		ans += mod - Do(a, d, k);
		if (ans >= mod)ans -= mod;
		ans += mod - Do(c, b, k);
		if (ans >= mod)ans -= mod;
		printf("%d\n", ans);
	}

}

#include<bits/stdc++.h>

using namespace std;

int dp[2][2][2], oldDp[2][2][2], cnt[2][2][2], oldCnt[2][2][2];
const int mod = 1e9 + 7;

int add (int x, int y) {int ans = x + y; if (ans >= mod) ans -= mod; return ans;}
int subtract (int x, int y) {if (x >= y) return x - y; return x - y + mod;}
int mul (int x, int y) {return 1LL * x * y % mod;}
void adto (int &x, int y) {x += y; if (x >= mod) x -= mod;}

int solve (int n, int m, int K)
{
    if (n < 0 || m < 0) return 0;
    n ++, m ++;///I want them to be strictly smaller now, strictly smaller than K too
    memset (dp, 0, sizeof (dp));
    memset (cnt, 0, sizeof (cnt));
    cnt[0][0][(K >> 30) & 1] = 1;
    for (int p=29; p>=0; p--)
    {
        int bN = (n >> p) & 1, bM = (m >> p) & 1, bK = (K >> p) & 1;
        memcpy (oldCnt, cnt, sizeof (cnt));
        memcpy (oldDp, dp, sizeof (dp));
        memset (dp, 0, sizeof (dp));
        memset (cnt, 0, sizeof (cnt));
        for (int i=0; i<2; i++)
            for (int j=0; j<2; j++)
                for (int k=0; k<2; k++)
                    if (oldCnt[i][j][k])
                        for (int b1 = 0; b1<2; b1++)
                            for (int b2 = 0; b2 < 2; b2++)
                            {
                                if (b1 > bN && i == 0) continue;
                                if (b2 > bM && j == 0) continue;
                                if ((b1 ^ b2) > bK && k == 0) continue;
                                int ni = i | (b1 < bN), nj = j | (b2 < bM), nk = k | ((b1 ^ b2) < bK);
                                adto (cnt[ni][nj][nk], oldCnt[i][j][k]);
                                if (b1 ^ b2) adto (dp[ni][nj][nk], add (oldDp[i][j][k], mul (oldCnt[i][j][k], 1 << p)));
                                else adto (dp[ni][nj][nk], oldDp[i][j][k]);
                            }
    }
    return add (dp[1][1][1], cnt[1][1][1]);
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

int Tests;
scanf ("%d", &Tests);
while (Tests --)
{
    int a1, b1, a2, b2, k;
    scanf ("%d %d %d %d %d", &a1, &b1, &a2, &b2, &k), a1 --, b1 --, a2 --, b2 --;
    int ans = subtract (add (solve (a2, b2, k), solve (a1 - 1, b1 - 1, k)), add (solve (a1 - 1, b2, k), solve (a2, b1 - 1, k)));
    printf ("%d\n", ans);
}
return 0;
}

//lxrtxdy!
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
const int mo=1000000007;
int lx[55],ly[55],rx[55],ry[55];
int v[55],vis[55][2][2][2][2][2];
pii f[55][2][2][2][2][2];
pii F(int d,int v1,int v2,int v3,int v4,int v5){
	if (d==-1) return pii(0,1);
	if (vis[d][v1][v2][v3][v4][v5])
		return f[d][v1][v2][v3][v4][v5];
	pii ans(0,0);
	For(vx,v1?lx[d]:0,v2?rx[d]:1)
		For(vy,v3?ly[d]:0,v4?ry[d]:1)
			if (!v5||((vx^vy)<=v[d])){
				pii tmp=F(d-1,v1&(vx==lx[d]),v2&(vx==rx[d]),
							  v3&(vy==ly[d]),v4&(vy==ry[d]),
							  v5&((vx^vy)==v[d]));
				if (vx^vy) tmp.fi=(tmp.fi+(1ll<<d)*tmp.se)%mo;
				ans.fi=(ans.fi+tmp.fi)%mo;
				ans.se=(ans.se+tmp.se)%mo;
			}
	vis[d][v1][v2][v3][v4][v5]=1;
	return f[d][v1][v2][v3][v4][v5]=ans;
}
void decode(int *a,int d){
	For(i,0,29)
		a[i]=(d>>i)&1;
}
void solve(){
	int a,b,c,d,e;
	scanf("%d%d%d%d%d",&a,&c,&b,&d,&e);
	decode(lx,a-1); decode(rx,b-1);
	decode(ly,c-1); decode(ry,d-1);
	decode(v,min(e,1<<30)-1);
	memset(f,0,sizeof(f));
	memset(vis,0,sizeof(vis));
	pii ans=F(29,1,1,1,1,1);
	printf("%d\n",(ans.fi+ans.se)%mo);
}
int main(){
	int T;
	scanf("%d",&T);
	while (T--) solve();
}
//orzlxr!//hello

#include<stdio.h>
#include"iostream"
#define mod 1000000007
typedef long long ll;
using namespace std;
ll ct = 0;
ll l(ll x){
  ll ans = 0, p = 1;
  while(2 * p <= x){
    ans++;
    p *= 2;
  }
  return ans;
}

ll sm(ll p){
  return ((p * (p + 1)) / 2) % mod;
}

ll comp_less(ll x, ll y, ll k){
  if(x == 0 || y == 0 || k < 1) return 0;
  ll m = max(l(x), l(y));
  ll ans = 0, r;
  if((1LL << m) >= min(x, y)){
    if(x > y) swap(x, y);
    r = min(1LL << m, k);
    ans = (r * x) % mod;
    if(k > (1LL << m)) ans += comp_less(x, y - (1LL << m), k - (1LL << m));
    ans %= mod;
    return ans;
  }
  m = min(l(x), l(y));
  r = min(1LL << m, k);
  ans = ((1LL << m) * r) % mod;
  if(k > (1LL << m)) ans += comp_less(1LL << m, y - (1LL << m), k - (1LL << m)) + comp_less(1LL << m, x - (1LL << m), k - (1LL << m));
  ans += comp_less(x - (1LL << m), y - (1LL << m), k);
  ans %= mod;
  return ans;
}

ll comp(ll x, ll y, ll k){
  //cout << x << " " << y << "\n";
  if(x == 0 || y == 0) return 0;
  ct++;
  ll m = max(l(x), l(y));
  ll ans = 0, r, extray, extrax;
  if((1LL << m) >= min(x, y)){
    if(x > y) swap(x, y);
    r = min(1LL << m, k);
    ans = (sm(r) * x) % mod;
    extray = (comp_less(x, y, k) - comp_less(x, 1LL << m, k)) % mod;
     //extray = 0;
    if(k > (1LL << m)) ans += (extray * (1LL << m)) % mod + comp(x, y - (1LL << m), k - (1LL << m));
    ans %= mod;
    return ans;
  }
  m = min(l(x), l(y));
  r = min(1LL << m, k);
  ans = ((1LL << m) * sm(r)) % mod;
  extray = (comp_less(1LL << m, y, k) - comp_less(1LL << m, 1LL << m, k)) % mod;
  extrax = (comp_less(x, 1LL << m, k) - comp_less(1LL << m, 1LL << m, k));
  //extray = 0;
  //extrax = 0;
  if(k > (1LL << m)) ans += (extray * (1LL << m)) % mod + (extrax * (1LL << m)) % mod + comp(1LL << m, y - (1LL << m), k - (1LL << m)) + comp(1LL << m, x - (1LL << m), k - (1LL << m));
  ans += comp(x - (1LL << m), y - (1LL << m), k);
  ans %= mod;
  return ans;
}

int main(){
  //  cout << comp_less(5, 5, 1);
  ll q, x1, y1, x2, y2, k;
  cin >> q;
  while(q--){
    cin >> x1 >> y1 >> x2 >> y2 >> k;
    ll ans = comp(x2, y2, k) - comp(x1 - 1, y2, k) - comp(x2, y1 - 1, k) + comp(x1 - 1, y1 - 1, k);
    ans = (ans % mod + mod) % mod;
    cout << ans << "\n";
    //   cout << ct << "\n";
  }

  return 0;
}

#include <bits/stdc++.h>
#define FO(i,a,b) for (int i = (a); i < (b); i++)
#define sz(v) int(v.size())

using namespace std;

int main() {
}

//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,popcnt,avx,avx2,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;

using matrix = vector<vector<ll>>;
template<typename T>
using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
//#define int long long;

void FAST_IO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //cout.setf(ios::fixed);
    //cout.precision(20);
    #ifndef _offline
    //freopen("kek.in", "r", stdin);
    //freopen("kek.out", "w", stdout);
    #endif
}

const ll mod = 1e9 + 7;
const int LOGN = 31;

ll cnt[LOGN][2][2][2], dp[LOGN][2][2][2];

void add(ll &a, ll b) {
    a += b;
    if (a >= mod) {
        a -= mod;
    }
}

ll solve(int x, int y, int k) {
    memset(cnt, 0, sizeof(cnt));
    memset(dp, 0, sizeof(dp));
    cnt[LOGN - 1][0][0][0] = 1;
    dp[LOGN - 1][0][0][0] = 0;
    for (int i = LOGN - 2; i >= 0; --i) {
        for (int f1 = 0; f1 <= 1; ++f1) {
            for (int f2 = 0; f2 <= 1; ++f2) {
                for (int f = 0; f <= 1; ++f) {
                    for (int b1 = 0; b1 <= 1; ++b1) {
                        for (int b2 = 0; b2 <= 1; ++b2) {
                            int b = (b1 ^ b2);
                            if ((f1 == 1 || (b1 == 0 || (x & (1 << i)))) && (f2 == 1 || (b2 == 0 || (y & (1 << i)))) && (f == 1 || (b == 0 || (k & (1 << i))))) {
                                int nf1 = (f1 | (b1 == 0 && (x & (1 << i))));
                                int nf2 = (f2 | (b2 == 0 && (y & (1 << i))));
                                int nf = (f | (b == 0 && (k & (1 << i))));
                                add(cnt[i][nf1][nf2][nf], cnt[i + 1][f1][f2][f]);
                                ll a = (cnt[i + 1][f1][f2][f] * b * 1LL * (1 << i));
                                add(a, dp[i + 1][f1][f2][f]);
                                add(dp[i][nf1][nf2][nf], a);
                            }
                        }
                    }
                }
            }
        }
    }
    ll ans = dp[0][1][1][1];
    add(ans, cnt[0][1][1][1]);
    return ans;
}

signed main() {
    FAST_IO();
    int q;
    cin >> q;
    while (q--) {
        int x1, y1, x2, y2, k;
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        ll ans = solve(x2, y2, k);
        add(ans, mod - solve(x1 - 1, y2, k));
        add(ans, mod - solve(x2, y1 - 1, k));
        add(ans, solve(x1 - 1, y1 - 1, k));
        cout << ans << '\n';
    }
}

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define gt getchar()
#define ll long long
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
typedef std::pair<int,int> P;
#define mk std::make_pair
#define fr first
#define sc second
inline int in()
{
	int k=0;char ch=gt;bool p=1;
	while(ch<'-')ch=gt;if(ch=='-')ch=gt,p=0;
	while(ch>'-')k=k*10+ch-'0',ch=gt;
	return p?k:-k;
}
const int N=55,YL=1e9+7;
inline int MO(const int &x){return x>=YL?x-YL:x;}
inline void add(int &x,int y){if((x+=y)>=YL)x-=YL;}
inline void dec(int &x,int y){if((x-=y)< 0 )x+=YL;}
int sn[N],sm[N],sk[N],tot,pw[N];
P dp[N][2][2][2];int o[N][2][2][2];
P dfs(int d,int a,int b,int c)
{
	if(d==-1)return mk(0,1);
	if(o[d][a][b][c])return dp[d][a][b][c];
	o[d][a][b][c]=1;P &res=dp[d][a][b][c];res=mk(0,0);
	int ma=a?1:sn[d],mb=b?1:sm[d],mc=c?1:sk[d],k;
	for(int i=0;i<=ma;++i)
		for(int j=0;j<=mb;++j)
			if((k=(i^j))<=mc)
			{
				P ret=dfs(d-1,i<ma||a,j<mb||b,k<mc||c);
				add(res.sc,ret.sc);add(res.fr,ret.fr);
				if(!k)continue;res.fr=(1ll*ret.sc*pw[d]+res.fr)%YL;
			}
	return res;
}
int calc(int n,int m,int k)
{
	if(n<0||m<0||k<0)return 0;memset(o,0,sizeof o);tot=-1;
	while(n||m||k)sn[++tot]=n&1,sm[tot]=m&1,sk[tot]=k&1,n>>=1,m>>=1,k>>=1;
	return dfs(tot,0,0,0).fr+dfs(tot,0,0,0).sc;
}
int main()
{
	for(int i=pw[0]=1;i<=31;++i)pw[i]=MO(pw[i-1]<<1);
	for(int t=in();t;--t)
	{
		int x1=in()-1,y1=in()-1,x2=in()-1,y2=in()-1,k=in()-1,ans=0;
		add(ans,calc(x2,y2,k));add(ans,calc(x1-1,y1-1,k));
		dec(ans,calc(x2,y1-1,k));dec(ans,calc(x1-1,y2,k));
		printf("%d\n",ans);
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <db, db> pdd;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;

#define FOR(k,a,b) for(int k=(a); k<=(b); ++k)
#define IFOR(k,a,b) for(int k=(a); k>=(b); --k)
#define REP(k,a) for(int k=0; k<(a);++k)
#define IREP(k,a) for(int k=a-1; k>=(0);--k)
#define SZ(a) int((a).size())
#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define x first
#define y second
#define INF 1000000001
#define INFLONG 1000000000000000000
#define MOD 1000000007
#define MAX 100
#define ITERS 100
#define MAXM 200000
#define MAXN 1000000
#define PI 3.1415926535897932384626
#define ERR -987654321
#define MEM(a,b) memset(a,b,sizeof(a));
#define read1(a) scanf("%d",&a)
#define read2(a,b) scanf("%d%d",&a,&b)
#define read3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define readl1(a) scanf("%lld",&a)
#define readl2(a,b) scanf("%lld%lld",&a,&b)
#define readl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)

ll dp[32][2][2][2];
ll sum[32][2][2][2];

void add(ll &x,ll y){
    x+=y;
    if(x>=MOD)x-=MOD;
}

void sub(ll &x,ll y){
    x-=y;
    if(x<0)x+=MOD;
}

ll mul(ll x,ll y){
    return x*y%MOD;
}

ll pot[32];

ll solve(int x,int y,int z){
    if(x<0||y<0||z<0)return 0;
    memset(dp,0,sizeof dp);
    memset(sum,0,sizeof sum);
    vi A,B,C;
    FOR(j,0,31){
        A.PB(x%2);x/=2;
        B.PB(y%2);y/=2;
        C.PB(z%2);z/=2;
    }
    reverse(ALL(A));
    reverse(ALL(B));
    reverse(ALL(C));
    dp[0][1][1][1]=1;
    sum[0][1][1][1]=0;
    FOR(i,0,31){
        FOR(a,0,2) FOR(b,0,2) FOR(c,0,2) {
            FOR(x,0,2) FOR(y,0,2){
                int z=x^y;
                if(a==1&&A[i]==0&&x==1)continue;
                if(b==1&&B[i]==0&&y==1)continue;
                if(c==1&&C[i]==0&&z==1)continue;
                add(dp[i+1][a&(A[i]==x)][b&(B[i]==y)][c&(C[i]==z)],dp[i][a][b][c]);
                add(sum[i+1][a&(A[i]==x)][b&(B[i]==y)][c&(C[i]==z)],sum[i][a][b][c]);
                add(sum[i+1][a&(A[i]==x)][b&(B[i]==y)][c&(C[i]==z)],mul(z<<(30-i),dp[i][a][b][c]));

            }
        }
    }
    ll res=0;
    FOR(a,0,2) FOR(b,0,2) FOR(c,0,2){
        add(res,sum[31][a][b][c]);
        add(res,dp[31][a][b][c]);
    }
    return res;
}

int main(){
    int T;
    cin>>T;
    while(T--){
        int x,y,x2,y2,k;
        cin>>x>>y>>x2>>y2>>k;
        --x;--y;--x2;--y2;--k;
        ll res=0;
        add(res,solve(x2,y2,k));
        sub(res,solve(x2,y-1,k));
        sub(res,solve(x-1,y2,k));
        add(res,solve(x-1,y-1,k));
        cout<<res<<'\n';
    }
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

#define PB push_back
#define MEM(n, val) memset(n, val, sizeof(n))
#define FOR(i, j, k) for(int i = j; i <= k; i++)
#define ROF(i, j, k) for(int i = j; i >= k; i--)
#define LL long long
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define INF 1000000000
#define MP make_pair
#define LN (node * 2)
#define RN ((node * 2) + 1)
#define MX 100010
#define SF(a) scanf("%d", &a)
#define SFF(a, b) scanf("%d %d", &a, &b)

int fx[] = {1, -1, 0, 0};
int fy[] = {0, 0, 1, -1};

int x1, y1, x2, y2, k;

LL bf()  {
    LL ans = 0;
    FOR(i, x1, x2)   {
        FOR(j, y1, y2)   {
            if((i ^ j) + 1LL <= k)    {
                ans += (LL)(i ^ j) + 1LL;
            }
        }
    }
    ///ans += (LL)(x2 - x1 + 1LL) * (LL)(y2 - y1 + 1LL);
    return ans;
}

LL a, b;
LL cnt[32][2][2][2], dp[32][2][2][2];
bool vis[32][2][2][2];

void func(int pos, bool chk1, bool chk2, bool chk3)  {
    LL cc = 0;
    LL ans = 0;
    /**
    if(pos < 0)    {
        cc = 1;
        cnt[pos][chk1][chk2][chk3] = cc;
        dp[pos][chk1][chk2][chk3] = ans;
        cout << pos << " " << chk1 << " " << chk2 << " " << chk3 << " : " << cc << " " << ans << "\n";
        return;
    }
    */
    if(vis[pos][chk1][chk2][chk3])    {
        return;
    }
    bool tchk1, tchk2, tchk3;
    ///a[pos] = 0 and b[pos] = 0 ///k[pos] = 0
    if(1)    {
        tchk1 = chk1;
        if((1 << pos) & a)    {
            tchk1 = 1;
        }
        tchk2 = chk2;
        if((1 << pos) & b)    {
            tchk2 = 1;
        }
        tchk3 = chk3;
        if((1 << pos) & k)    {
            tchk3 = 1;
        }
        if(pos - 1 < 0)    {
            cc += 1;
            ans += 0;
        }
        else    {
            func(pos - 1, tchk1, tchk2, tchk3);
            cc += cnt[pos - 1][tchk1][tchk2][tchk3];
            ans += dp[pos - 1][tchk1][tchk2][tchk3];
        }
    }
    ///a[pos] = 1 and b[pos] = 1 ///k[pos] = 0
    if((chk1 || ((1 << pos) & a)) && (chk2 || ((1 << pos) & b)))    {
        tchk1 = chk1;
        tchk2 = chk2;
        tchk3 = chk3;
        if((1 << pos) & k)    {
            tchk3 = 1;
        }

        if(pos - 1 < 0)    {
            cc += 1;
            ans += 0;
        }
        else    {
            func(pos - 1, tchk1, tchk2, tchk3);
            cc += cnt[pos - 1][tchk1][tchk2][tchk3];
            ans += dp[pos - 1][tchk1][tchk2][tchk3];
        }
    }
    ///a[pos] = 0 and b[pos] = 1 ///k[pos] = 1
    if((chk2 || ((1 << pos) & b)) && (chk3 || ((1 << pos) & k)))    {
        tchk1 = chk1;
        if((1 << pos) & a)    {
            tchk1 = 1;
        }
        tchk2 = chk2;
        tchk3 = chk3;

        if(pos - 1 < 0)    {
            cc += 1;
            ans += (1 * (1LL << pos)) + 0;
        }
        else    {
            func(pos - 1, tchk1, tchk2, tchk3);
            cc += cnt[pos - 1][tchk1][tchk2][tchk3];
            ans += (cnt[pos - 1][tchk1][tchk2][tchk3] * (1LL << pos)) + dp[pos - 1][tchk1][tchk2][tchk3];
        }
    }
    ///a[pos] = 1 and b[pos] = 0 ///k[pos] = 1
    if((chk1 || ((1 << pos) & a)) && (chk3 || ((1 << pos) & k)))    {
        tchk1 = chk1;
        tchk2 = chk2;
        if((1 << pos) & b)    {
            tchk2 = 1;
        }
        tchk3 = chk3;

        if(pos - 1 < 0)    {
            cc += 1;
            ans += (1 * (1LL << pos)) + 0;
        }
        else    {
            func(pos - 1, tchk1, tchk2, tchk3);
            cc += cnt[pos - 1][tchk1][tchk2][tchk3];
            ans += (cnt[pos - 1][tchk1][tchk2][tchk3] * (1LL << pos)) + dp[pos - 1][tchk1][tchk2][tchk3];
        }
    }
    ///cout << pos << " " << chk1 << " " << chk2 << " " << chk3 << " : " << cc << " " << ans << "\n";
    cnt[pos][chk1][chk2][chk3] = cc;
    dp[pos][chk1][chk2][chk3] = ans;
    vis[pos][chk1][chk2][chk3] = 1;
}

int main()  {
    std::ios_base::sync_with_stdio(false);
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    ///cout << (1 << 30) << "\n";///2^30
    int q;
    cin >> q;
    FOR(i, 1, q)   {
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        x1--;y1--;x2--;y2--;
        ///cout << bf() << "\n";

        k--;
        LL sum = 0;

        a = x2;b = y2;
        ///cout << "ab : " << a << " " << b << "\n";
        MEM(vis, 0);func(30, 0, 0, 0);
        sum += dp[30][0][0][0] + cnt[30][0][0][0];
        ///cout << dp[30][0][0][0] + cnt[30][0][0][0] << "\n";

        a = x2;b = y1 - 1;
        ///cout << "ab : " << a << " " << b << "\n";
        if(b >= 0)    {
            MEM(vis, 0);func(30, 0, 0, 0);
            sum -= dp[30][0][0][0] + cnt[30][0][0][0];
            ///cout << dp[30][0][0][0] + cnt[30][0][0][0] << "\n";
        }

        a = x1 - 1;b = y2;
        ///cout << "ab : " << a << " " << b << "\n";
        if(a >= 0)    {
            MEM(vis, 0);func(30, 0, 0, 0);
            sum -= dp[30][0][0][0] + cnt[30][0][0][0];
            ///cout << dp[30][0][0][0] + cnt[30][0][0][0] << "\n";
        }

        a = x1 - 1;b = y1 - 1;
        ///cout << "ab : " << a << " " << b << "\n";
        if(a >= 0 && b >= 0)    {
            MEM(vis, 0);func(30, 0, 0, 0);
            sum += dp[30][0][0][0] + cnt[30][0][0][0];
            ///cout << dp[30][0][0][0] + cnt[30][0][0][0] << "\n";
        }
        cout << sum << "\n";
    }
    return 0;
}
/**
10
1 1 4 1 1
1 1 4 1 2
1 1 4 1 5
1 1 13 10 8

1
100 104 1000000000 1000000000 2000000000
*/

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll mod = 1000*1000*1000 + 7;
int Q;
ll psum(ll x) {
    return x * (x + 1) / 2 % mod;
}
ll solve(int x, int y, int k) {
    if(x == 0 || y == 0 || k <= 0) return 0;
    if(x > y) swap(x, y);
    int p;
    for(int i = 0; i < 32; i++) {
        if((1 << i) > x) {
            p = i - 1;
            break;
        }
    }
    int q;
    for(int i = 0; i < 32; i++) {
        if((1 << i) > y) {
            q = i - 1;
            break;
        }
    }
    ll ret = solve(x - (1 << p), y - (1 << q), k);
    ret += 1LL * (1 << p) * psum(min((1 << q), k)) % mod; ret %= mod;
    ret += 1LL * (x - (1 << p)) * psum(max(0, min((1 << p), k - (1 << p)))) % mod; ret %= mod;
    ret += 1LL * (1 << p) * (x - (1 << p)) % mod * max(0, min((1 << p), k - (1 << p))) % mod; ret %= mod;
    ret += 1LL * (y - (1 << q)) * psum(max(0, min((1 << q), k - (1 << q)))) % mod; ret %= mod;
    ret += 1LL * (1 << q) * (y - (1 << q)) % mod * max(0, min((1 << q), k - (1 << q))) % mod; ret %= mod;
    return ret;
}
int main() {
    scanf("%d", &Q);
    while(Q--) {
        int x1, y1, x2, y2, k; scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
        ll ans = solve(x2, y2, k);
        ans -= solve(x1 - 1, y2, k); ans = (ans % mod + mod) % mod;
        ans -= solve(x2, y1 - 1, k); ans = (ans % mod + mod) % mod;
        ans += solve(x1 - 1, y1 - 1, k); ans %= mod;
        printf("%lld\n", ans);
    }
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#define IL inline
#define M 33
#define REP(a,b,c) for(a=b;a<=c;a++)
using namespace std;
typedef long long lol;
const int mod=1e9+7,lim=31;
int q,K[M],A[M],B[M],tk,ta,tb,ans;
int f[M][2][2][2],g[M][2][2][2];
IL int rd(){
	int res=0;char c;while((c=getchar())<'0'||c>'9');
	while(c>='0'&&c<='9')res=res*10+c-'0',c=getchar();return res;
}
IL void inc(int &x,int y){x=(x+y)%mod;}
IL int solve(int n,int m){
	if(n<0||m<0)return 0;int i,j,a,b,x,y,z,r;
	memset(A,0,sizeof(A)),ta=0;
	for(i=n;i;)A[++ta]=i&1,i>>=1;
	memset(B,0,sizeof(B)),tb=0;
	for(i=m;i;)B[++tb]=i&1,i>>=1;
	memset(f,0,sizeof(f)),memset(g,0,sizeof(g));
	g[1][0][0][0]=1;
	REP(i,1,lim){
		REP(j,0,1)
			REP(a,0,1)
			REP(b,0,1)
			if((z=g[i][j][a][b])){
				REP(x,0,1)
					REP(y,0,1){
					r=x^y;
					inc(f[i+1][j+r>K[i]][a+x>A[i]][b+y>B[i]],((1LL<<(i-1))*z*r+f[i][j][a][b])%mod);
					inc(g[i+1][j+r>K[i]][a+x>A[i]][b+y>B[i]],z);
				}
			}
	}
	return (f[lim+1][0][0][0]+g[lim+1][0][0][0])%mod;
}
int main(){
	q=rd();int x1,y1,x2,y2,k;
	while(q--){
		x1=rd()-1,y1=rd()-1,x2=rd()-1,y2=rd()-1,k=rd()-1;
		memset(K,0,sizeof(K));tk=0;
		while(k)K[++tk]=k&1,k>>=1;
		ans=(solve(x2,y2)+solve(x1-1,y1-1)-solve(x1-1,y2)-solve(x2,y1-1))%mod;
		printf("%d\n",ans);
	}
  return 0;
}

/*
    Author: zxy_hhhh
    date: 2020/02/24
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<iostream>
#define ll long long
inline ll rd() {
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define bit(x,y) ((bool)(x&(1<<y)))
int q;
std::pair<ll,ll> dp[31];
std::pair<ll,ll> dfs(int pos,int A,int B,int C) {
    if (pos<0) return std::make_pair(0,1);
    if (A==-1&&B==-1&&C==-1&&dp[pos].second)
        return dp[pos];
    std::pair<ll,ll> ans=std::make_pair(0,0);
    for(int a=0;a<=bit(A,pos);a++) {
        for(int b=0;b<=bit(B,pos);b++) {
            int aa=a<bit(A,pos)?-1:A;
            int bb=b<bit(B,pos)?-1:B;
            if ((a^b)>bit(C,pos)) continue;
            int cc=(a^b)<bit(C,pos)?-1:C;
            std::pair<ll,ll> x=dfs(pos-1,aa,bb,cc);
            x.first+=x.second*((a^b)<<pos);
            ans.first+=x.first,ans.second+=x.second;
        }
    }
    if (A==-1&&B==-1&&C==-1) dp[pos]=ans;
    return ans;
}
inline ll add(std::pair<ll,ll> A) {
    return A.first+A.second;
}
int main() {
    #ifndef ONLINE_JUDGE
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
    #endif
    q=rd();
    while(q--) {
        int x=rd()-1,y=rd()-1,xx=rd()-1,yy=rd()-1;
        int K=rd()-1;
        ll r1=0,r2=0,r3=0,r4=0;
        if (x>0&&y>0)
            r1=add(dfs(30,x-1,y-1,K));
        if (x>0)
            r2=add(dfs(30,x-1,yy,K));
        if (y>0)
            r3=add(dfs(30,xx,y-1,K));
        r4=add(dfs(30,xx,yy,K));
        wrt(r4-r2-r3+r1,'\n');
    }
}

#include<bits/stdc++.h>
#define pp pair<int,int>
#define MZ 1000000007
using namespace std;
long long k;
int numx[35],numy[35],numk[35];
long long sum[35][2][2][2],num[35][2][2][2];
long long ask(long long x,long long y)
{
    long long ans=0;
    long long z=k;
    if(x<0 || y<0) return 0;
    memset(sum,0,sizeof(sum));
    memset(num,0,sizeof(num));
    for(int i=0;i<30;i++)
    {
        numx[i]=x%2;
        numy[i]=y%2;
        numk[i]=z%2;
        x>>=1;
        y>>=1;
        z>>=1;
    }
    num[30][1][1][1]=1;
    sum[30][1][1][1]=0;
    int im,jm;
    for(int lens=30;lens>=0;lens--)
        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                for(int kk=0;kk<2;kk++)
                if(num[lens][i][j][kk])
                {
                    if(lens==0)
                    {
                        ans=(ans+num[lens][i][j][kk]+sum[lens][i][j][kk])%MZ;
                        continue;
                    }
                    im=jm=1;
                    if(i)
                        im=numx[lens-1];
                    if(j)
                        jm=numy[lens-1];
                    for(int nowi=0;nowi<=im;nowi++)
                        for(int nowj=0;nowj<=jm;nowj++)
                        {
                            if(!kk || (nowi^nowj)<=numk[lens-1])
                            {
                                num[lens-1][nowi==im && i][nowj==jm && j][(nowi^nowj==numk[lens-1]) && kk]=(num[lens-1][nowi==im && i][nowj==jm && j][(nowi^nowj==numk[lens-1]) && kk]+num[lens][i][j][kk])%MZ;
                                sum[lens-1][nowi==im && i][nowj==jm && j][(nowi^nowj==numk[lens-1]) && kk]=(sum[lens-1][nowi==im && i][nowj==jm && j][(nowi^nowj==numk[lens-1]) && kk]+(sum[lens][i][j][kk]<<1)+(nowi^nowj)*num[lens][i][j][kk])%MZ;
                            }
                        }
                }
    return ans;
}
int main()
{
    int q;
    long long x1,y1,x2,y2;
    scanf("%d",&q);
    while(q--)
    {
        scanf("%lld%lld%lld%lld%lld",&x1,&y1,&x2,&y2,&k);
        x1--;
        y1--;
        x2--;
        y2--;
        k--;
        printf("%lld\n",(ask(x2,y2)-ask(x2,y1-1)-ask(x1-1,y2)+ask(x1-1,y1-1)+MZ)%MZ);

    }
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <fstream>
#include <bitset>
#include <time.h>
#define int long long
using namespace std;
int q, x1, x2, yf, y2, kk;
int CONST = 1000000007;
int get_answer(int k, int wanted, int x, int y, int pl){
    if (x < 1 || y < 1 || wanted < 1) return 0;
    if (k == 1){
        if (wanted >= 1) return (1 + pl) % CONST;
        return 0;
    }
    else{
        if (x == k){
            int counter = min(wanted, k);
            int go = (((y * counter) % CONST) * pl) % CONST;
            int may = (counter * (counter + 1) / 2) % CONST;
            return ((may * y) % CONST + go) % CONST;
        }
        if (y == k){
            int counter = min(wanted, k);
            int go = (((x * counter) % CONST) * pl) % CONST;
            int may = (counter * (counter + 1) / 2) % CONST;
            return ((may * x) % CONST + go) % CONST;
        }
        int get = 0;
        get += get_answer(k/2, wanted, min(x, k/2), min(y, k/2), pl);
        get += get_answer(k/2, wanted, x-k/2, y-k/2, pl);
        get += get_answer(k/2, wanted-k/2, x-k/2, min(y, k/2), pl+k/2);
        get += get_answer(k/2, wanted-k/2, min(x, k/2), y-k/2, pl+k/2);
        get %= CONST;
        return get;
    }
}
int32_t main(){
    cin >> q;
    int st = 4294967296;
    for (int i=0;i<q;i++){
        cin >> x1 >> yf >> x2 >> y2 >> kk;
        int aa = 0;
        aa += get_answer(st, kk, x2, y2, 0);
        aa -= get_answer(st, kk, x1-1, y2, 0);
        aa -= get_answer(st, kk, x2, yf-1, 0);
        aa += get_answer(st, kk, x1-1, yf-1, 0);
        aa += CONST;
        aa %= CONST;
        cout << aa << endl;
    }
    return 0;
}

# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) ((x)&1)
#define sqr(x) ((x)*(x))
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define rep(i,a,b) for (int i=(a); i<=(b); ++i)
#define rep_it(it,x) for (__typeof((x).begin()) it=(x).begin(); it!=(x).end(); it++)
#define ____ puts("\n_______________\n\n")
#define debug(x) ____; cout<< #x << " => " << (x) << endl
#define debug_pair(x) cout<<"\n{ "<<(x).fir<<" , "<<(x).sec<<" }\n"
#define debug_arr(x,n) ____; cout<<#x<<":\n"; rep(i,0,n) cout<<#x<<"["<<(i)<<"] => "<<x[i]<<endl
#define debug_set(x) ____; cout<<#x<<": \n"; rep_it(it,x) cout<<(*it)<<" "; cout<<endl
#define debug_map(x) ____; cout<<#x<<": \n"; rep_it(it,x) debug_pair(*it)
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
const int oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const double PI=acos(-1);
const double eps=1e-9;
int o(int x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

void add(ll &x,ll k) {
	x+=k;
	x%=_p;
}

ll x1,y1,x2,y2,k,ans=0;
int q;

ll calc(ll x,ll y,ll k,ll d=0) {
	if (x>y) swap(x,y);
	if (x<=0 || k<=0) return 0;
	ll p=(1ll<<((int)log2(y))),ans=0,t=min(p,k);
	if (x<=p) {
		ans=(1+t)*t/2%_p*x%_p;
		add(ans,(t*x%_p*d%_p+calc(x,y-p,k-p,d+p))%_p);
		return ans;
	}
	ans=((1+t)*t/2%_p*p%_p+p*t%_p*d%_p)%_p;
	add(ans,calc(x-p,y-p,k,d));
	add(ans,calc(x-p,p,k-p,d+p));
	add(ans,calc(p,y-p,k-p,d+p));
	return ans;
}

int main() {
    //file_put();

	scanf("%d",&q);
	while (q--) {
		scanf("%I64d%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2,&k);
		ans=calc(x2,y2,k);
		add(ans,calc(x1-1,y1-1,k));
		add(ans,-calc(x2,y1-1,k));
		add(ans,-calc(x1-1,y2,k));
		printf("%I64d\n",ans);
	}

    return 0;
}

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mod = 10e9+7;

inline void add(ll &a, ll b)
{
    a += b;
    if(a > mod)
        a -= mod;
}

inline void sub(ll &a, ll b)
{
    a -= b;
    if(a < 0)
        a += mod;
}

inline ll mul(ll a, ll b)
{
    return (a*b)%mod;
}

inline int dig(int a, int i)
{
    return 1 & (a >> (31-i));
}
ll solve(int x, int y, int k)
{
    ll ct[32][2][2][2], sum[32][2][2][2];
    memset(ct, 0, sizeof ct);
    memset(sum, 0, sizeof sum);
    if(x < 0 || y < 0 || k < 0)
        return 0;
    ct[0][1][1][1] = 1;
    for(int i = 0; i < 31; i++)    {
        for(int xx = 0; xx < 2; xx++)
            for(int yy = 0; yy < 2; yy++)   {
                int kk = xx ^ yy;
                for(int fx = 0; fx < 2; fx++)
                    for(int fy = 0; fy < 2; fy++)
                        for(int fk = 0; fk < 2; fk++)   {
                            if(fx && xx>dig(x,i+1)) continue;
                            if(fy && yy>dig(y,i+1)) continue;
                            if(fk && kk>dig(k,i+1)) continue;
                            add(ct[i+1][fx&(xx==dig(x,i+1))][fy&(yy==dig(y,i+1))][fk&(kk==dig(k,i+1))], ct[i][fx][fy][fk]);
                            add(sum[i+1][fx&(xx==dig(x,i+1))][fy&(yy==dig(y,i+1))][fk&(kk==dig(k,i+1))], sum[i][fx][fy][fk]);
                            add(sum[i+1][fx&(xx==dig(x,i+1))][fy&(yy==dig(y,i+1))][fk&(kk==dig(k,i+1))], mul(kk<<(30-i), ct[i][fx][fy][fk]));
                        }
            }

    }
    ll ans = 0;
    for(int xx = 0; xx < 2; xx++)
        for(int yy = 0; yy < 2; yy++)
            for(int kk = 0; kk < 2; kk++)   {
                add(ans, ct[31][xx][yy][kk]);
                add(ans, sum[31][xx][yy][kk]);
            }
    return ans;
}

int main()
{
    int q;
    cin>>q;
    while(q--)  {
        int x, y, x2, y2, k;
        cin>>x>>y>>x2>>y2>>k;
        x--;y--;x2--;y2--;k--;
        ll res = 0;
        add(res,solve(x2,y2,k));
        sub(res,solve(x2,y-1,k));
        sub(res,solve(x-1,y2,k));
        add(res,solve(x-1,y-1,k));
        cout<<res<<'\n';
    }
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef complex<ld> cd;

const ll mod = 1000000007;

ll large(ll x){
    for(int i = 30; i >= 0; i--){
        if((x>>i)&1)return (1LL<<i);
    }
}

ll comp(ll x, ll k){
    ll ret = k*(k+1)/2;
    ret %= mod;
    ret *= x;
    return ret%mod;
}

ll func(ll x, ll y, ll k, ll off = 0){
    if(!x || !y)return 0;
    if(x > y)return func(y, x, k, off);
    ll m = large(y);
    if(k <= m){
        ll ret = (off*x%mod)*k%mod;
        ret += comp(x, k);
//         cout<<x<<" "<<y<<" "<<k<<" "<<off<<" "<<m<<" ";
//         cout<<ret<<" "<<1<<endl;
        return ret;
    }
    if(x <= m){
        ll ret = func(x, y - m, k - m, off^m);
        ret += comp(x, m);
        ret += (off*x%mod)*m%mod;
        ret %= mod;
//         cout<<x<<" "<<y<<" "<<k<<" "<<off<<" "<<m<<" ";
//         cout<<ret<<endl;
        return ret;
    }
    ll ret;
    if(off&m)ret = func(m, y - m, k + m, off - m);
    else ret = func(m, y - m, k - m, off + m);

    ret += func(x - m, y - m, k, off);

    ret += comp(m, m);
    ret += (off*m%mod)*m%mod;

    ll off2 = off^m;
    ret += comp(x-m, min(m, k-m));
    ret += (off2*(x-m)%mod)*min(m, k-m)%mod;
    ret %= mod;

//     cout<<x<<" "<<y<<" "<<k<<" "<<off<<" "<<m<<" "<<off2<<" "<<comp(x-m,min(m,k-m))<<" "<<(off2*(x-m)%mod)*min(m, k-m)%mod<<" ";
//     cout<<ret<<endl;
    return ret;
}

inline ll query(ll x1, ll y1, ll x2, ll y2, ll k){
    ll ret = func(x2, y2, k) - func(x1 - 1, y2, k) - func(x2, y1 - 1, k) + func(x1 - 1, y1 - 1, k) + mod + mod;
    return ret%mod;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cout<<setprecision(30);

//     for(int i = 1; i <= 20; i++){
//         for(int j = 1; j <= 20; j++){
//             cout<<query(i, j, i, j, 1000)<<" ";
//         }
//         cout<<endl;
//     }

    int q;
    ll x1, x2, y1, y2, k;
    cin>>q;
    while(q--){
        cin>>x1>>y1>>x2>>y2>>k;
        cout<<query(x1, y1, x2, y2, k)<<endl;
    }

}

#include<algorithm>
#include<stdio.h>
using namespace std;
const int mod=1e9+7;
int UP(int x) //二进制最高位
{
	int l=0,r=32;
	int ans=0;
	while (l<=r){
		int mid=(l+r)>>1;
		if (!(x>>mid)){
			ans=mid;
			r=mid-1;
		}else l=mid+1;
	}
	return ans;
}
int qury(int x,int y,int k,int va)
{
	if (x==0||y==0)return 0;
	int up1=UP(x)-1;
	int up2=UP(y)-1;
	int ret=0;
	int n=min(k,max(1<<up1,1<<up2));
		ret=((1LL*n*(n+1)/2)%mod*min(1<<up1,1<<up2))%mod;
		ret=(ret+(n*min(1<<up1,1<<up2)%mod*va%mod))%mod;
		if (k-(1<<up1)>0)	ret=(ret+qury(x-(1<<up1),1<<up2,k-(1<<up1),va+(1<<up1)))%mod;
		if (k-(1<<up2)>0)	ret=(ret+qury(1<<up1,y-(1<<up2),k-(1<<up2),va+(1<<up2)))%mod;
	ret=(ret+qury(x-(1<<up1),y-(1<<up2),k,va))%mod;
	return ret;
}
main()
{
	int x1,y1,x2,y2;
	int k;
	int n;
	scanf("%d",&n);
	while (n--){
		scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
		int q1=qury(x1-1,y1-1,k,0);
		int q2=qury(x2,y1-1,k,0);
		int q3=qury(x1-1,y2,k,0);
		int q4=qury(x2,y2,k,0);
		//printf("%d_%d_%d_%d ",q1,q2,q3,q4);
		int ans=(((q4+q1)%mod-(q2+q3)%mod)%mod+mod)%mod;
		printf("%d\n",ans);
	}
}

#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;

int pot [40];

struct ans{
    int n, s;
    void add( ans val ){
        n += val.n;
        n %= mod;
        s += val.s;
        s %= mod;
    }
    void mult ( ans val , int i ){
        n += val.n;
        n %= mod;
        s += (val.s + val.n * pot[i]);
        s %= mod;
        //cout << " mult " << endl ;
    }
};

ans mem [35][2][2][2];
int vis [35][2][2][2];
int h, w, k, cont = 0;

int is_one( int x, int i ) {
    return (x>>i)&1;
}

ans dp ( int i , int same_h , int same_w , int same_k ){
    if( i == -1 ){
        ans r = ans{1,0};
        return r;
    }
    ans &ret = mem [i][same_h][same_w][same_k];
    if( vis[i][same_h][same_w][same_k] == cont ){
        return ret;
    }
    ret.n = ret.s = 0;
    /// Colocar 0 en x y 0 en y
    ret.add( dp( i-1 , same_h & (1^is_one(h,i)), same_w & (1^is_one(w,i)), same_k & (1^is_one(k,i)) ) );
    ///Colocar ( 1 , 1 )
    if( (!same_h or is_one(h,i)) and (!same_w or is_one(w,i)) ){
        ret.add( dp( i-1 , same_h & is_one(h,i) , same_w & is_one(w,i) , same_k & (1^is_one(k,i)) ) );
    }
    ///Colocar ( 1 , 0 )
    if( (!same_h or is_one(h,i)) and ( !same_k or is_one(k,i) ) ){
        ret.mult( dp( i-1 , same_h & is_one(h,i) , same_w & (1^is_one(w,i)) , same_k & is_one(k,i) ), i );
    }
    ///Colocar ( 0 , 1 )
    if( (!same_w or is_one(w,i)) and ( !same_k or is_one(k,i) ) ){
        ret.mult( dp( i-1 , same_h & (1^is_one(h,i)) , same_w & is_one(w,i) , same_k & is_one(k,i) ), i );
    }
    vis [i][same_h][same_w][same_k] = cont;
    return ret;
}

int count ( int x, int y ) {
    if ( x < 0 || y < 0 ) return 0;
    h = x;
    w = y;
    ans a = dp( 31 , 1 , 1 , 1 );
    return (  a.s + a.n ) % mod;
}

int main(){
    pot[0] = 1;
    for( int i = 1 ; i < 35 ; i ++ )
        pot[i] = (2*pot[i-1]) % mod ;

    int q, x1, y1, x2, y2, res;
    scanf("%d",&q);
    for( int i = 0 ; i < q ; i ++ ){
        scanf( "%d%d%d%d%d" , &x1 , &y1 , &x2 , &y2 , &k );
        x1--;
        x2--;
        y2--;
        y1--;
        k--;
        cont ++;
        res = count(x2,y2) - count(x2,y1-1) - count(x1-1,y2) + count(x1-1,y1-1);
        res = ( res % mod + mod ) % mod;
        printf("%d\n",res);
    }
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

const int DMAX = 31, MOD = 1000000007;

int Q;

int sum(int l, int r)
{
	return (long long)(l + r) * (r - l + 1) / 2 % MOD;
}

int clearLowBit(int x, int d)
{
	if(d < 0)
		return x;
	return x & ((~0u>>1) ^ ((1<<(d + 1)) - 1));
}

int fillLowBit(int x, int d)
{
	if(d < 0)
		return x;
	return (x | ((1<<(d + 1)) - 1));
}

int trucLowBit(int x, int d)
{
	if(d < 0)
		return x;
	return x & ~((1<<(d + 1)) - 1);
}

int solve(int n, int m, int k)
{
	int i, j, d, f, res = 0;
	int x, y, pre, factor;
	if(n < 0 || m < 0)
		return 0;
	for(i = -1;i < DMAX;i += 1)
	{
		if(i >= 0 && !(n & (1<<i)))
			continue;
		for(j = -1;j < DMAX;j += 1)
		{
			if(j >= 0 && !(m & (1<<j)))
				continue;
			d = max(i, j);
			f = max(0, min(i, j));
			x = clearLowBit(n, i);
			y = clearLowBit(m, j);
			pre = trucLowBit(x ^ y, d - 1);
			if(pre > k)
				continue;
			if(clearLowBit(k, d - 1) == pre)
				factor = sum(pre + 1, k + 1) % MOD;
			else
				factor = sum(pre + 1, fillLowBit(pre, d - 1) + 1) % MOD;
			res = (res + (long long)(1<<f) * factor) % MOD;
		}
	}
	return res;
}

int main()
{
	int i, x1, y1, x2, y2, k, res;
	scanf("%d", &Q);
	for(i = 1;i <= Q;i += 1)
	{
		scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
		x1 -= 1;
		y1 -= 1;
		x2 -= 1;
		y2 -= 1;
		k -= 1;
		res = solve(x2, y2, k);
		res = (res + solve(x1 - 1, y1 - 1, k) + MOD) % MOD;
		res = (res - solve(x1 - 1, y2, k) + MOD) % MOD;
		res = (res - solve(x2, y1 - 1, k) + MOD) % MOD;
		printf("%d\n", res);
	}
	exit(0);
}

#include <bits/stdc++.h>
using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
#define db(x) cerr << #x << " = " << (x) << ", ";
#define endln cerr << "\n";
#define chkpt cerr << "-----\n";

template<class T, class mul, class dvd, class mod> struct FiniteField {
    T x;
    template<class G> T normalize(G x) {
        T p = mod().get();
        if (x >= 0 && x < p) return x;
        x %= p;
        if (x < 0) x += p;
        else if (x >= p) x -= p;
        return x;
    }
    FiniteField() : x(0) {}
    template<class G> FiniteField(const G& y) {
        x = normalize(y);
    }
    FiniteField(const FiniteField& rhs) : x(rhs.x) {
    }
    template<class G> FiniteField operator = (const G& y) {
        x = normalize(y);
        return *this;
    }
    int operator == (const FiniteField& rhs) const {
        return x == rhs.x;
    }
    int operator != (const FiniteField& rhs) const {
        return x != rhs.x;
    }
    int operator < (const FiniteField& rhs) const {
        return x < rhs.x;
    }
    FiniteField operator ++(int) {
        T p = mod().get();
        FiniteField res = *this;
        if ((++x) >= p) x -= p;
        return res;
    }
    FiniteField operator ++() {
        T p = mod().get();
        if ((++x) >= p) x -= p;
        return *this;
    }
    FiniteField operator --(int) {
        T p = mod().get();
        FiniteField res = *this;
        if ((--x) < 0) x += p;
        return res;
    }
    FiniteField operator --() {
        T p = mod().get();
        if ((--x) < 0) x += p;
        return *this;
    }
    template<class G> FiniteField operator += (const G& y) {
        T p = mod().get();
        if ((x += normalize(y)) >= p) x -= p;
        return *this;
    }
    template<class G> FiniteField operator -= (const G& y) {
        T p = mod().get();
        if ((x -= normalize(y)) < 0) x += p;
        return *this;
    }
    FiniteField operator += (const FiniteField& rhs) {
        T p = mod().get();
        if ((x += rhs.x) >= p) x -= p;
        return *this;
    }
    FiniteField operator -= (const FiniteField& rhs) {
        T p = mod().get();
        if ((x -= rhs.x) < 0) x += p;
        return *this;
    }
    FiniteField operator + (const FiniteField& rhs) const {
        T p = mod().get();
        FiniteField res;
        res.x = x + rhs.x;
        if (res.x >= p) res.x -= p;
        return res;
    }
    FiniteField operator - (const FiniteField& rhs) const {
        T p = mod().get();
        FiniteField res;
        res.x = x - rhs.x;
        if (res.x < 0) res.x += p;
        return res;
    }
    template<class G> FiniteField operator *= (const G& y) {
        T p = mod().get();
        x = mul()(x, normalize(y), p);
        return *this;
    }
    template<class G> FiniteField operator /= (const G& y) {
        T p = mod().get();
        x = dvd()(x, normalize(y), p);
        return *this;
    }
    FiniteField operator *= (const FiniteField& rhs) {
        T p = mod().get();
        x = mul()(x, rhs.x, p);
        return *this;
    }
    FiniteField operator /= (const FiniteField& rhs) {
        T p = mod().get();
        x = dvd()(x, rhs.x, p);
        return *this;
    }
    template<class G> FiniteField operator * (const G& y) const {
        T p = mod().get();
        FiniteField res;
        res.x = mul()(x, res.normalize(y), p);
        return res;
    }
    template<class G> FiniteField operator / (const G& y) const {
        T p = mod().get();
        FiniteField res;
        res.x = dvd()(x, res.normalize(y), p);
        return res;
    }
    FiniteField operator * (const FiniteField& rhs) const {
        T p = mod().get();
        FiniteField res;
        res.x = mul()(x, rhs.x, p);
        return res;
    }
    FiniteField operator / (const FiniteField& rhs) const {
        T p = mod().get();
        FiniteField res;
        res.x = dvd()(x, rhs.x, p);
        return res;
    }
    template<class G> friend FiniteField operator + (const G& y, const FiniteField& rhs) {
        FiniteField res;
        res.x = res.normalize(y);
        return res + rhs;
    }
    template<class G> friend FiniteField operator - (const G& y, const FiniteField& rhs) {
        FiniteField res;
        res.x = res.normalize(y);
        return res - rhs;
    }
    template<class G> friend FiniteField operator * (const G& y, const FiniteField& rhs) {
        FiniteField res;
        res.x = res.normalize(y);
        return res * rhs;
    }
    template<class G> friend FiniteField operator / (const G& y, const FiniteField& rhs) {
        FiniteField res;
        res.x = res.normalize(y);
        return res / rhs;
    }
    template<class G> FiniteField operator ^= (G k) {
        T p = mod().get();
        if (k == 0) {
            x = 1;
            return *this;
        }
        T t = x; k--;
        while (k > 0) {
            if (k & 1) x = mul()(x, t, p);
            t = mul()(t, t, p);
            k >>= 1;
        }
        return *this;
    }
    template<class G> FiniteField operator ^ (G k) const {
        T p = mod().get();
        if (k == 0) return 1;
        T r = x, t = x; k--;
        while (k > 0) {
            if (k & 1) r = mul()(r, t, p);
            t = mul()(t, t, p);
            k >>= 1;
        }
        return r;
    }
    friend istream& operator >> (istream& is, FiniteField& rhs) {
        is >> rhs.x;
        rhs.x = rhs.normalize(rhs.x);
        return is;
    }
    friend ostream& operator << (ostream& os, const FiniteField& rhs) {
        os << rhs.x;
        return os;
    }
};
template<class T> class mul {
public:
    T operator () (const T& x, const T& y, const T& p) {
        return (long long) x * y % p;
    }
};
template<class T> class dvd {
public:
    T operator () (const T& x, const T& y, const T& p) {
        pair<T, T> r = euclid(y, p);
        return mul<T>()(x, (r.first % p + p) % p, p);
    }
private:
    pair<T, T> euclid(T a, T b) {
        if (b == 0) return make_pair(1, 0);
        pair<T, T> r = euclid(b, a % b);
        return make_pair(r.second, r.first - a / b * r.second);
    }
};
template<class T> class mod {
public:
    T get() {
        return (T) 1e9 + 7;
    }
};
typedef FiniteField<int, mul<int>, dvd<int>, mod<int> > FF;
FF iv2 = FF(1) / 2;

FF get(int sx, int sy, int x, int y, int mx) {
    int v = ((sx - 1) ^ (sy - 1)) + 1;
    int tmx = mx;
    if (!x || !y) return 0;
    if (mx < v) return 0;
    if (x > y) return get(sy, sx, y, x, tmx);
    int k = __lg(x);
    int d = 1 << k;
    if (x == d && y == d) {
        chkmin(mx, v + d - 1);
        return FF(d) * (v + mx) * (mx - v + 1) * iv2;
    }
    int cy = (y - 1) / d;
    FF res = 0;
    if (cy) {
        chkmin(mx, v + cy * d - 1);
        res += FF(d) * (v + mx) * (mx - v + 1) * iv2;
    }
    int nx = x % d;
    int ny = y % d;
    if (!nx) nx = d;
    if (!ny) ny = d;
    res += get(sx + x - nx, sy + y - ny, nx, ny, tmx);
    if (nx < d) {
        res += get(sx, sy + y - ny, x - nx, ny, tmx);
        int p = (y - 1) / d;
        res += p / 2 * get(sx, sy, nx, d + d, tmx);
        if (p & 1) {
            res += get(sx + x - nx, sy + p / 2 * 2 * d, nx, d, tmx);
        }
    }
    return res;
}

void solve() {
    int q; cin >> q;
    while (q--) {
        int x[2], y[2], k;
        FOR(i, 0, 2) cin >> x[i] >> y[i];
        cin >> k;
        FF res = 0;
        res += get(1, 1, x[1], y[1], k);
        res -= get(1, 1, x[0] - 1, y[1], k);
        res -= get(1, 1, x[1], y[0] - 1, k);
        res += get(1, 1, x[0] - 1, y[0] - 1, k);
        cout << res << "\n";
    }
}

int main() {
    int JUDGE_ONLINE = 1;
    if (fopen("in.txt", "r")) {
        JUDGE_ONLINE = 0;
        assert(freopen("in.txt", "r", stdin));
        //assert(freopen("out.txt", "w", stdout));
    }
    else {
        //ios_base::sync_with_stdio(0), cin.tie(0);
    }
    solve();
    if (!JUDGE_ONLINE) {
        //cout << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <deque>
#include <queue>
#include <cstring>
#include <set>
#include <list>
#include <map>
#include <random>
#include <unordered_map>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;

const long long mod = 1000000007;

#define all(c) (c).begin(),(c).end()
#define forn(i, a, b) for(int i = a; i < b; i++)

#define pb push_back
#define mp make_pair

pll req(ll x, ll y, ll k) {
    if(k<1) return mp(0,0);
    if(x == 0 || y == 0) return mp(0,0);
    if(x == 1 && y == 1) {
        if(k >= 1) return mp(1,1);
        else return mp(0,0);
    }
    ll deg = 0;
    ll d2 = 1;
    while(max(x,y) > d2) {
        deg++;
        d2*=2;
    }
    ll a = d2/2;
    if(x>y) swap(x,y);
    if(x<=a) {
        ll m = min(a,k);
        ll sm = ((m*(m+1))/2) % mod;
        pll res = mp(x*m, x*sm);
        pll res2 = req(x, y-a, k-a);
        res2.second += res2.first*a;
        res.first += res2.first;
        res.second = (res.second + res2.second)%mod;
        return res;
    }
    ll m = min(a,k);
    ll sm = ((m*(m+1))/2) % mod;
    pll res = mp(a*m, a*sm);
    ll m2 = min(a, k-a);
    ll sm2 = (a*m2+(m2*(m2+1))/2) % mod;
    res.first += (x+y-2*a)*m2;
    res.second += (x+y-2*a)*sm2;
    pll res2 = req(x-a, y-a, k);
    res.first += res2.first;
    res.second = (res.second + res2.second)%mod;
    return res;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    int q;
    scanf("%d", &q);
    forn(i,0,q) {
        int x1,y1,x2,y2,k;
        scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
        printf("%lld\n", (req(x2,y2,k).second - req(x1-1,y2,k).second - req(x2,y1-1,k).second + req(x1-1,y1-1,k).second + 10*mod)%mod);
    }

}

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head

#define y1 afasdfads
//const int N=16;
//int a[110][110];
int _,x1,y1,x2,y2,k;
ll cret;
ll solve(ll x,ll y,ll k) {
	--k;
	if (x<=0||y<=0) return 0;
	vector<ll> dx,dy;
	ll ret=0;
	dx.pb(x);
	while (x) x-=x&-x,dx.pb(x);
	dy.pb(y);
	while (y) y-=y&-y,dy.pb(y);
	rep(i,0,SZ(dx)-1) rep(j,0,SZ(dy)-1) {
		int v=max(__builtin_ctzll(dx[i]),__builtin_ctzll(dy[j]));
		ll prex=dx[i+1]^dy[j+1];
		ll cnt=((dx[i]-dx[i+1])*(dy[j]-dy[j+1])>>v)%mod;
		ll b=prex,e=min(prex+(1<<v)-1,k);
		if (b<=e) ret+=(b+e+2)*(e-b+1)/2%mod*cnt%mod;
	}
	return ret;
}
int main() {
/*	rep(i,1,N) {
		rep(j,1,N) {
			set<int> s;
			rep(k,1,i) s.insert(a[k][j]);
			rep(k,1,j) s.insert(a[i][k]); a[i][j]=1;
			while (s.count(a[i][j])) a[i][j]++;
			printf("%d ",a[i][j]);
		}
		puts("");
	}*/
	for (scanf("%d",&_);_;_--) {
		scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
		cret=solve(x2,y2,k)+solve(x1-1,y1-1,k)-solve(x2,y1-1,k)-solve(x1-1,y2,k);
		cret%=mod;
		if (cret<0) cret+=mod;
		printf("%lld\n",cret);
	}
}

#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007
#define LL long long

LL qpow(LL x,LL y){
    LL e=1;
    while(y){
        if(y%2)e=(e*x)%MOD;
        x=x*x%MOD;
        y/=2;
    }
    return e;
}

LL low(LL x){

    for(LL i=1;i<=x;i<<=1){
        if(i<<1>x)
            return i;
    }
}

LL getS1(LL x,LL n){
    LL a1=(1+x)*x%MOD*qpow(2,MOD-2)%MOD*x%MOD;
    LL an=(a1+(x*x%MOD)*((n-1)*x%MOD))%MOD;
    return (a1+an)*n%MOD*qpow(2,MOD-2)%MOD;
}

LL getS2(LL x,LL y,LL n){
    return (y-x+1)*(x+y)%MOD*qpow(2,MOD-2)%MOD*n%MOD;
}

LL getS3(LL x,LL n){
    return n*(x+x+n-1)%MOD*qpow(2,MOD-2)*n%MOD;
}

LL dfs(int a,int b,int k){

    if(a==0||b==0) return 0;

    LL l=min(a,b);
    LL r=max(a,b);

    LL x=low(l);

    LL r1=r/x;
    LL r2=r%x;

    LL ans=0;

    if(k<=r){
        r1=k/x;
        ans=(ans+getS3(k-k%x+1,k%x))%MOD;
    }
    else{
        ans=(ans+getS2(r-r%x+1,r-r%x+x,r%x))%MOD;
    }

    if(k<=l){
        ans=(ans+getS3(k-k%x+1,k%x))%MOD;
    }
    else{
        ans=(ans+getS2(l-l%x+1,l-l%x+x,l%x))%MOD;
    }

    ans=(ans+getS1(x,r1))%MOD;

    return ans+dfs(l-x,r-x,k);
}

int main() {

    int N,x1,x2,y1,y2,k;
    scanf("%d", &N);
    for(int i=1;i<=N;i++){
        scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
        LL ans=dfs(x2,y2,k)-dfs(x1-1,y2,k)-dfs(x2,y1-1,k)+dfs(x1-1,y1-1,k);
        printf("%lld\n",ans%MOD);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = (1e9)+7;

pair<ll, ll> query(ll x, ll y, ll k, ll lim) {
	if(x <= 0 || y <= 0 || k < 0 || lim <= 0) return {0, 0};
	ll pw = 1 << k;
	ll cnt = 0, sum = 0;
	if(x >= pw || y >= pw) {
		ll val_cnt = min(pw, lim), node_cnt = min(pw, min(x, y));
		cnt = 1ll * val_cnt * node_cnt % MOD;
		sum = 1ll * (val_cnt * (val_cnt + 1) / 2 % MOD) * node_cnt;
	} else {
		pw >>= 1;
		pair<ll, ll> res[2][2] = {
			query(x, y, k-1, lim),
			query(x - pw, y, k-1, lim - pw),
			query(x, y - pw, k-1, lim - pw),
			query(x - pw, y - pw, k-1, lim)
		};
		cnt = (res[0][0].second + res[0][1].second + res[1][0].second + res[1][1].second) % MOD;
		sum = (res[0][0].first + res[0][1].first + res[1][0].first + res[1][1].first) % MOD;
		sum = (sum + (res[0][1].second + res[1][0].second) * pw) % MOD;
	}
	return {sum, cnt};
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while(q--) {
		ll x1, y1, x2, y2, lim;
		cin >> x1 >> y1 >> x2 >> y2 >> lim;
		ll k = 0;
		while(1ll << k <= max(x2, y2)) ++k;
		ll res = query(x2, y2, k, lim).first
					 - query(x1 - 1, y2, k, lim).first
					 - query(x2, y1 - 1, k, lim).first
					 + query(x1 - 1, y1 - 1, k, lim).first;
		cout << res << '\n';
	}
}

#include <bits/stdc++.h>
using namespace std;

using db = double;
using ll = long long;
using vi = vector <int>;
#define op operator
#define pb push_back

const ll mod = 1e9L + 7;

ll f(ll x, ll y, ll k, ll offset) {
	//cout << x << ' ' << y << ' ' << k << ' ' << offset << '\n';
	k -= offset;
	if(x > y) swap(x, y);
	if(k >= y) k = y;
	if(k <= 0) return 0;
	//cout << x << ' ' << y << ' ' << k << ' ' << offset << ' ' << (k * (1 + k) / 2 % mod * x % mod + x * y % mod * offset % mod) << '\n';
	return (k * (1 + k) / 2 % mod * x % mod + k * x % mod * offset % mod) % mod;
}

ll get(ll n, ll m) {
	if(n > m) swap(n, m);
	if(n == m) return 0;
	//if(n * 2 == m) return n;
	return n + m;
}

ll solve(ll x, ll y, ll k, ll offset = 0) {
	//cout << x << ' ' << y << '\n';
	//if(x && y) cout << x << ' ' << y << ' ' << k << ' ' << offset << '\n';
	if(x > y) swap(x, y);
	if(x == 0) return 0;
	ll n = 1, m = 1;
	while(n * 2 <= x) n *= 2;
	while(m * 2 <= y) m *= 2;
	//cout << n << ' ' << m << '\n';

	ll r = f(n, m, k, offset);

	if(n == m) {
		r += solve(x - n, m, k, offset + n);
		r += solve(n, y - m, k, offset + m);
		r += solve(x - n, y - m, k, offset);
	} else {

		r += solve(x - n, m, k, offset);
		r += solve(n, y - m, k, offset + m);
		r += solve(x - n, y - m, k, offset + get(n, m));
	}
	return r % mod;
}

int main() {

	//cout << solve(2, 5, 2) << '\n';

	cout << fixed << setprecision(9);
	ios :: sync_with_stdio(0);
	/*
	for(int i : {1, 2, 4})
		for(int j : {1, 2, 4}) cout << get(i, j) << '\n';
*/

	int q; cin >> q;
	while(q --) {
		ll x1, x2, y1, y2, k;
		cin >> x1 >> y1 >> x2 >> y2 >> k;
		//cout << "------\n";
		//cout << x1 << ' ' << x2 << ' ' << y1 << ' ' << y2 << ' ' << k << '\n';

		//cout << solve(x2, y2, k) << ' ' << solve(x1 - 1, y2, k) << ' ' << solve(x2, y1 - 1, k) << ' ' << solve(x1 - 1, y1 - 1, k) << '\n';

		ll ans = solve(x2, y2, k) - solve(x1 - 1, y2, k) - solve(x2, y1 - 1, k) + solve(x1 - 1, y1 - 1, k);
		ans = (ans % mod + mod) % mod;
		cout << ans << '\n';
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7, B = 30;
int X[B], Y[B], K[B];
pair<int, int> dfs(int i, int ox, int oy, int ok) {
	if (!ox && !oy && !ok) {
		int cnt = (1LL << (2 * i + 2)) % MOD;
		int xo = 0;
		for (int j = 0; j <= i; ++ j) {
			(xo += (((1LL << (2 * i + 1)) % MOD) << j) % MOD) %= MOD;
		}
		return {cnt, xo};
	}
	if (i < 0) return {0, 0};
	int cnt = 0, xo = 0;
	for (int x = 0; x <= (ox ? X[i] : 1); ++ x) {
		for (int y = 0; y <= (oy ? Y[i] : 1); ++ y) {
			int k = x ^ y;
			if (ok && k > K[i]) continue;
			auto tmp = dfs(i - 1, ox && x == X[i], oy && y == Y[i], ok && k == K[i]);
			(cnt += tmp.first) %= MOD;
			(xo += tmp.second) %= MOD;
			(xo += tmp.first * (1LL << i) * k % MOD) %= MOD;
		}
	}
	return {cnt, xo};
}
int solve(int x, int y, int k) {
	for (int i = 0; i < B; ++ i) X[i] = x >> i & 1;
	for (int i = 0; i < B; ++ i) Y[i] = y >> i & 1;
	for (int i = 0; i < B; ++ i) K[i] = k >> i & 1;
	auto tmp = dfs(B - 1, 1, 1, 1);
	return (tmp.first + tmp.second) % MOD;
}
int main() {
	int n;
	scanf("%d", &n);
	while (n --) {
		int x1, y1, x2, y2, k;
		scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
		x1 --, y1 --;
		int ans = (solve(x2, y2, k) - solve(x2, y1, k) - solve(x1, y2, k) + solve(x1, y1, k)) % MOD;
		printf("%d\n", ans);
	}
}

//#pragma GCC optimize("O2")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
const int maxn = 3e2 + 55, mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, a[maxn][maxn], cnt[maxn];
ll ari(ll x) {
	return x*(x+1)/2;
}
pi get(ll l, ll r, ll k) {
	if(!l||!r) return {0, 0};
	if(k<=0) return {0, 0};
	if(l==1&&r==1) return {1, 1};
	ll a = 1;
	while((a<<1)<l||(a<<1)<r)a<<=1;
	if(2*a==l&&2*a==r) return {2ll*a*ari(min(k, 2ll*a)), 2ll*a*min(k, 2ll*a)};
	ll res = 0;
	ll cnt = 0;
	pi t = get(min(a, l), min(a, r), k);
	(res += t.first)%=mod, (cnt += t.second)%=mod;
	if(l>=a&&r>=a) {
		t = get(l-a, r-a, k);
		(res += t.first)%=mod, (cnt += t.second)%=mod;
	}
	if(r>=a) {
		pi t = get(min(l, a), r-a, k-a);
		(res += t.first + a*t.second)%=mod;
		(cnt += t.second)%=mod;
	}
	if(l>=a) {
		pi t = get(l-a, min(r, a), k-a);
		(res += t.first + a*t.second)%=mod;
		(cnt += t.second)%=mod;
	}
//	cout << ";" << endl;
//	cout << l << " " << r << " " << k << " " << res << endl;
	return {res, cnt};
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
//	cout << get(5, 2, 5);
//	return 0;
	ll t, lx, rx, ly, ry, k;
	cin >> t;
	while(t--) {
		cin >> lx >> ly >> rx >> ry >> k, lx--, ly--;
		ll ans = get(rx, ry, k).first;
		if(lx) ans -= get(lx, ry, k).first;
		if(ly) ans -= get(rx, ly, k).first;
		if(lx&&ly) ans += get(lx, ly, k).first;
		cout << ans << "\n";
	}
}

#include "bits/stdc++.h"
using namespace std;
const int LN = 30;
const int mod = 1e9 + 7;
int n , m , k;
pair < int , int > dp[LN][2][2][2];
bool calc[LN][2][2][2];
pair < int , int > solve(int pos , bool prea , bool preb , bool prek){
	if(pos < 0){
		return {0 , 1};
	}
	if(calc[pos][prea][preb][prek]){
		return dp[pos][prea][preb][prek];
	}
	int res = 0;
	int resb = 0;
	int diga = (n >> pos) & 1;
	int digb = (m >> pos) & 1;
	int digk = (k >> pos) & 1;
	for(int i = 0 ; i < 2 ; ++i){
		if(!prea && i > diga){
			continue;
		}
		for(int j = 0 ; j < 2 ; ++j){
			if(!preb && j > digb){
				continue;
			}
			bool kbit = i ^ j;
			if(!prek && kbit > digk){
				continue;
			}
			auto it = solve(pos - 1 , prea | (i < diga) , preb | (j < digb) , prek | (kbit < digk));
			res = (res + 1LL * it.second * kbit * (1LL << pos) + it.first) % mod;
			resb += it.second;
			resb -= (resb >= mod) * mod;
		}
	}
	calc[pos][prea][preb][prek] = 1;
	return dp[pos][prea][preb][prek] = {res , resb};
}
int solve(int x , int y , int kk){
	n = x - 1;
	m = y - 1;
	k = kk - 1;
	if(n < 0 || m < 0 || k < 0){
		return 0;
	}
	return solve(LN - 1 , 0 , 0 , 0).first + solve(LN - 1 , 0 , 0 , 0).second;
}
int main(){
	int t;
	cin >> t;
	while(t--){
		int x1 , y1 , x2 , y2 , k;
		cin >> x1 >> y1 >> x2 >> y2 >> k;
		long long res = 0;
		for(int i = 0 ; i < LN ; ++i){
			calc[i][0][0][0] = calc[i][0][0][1] = calc[i][0][1][0] = calc[i][0][1][1] = calc[i][1][0][0] = calc[i][1][0][1] = calc[i][1][1][0] = 0;
		}
		res += solve(x2 , y2 , k);
		for(int i = 0 ; i < LN ; ++i){
			calc[i][0][0][0] = calc[i][0][0][1] = calc[i][0][1][0] = calc[i][0][1][1] = 0;
		}
		res -= solve(x1 - 1 , y2 , k);
		for(int i = 0 ; i < LN ; ++i){
			calc[i][0][0][0] = calc[i][0][0][1] = calc[i][1][0][0] = calc[i][1][0][1] = 0;
		}
		res += solve(x1 - 1 , y1 - 1 , k);
		for(int i = 0 ; i < LN ; ++i){
			calc[i][0][0][0] = calc[i][0][0][1] = calc[i][0][1][0] = calc[i][0][1][1] = 0;
		}
		res -= solve(x2 , y1 - 1 , k);
		res %= mod;
		res += mod;
		res %= mod;
		printf("%lld\n" , res);
	}
}

#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
#ifdef VAMP
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cout << name << " = " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cout.write(names, comma - names) << " = " << arg1 <<" | ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif

const LL MOD = 1e9 + 7;
LL dp[33][2][2][2][33][2];
int vis[33][2][2][2][33][2],vid;
LL call(LL pos,bool t1,bool t2,bool t3,LL x,LL y,LL k,LL in,bool T)
{
    if(pos == -1){
        return T == 1;
    }
    if(vis[pos][t1][t2][t3][in][T] == vid || (vis[pos][t1][t2][t3][in][T] > 0 and t1 == 0 and t2 == 0 and t3 == 0) )return dp[pos][t1][t2][t3][in][T];
    vis[pos][t1][t2][t3][in][T] = vid;
    LL ret = 0;

    bool h1 = (t1 ? (x & (1LL << pos)) : 1);
    bool h2 = (t2 ? (y & (1LL << pos)) : 1);
    bool h3 = (t3 ? (k & (1LL << pos)) : 1);

    for(LL f = 0;f <= h1;f++){
        for(LL s = 0;s <= h2;s++){
            if((f ^ s) > h3)continue;
            bool newt1 = t1 & (f == h1);
            bool newt2 = t2 & (s == h2);
            bool newt3 = t3 & ((f ^s) == h3);
            bool newT = T;
            if(in == pos and (f ^ s) == 1)newT = 1;
            ret = ret + call(pos - 1,newt1,newt2,newt3,x,y,k,in,newT);
        }
    }

    return dp[pos][t1][t2][t3][in][T] = ret;
}
LL solve(LL x,LL y,LL k){
    if(x == 0 || y == 0)return 0;
    vid++;
    x--;y--;k--;
    MEM(dp,-1);
    LL ans = 0;
    for(LL in = 0;in <= 30;in++){
        ans += (call(30,1,1,1,x,y,k,in,0) % MOD) * (1LL << in);
        ans %= MOD;
    }
    ans += call(30,1,1,1,x,y,k,30,1);
    return ans % MOD;
}
int main()
{
    #ifdef VAMP
        clock_t tStart = clock();
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    FastRead
    LL q;
    cin >> q;
    while(q--){
        LL x1 , y1 , x2 , y2, k;
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        LL ans = solve(x2,y2,k) - solve(x1 - 1,y2,k) - solve(x2,y1 - 1,k) + solve(x1 - 1,y1 - 1,k);
        ans = (ans % MOD + MOD) % MOD;
        cout << ans << "\n";
    }

    #ifdef VAMP
        fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
    #endif
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long ll;
ll a[1010][1010];
int b[1010];
int solve(int x,int y)
{
	memset(b,0,sizeof b);
	int i;
	for(i=1;i<y;i++)
		b[a[x][i]]++;
	for(i=1;i<x;i++)
		b[a[i][y]]++;
	for(i=1;b[i];i++);
	return i;
}
int main()
{
//	freopen("a.txt","w",stdout);
	int n=50;
	int i,j;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			int x=i;
			int y=j;
			if(x<y)
				swap(x,y);
			a[i][j]=(i-1)^(j-1)+1;
//			a[i][j]=solve(i,j);
//			printf("%3d ",a[i][j]);
		}
//		putchar('\n');
	}
	int q;
	scanf("%d",&q);
	int x1,x2,y1,y2,p;
	ll s;
	for(i=1;i<=q;i++)
	{
		int j,k;
		scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&p);
		s=0;
		for(j=x1;j<=x2;j++)
			for(k=y1;k<=y2;k++)
				if(a[j][k]<=p)
					s=(s+a[j][k])%1000000007;
		printf("%I64d\n",s);
	}
	return 0;
}

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int bitmaks=30;
#define x1 uvuvwe
#define x2 onyete
#define y1 onyetve
#define y2 ugmewuben
int q,target,dp[bitmaks+5][5][5][5],batas1,batas2,ans=0;
LL jumlah[bitmaks+5][5][5][5];
int bitke(int angka,int idx){
	return (angka&(1<<idx))?1:0;
}
int topdown(int digitnow,bool sama1,bool sama2,bool samatarget){
	if(digitnow==-1)
		return 1;
	int &ret=dp[digitnow][sama1][sama2][samatarget];
	if(ret!=-1)
		return ret;
	ret=0;
	int loop1,loop2,digittarget;
	loop1=(sama1)?bitke(batas1,digitnow):1;
	loop2=(sama2)?bitke(batas2,digitnow):1;
	digittarget=(samatarget)?bitke(target,digitnow):1;
	//cout<<"batas loopnya "<<loop1<<" "<<loop2<<endl;
	for(int i=0;i<=loop1;i++)
	{
		for(int j=0;j<=loop2;j++)
		{
			if((i^j)<=digittarget)
			{
				bool nextsama1=(sama1)?(i==loop1):false;
				bool nextsama2=(sama2)?(j==loop2):false;
				bool nextsamatarget=(samatarget)?((i^j)==digittarget):false;
				int banyak=topdown(digitnow-1,nextsama1,nextsama2,nextsamatarget);
				ret+=banyak;
				jumlah[digitnow][sama1][sama2][samatarget]+=LL(banyak)*LL((i^j)<<digitnow);
				if(digitnow!=0)
					jumlah[digitnow][sama1][sama2][samatarget]+=jumlah[digitnow-1][nextsama1][nextsama2][nextsamatarget];
			}
		}
	}
	return ret;
}
int total(int angka1,int angka2){
	if(angka1<0||angka2<0)
		return 0;
	batas1=angka1;
	batas2=angka2;
	memset(dp,-1,sizeof(dp));
	memset(jumlah,0,sizeof(jumlah));
	int temp=topdown(bitmaks,true,true,true);
	return jumlah[bitmaks][1][1][1]+temp;
}
int main()
{
	scanf("%d",&q);
	for(int i=1;i<=q;i++)
	{
		int x1,x2,y1,y2;
		scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&target);
		x1--,y1--,x2--,y2--,target--;
		//cout<<"soalnya itu dari range "<<x1<<" "<<x2<<endl;
		//cout<<"dan range "<<y1<<" "<<y2<<endl;
		//cout<<"targetnya "<<target<<endl;
		ans=total(x2,y2)-total(x1-1,y2)+total(x1-1,y1-1)-total(x2,y1-1);
		printf("%d\n",ans);
	}
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int maxn = 200005;
int a[30], b[30], c[30];
ll dp[30][2][2][2];
ll sum[30][2][2][2];
pair<ll,ll> dfs(int pos, int ba, int bb, int bc) {
	if (pos == -1) {
		return {1,0};
	}
	if (~dp[pos][ba][bb][bc]) {
		return {dp[pos][ba][bb][bc], sum[pos][ba][bb][bc]};
	}
	int upa = ba ? a[pos] : 1;
	int upb = bb ? b[pos] : 1;
	int upx = bc ? c[pos] : 1;
	ll cnt = 0;
	ll ans = 0;
	for (int i = 0 ; i <= upa ; i++) {
		for (int j = 0 ; j <= upb ; j++) {
			if ((i ^ j) > upx)continue;
			auto x = dfs(pos - 1, ba && (i == upa), bb && (j == upb), bc && (i ^ j) == upx);
			cnt += x.F;
			ans += x.S + (i^j) * (1LL<<pos) * x.F % mod;
			ans %= mod;
			cnt %= mod;
		}
	}
	dp[pos][ba][bb][bc] = cnt;
	sum[pos][ba][bb][bc] = ans;
	return {cnt, ans};
}
ll solve(int x, int y, int k) {
	if (x < 0 || y < 0 || k < 0) {
		return 0;
	}
	memset(dp, -1 ,sizeof(dp));
	memset(sum, 0 ,sizeof(sum));
	for (int i = 0 ; i < 30 ; i++) {
		a[i] = x & 1;
		x >>= 1;
		b[i] = y & 1;
		y >>= 1;
		c[i] = k & 1;
		k >>= 1;
	}
	auto ret = dfs(29,1,1,1);
	//cout << ret.F <<' '<<ret.S<<endl;
	return (ret.F + ret.S) % mod;
}
void go() {
	int q;
	cin >> q;
	while (q--) {
		int x1, x2 ,y2, y1, k;
		cin >> x1 >> y1 >> x2 >> y2 >> k;
		x1--, x2--, y1--, y2--;
		k--;
		cout << ((solve(x2, y2, k) - solve(x1 - 1, y2, k) - solve(x2, y1 - 1, k) + solve(x1-1,y1-1,k)) % mod + mod) % mod << '\n';
	}
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int c = 0;
    int t;
    if (!c) {
        t = 1;
    }
    else {
        cin >> t;
    }
    while (t--) {
        go();
    }

}

//be name khoda

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 1;
const ll mod = 1e9 + 7;

#define pii pair<ll,ll>

ll find(ll x1, ll y1, ll x2, ll y2, ll k, ll r, ll d)
{
	//cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << ' ' << k << ' ' << r << ' ' << d << endl;
	ll ans = 0;
	if(k <= 0)
		return ans;
	if(x1 == 1 && y1 == 1 && x2 == r && y2 == r)
	{
		ll o = min(r, max(k, (ll)0)) * r;
		o %= mod;
		ll d2 = min(k, r);
		ans = (((o % mod) * (d % mod)) % mod + ((((d2 + 1) * d2 / 2) % mod) * r) % mod ) % mod;
			//cout << ans << endl;
		return ans;
	}
	//top left:
	if(x1 <= r / 2 && y1 <= r / 2)
	{
		ll xt = min(x2, r / 2);
		ll yt = min(y2, r / 2);
		ans += find(x1, y1, xt, yt, k, r / 2, d);
		ans %= mod;
	}
	//top right
	if(x1 <= r / 2 && y2 > r / 2)
	{
		ll x1t = x1;
		ll y1t = 1;
		ll x2t = min(x2, r / 2);
		ll y2t = y2 - r / 2;
		ans += find(x1t, y1t, x2t, y2t, k - r / 2, r / 2, d + r / 2);
		ans %= mod;
	}
	//bottom left
	if(x2 > r / 2 && y1 <= r / 2)
	{
		//		cout << "bottom left\n";
		ll x1t = 1;
		ll y1t = y1;
		ll x2t = min(x2, r) - r / 2;
		ll y2t = min(y2, r / 2);
		ans += find(x1t, y1t, x2t, y2t, k - r / 2, r / 2, d + r / 2);
		ans %= mod;
	}
	//bottom right
	if(y2 > r / 2 && x2 > r / 2)
	{
		//		cout << "bottom right\n";
		ll x1t = 1;
		ll y1t = max(r / 2 + 1, y1) - r / 2;
		ll x2t = x2 - r / 2;
		ll y2t = y2 - r / 2;
		ans += find(x1t, y1t, x2t, y2t, k, r / 2, d);
		ans %= mod;
	}
	return ans;
}

int main()
{
	ios_base::sync_with_stdio(0);
	int q;
	cin >> q;
	while(q--)
	{
		int x1, y1, x2, y2, k;
		cin >> x1 >> y1 >> x2 >> y2 >> k;
		ll r = 1;
		while(r < x2 || r < y2)
			r *= 2;
		//cout << r << endl;
		ll o = find(x1, y1, x2, y2, k, r, 0);
		cout << o << endl;
	}
	return 0;
}

#include <algorithm>
#include <cassert>
#include <cstdio>

using namespace std;

constexpr static int MOD = 1e9 + 7;
constexpr static int MAXL = 30;

int64_t cnt[MAXL][MAXL];

int64_t calc(int64_t x, int64_t y, int64_t k) {
    if (x == 0 || y == 0)
        return 0;

    // try all prefixes
    int64_t total = 0;
    for (int i = 0; i < MAXL; i++) {
        if ((x & (1 << i)) == 0)
            continue;
        int64_t px = (x >> (i + 1)) << (i + 1);
        for (int j = 0; j < MAXL; j++) {
            if ((y & (1 << j)) == 0)
                continue;
            int64_t py = (y >> (j + 1)) << (j + 1);
            for (int l = 0; l < MAXL; l++) {
                if ((k & (1 << l)) == 0)
                    continue;
                int64_t pk = (k >> (l + 1)) << (l + 1);

                if ((px ^ py ^ pk) >> max(max(i, j), l))
                    continue;

                int64_t cnt = (1LL << (i + j));
                int idx = l;
                int64_t base = pk;
                if (i < l && j < l) {
                    idx = max(i, j);
                    base = ((px ^ py) >> idx) << idx;
                }
                cnt >>= (max(i, j) - idx);
                cnt %= MOD;
                if (cnt % 2 == 1)
                    cnt += MOD;
                int64_t sum = (base + base + (1LL << idx) - 1) * cnt / 2;
                total = (total + sum + cnt) % MOD;
            }
        }
    }
    return total;
}

int main() {
    int q;
    scanf("%d", &q);
    while (q--) {
        int x1, y1, x2, y2, k;
        scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
        int64_t r = (calc(x2, y2, k)
                - calc(x1 - 1, y2, k) + MOD
                - calc(x2, y1 - 1, k) + MOD
                + calc(x1 - 1, y1 - 1, k)) % MOD;
        printf("%lld\n", r);
    }
}

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int mod=(int)1e9+7;
int k;
int get(int a,int b){
    if(a>b) return 0;
    else return (ll)(b-a+1)*(a+b)/2;
}
int calc(int n,int m,int add){
    if(n<m) swap(n,m);
    if(n==0||m==0) return 0;
    int i;
    for(i=1;i<=n;i<<=1);i>>=1;
    if(i>=m) return (calc(n-i,m,add+i)+(ll)m*get(add+1,min(k,i+add)))%mod;
    else return (calc(n-i,m-i,add)+(ll)i*get(add+1,min(k,i+add))+(ll)(n+m-i*2)*get(add+i+1,min(k,i+i+add)))%mod;
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("C.in","r",stdin);
        freopen("C.out","w",stdout);
    #endif
    int q,x1,y1,x2,y2,ans;
    scanf("%d",&q);
    while(q--){
        scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&k);
        ans=calc(x2,y2,0)-calc(x2,y1-1,0)-calc(x1-1,y2,0)+calc(x1-1,y1-1,0);
        ans=(ans%mod+mod)%mod;
        printf("%d\n",ans);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef complex<ld> cd;

const ll mod = 1000000007;

ll large(ll x){
    for(int i = 30; i >= 0; i--){
        if((x>>i)&1)return (1LL<<i);
    }
}

ll comp(ll x, ll k){
    ll ret = k*(k+1)/2;
    ret %= mod;
    ret *= x;
    return ret%mod;
}

ll func(ll x, ll y, ll k, ll off = 0){
    if(!x || !y)return 0;
    if(x > y)return func(y, x, k, off);
    ll m = large(y);
    if(k <= m){
        ll ret = (off*x%mod)*k%mod;
        ret += comp(min(x, m), k);
        if(x > m){
            ret += func(x - m, y - m, k, off);
        }
        ret %= mod;
//         cout<<x<<" "<<y<<" "<<k<<" "<<off<<" "<<m<<" ";
//         cout<<ret<<" "<<1<<endl;
        return ret;
    }
    if(x <= m){
        ll ret = func(x, y - m, k - m, off^m);
        ret += comp(x, m);
        ret += (off*x%mod)*m%mod;
        ret %= mod;
//         cout<<x<<" "<<y<<" "<<k<<" "<<off<<" "<<m<<" ";
//         cout<<ret<<endl;
        return ret;
    }
    ll ret;
    if(off&m)ret = func(m, y - m, k + m, off - m);
    else ret = func(m, y - m, k - m, off + m);

    ret += func(x - m, y - m, k, off);

    ret += comp(m, m);
    ret += (off*m%mod)*m%mod;

    ll off2 = off^m;
    ret += comp(x-m, min(m, k-m));
    ret += (off2*(x-m)%mod)*min(m, k-m)%mod;
    ret %= mod;

//     cout<<x<<" "<<y<<" "<<k<<" "<<off<<" "<<m<<" "<<off2<<" "<<comp(x-m,min(m,k-m))<<" "<<(off2*(x-m)%mod)*min(m, k-m)%mod<<" ";
//     cout<<ret<<endl;
    return ret;
}

inline ll query(ll x1, ll y1, ll x2, ll y2, ll k){
    ll ret = func(x2, y2, k) - func(x1 - 1, y2, k) - func(x2, y1 - 1, k) + func(x1 - 1, y1 - 1, k) + mod + mod;
    return ret%mod;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cout<<setprecision(30);

//     for(int i = 1; i <= 20; i++){
//         for(int j = 1; j <= 20; j++){
//             cout<<query(i, j, i, j, 1000)<<" ";
//         }
//         cout<<endl;
//     }

    int q;
    ll x1, x2, y1, y2, k;
    cin>>q;
    while(q--){
        cin>>x1>>y1>>x2>>y2>>k;
        cout<<query(x1, y1, x2, y2, k)<<endl;
    }

}

/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>*/
#include <bits/stdc++.h>

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

const int MOD = 1000000007;
double PI = 4*atan(1);

ll get(ll mx) {
    return (mx*(mx+1)/2) % MOD;
}

ll case1(ll s, ll x, ll y, ll k, ll ad) {
    // just squares
    // 1 to div*s each appear s times
    ll div = y/s;
    return ((get(min(div*s,k))+min(div*s,k)*ad)*s) % MOD;
}

ll case2(ll s, ll x, ll y, ll k, ll ad) {
    ll div = y/s;
    // case 2:
    // div*s+1 to (div+1)*s appear y-div*s times;
    ll up = min((div+1)*s,k);
    if (up > div*s) return (get(up)-get(div*s)+(up-div*s)*ad)*(y-div*s) % MOD;
    return 0;
}

ll case3(ll s, ll x, ll y, ll k, ll ad) {
    // case 3: upper rectangles excluding one case
    ll div = y/s;
    ll up = min((div/2)*2*s,k);
    return ((x-s)*get(up)+up*(x-s)*ad) % MOD;
}

ll query (ll x, ll y, ll k, ll ad) {
    if (x <= 0 || y <= 0 || k <= 0) return 0;
    if (x>y) swap(x,y);

    ll s = 1;
    while (2*s <= x) s *= 2; // correct
    ll div = y/s;
    ll ans = (case1(s,x,y,k,ad)+case2(s,x,y,k,ad)+case3(s,x,y,k,ad)) % MOD;
    if (div % 2 == 1) {
        ll lo = div*s+1, hi = min((div+1)*s,k);
        if (lo <= hi) ans = (ans+(get(hi)-get(lo-1)+ad*(hi-lo+1))*(x-s)) % MOD;
        return (ans+query(x%s,y%s,k-(div-1)*s,(div-1)*s+ad)) % MOD;
    } else {
        return (ans+query(x%s,y%s,k-(div+1)*s,(div+1)*s+ad)) % MOD;
    }
}

int main() {
    int q; cin >> q;
    F0R(i,q) {
        ll x1,x2,y1,y2,k; cin >> x1 >> y1 >> x2 >> y2 >> k;
        ll ans = query(x2,y2,k,0)-query(x1-1,y2,k,0)-query(x2,y1-1,k,0)+query(x1-1,y1-1,k,0);
        ans %= MOD;
        while (ans<0) ans += MOD;
        cout << ans << "\n";
    }
}

#include<map>
#include<set>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define sz(a) ((int)(a).size())
#define all(x) (x).begin(),(x).end()
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define R(a) scanf("%d",&a)
#define per(i,a,b) for(int i=a;i>=int(b);i--)
#define rep(i,a,b) for(int i=a;i<=int(b);i++)
#define rix(i,x) for(int i=last[x];i;i=pre[i])
#define block(x) int l=x*L,r=min((x+1)*L,n)-1
const ld eps=1e-8;
int sgn(ld x){return x<-eps?-1:x>eps;}
auto Mn(auto&a,auto b){if(a>b){a=b;return 1;}return 0;}
auto Mx(auto&a,auto b){if(a<b){a=b;return 1;}return 0;}
ll gcd(ll a,ll b){return a==0?b:gcd(b%a,a);}
ll inv(ll a,ll b){return a==1?1:(1-b*inv(b%a,a))/a;}
ll exg(ll a,ll b,int&x,int&y){!a?x=0,y=1:(exg(b%a,a,y,x),x-=b/a*y);}
ll pow(ll a,ll b,ll M) {ll t=1;for(a%=M;b;b>>=1){if(b&1)t=t*a%M;a=a*a%M;}return t;}
const int M=1e9+7;
int get(int x,int y,int k,int d){
	if(x<1||y<1||k<1)return 0;
	if(x>y)swap(x,y);
	int i=31-__builtin_clz(x);
	int j=31-__builtin_clz(y);
	if(y==(1<<j)){
		if(y<=k)return 1LL*(1+y)*y/2%M*x%M+1LL*d*x%M*y%M;
		return 1LL*(1+k)*k/2%M*x%M+1LL*d*k%M*x%M;;
	}
	if(i==j)return (0LL+get(1<<i,1<<i,k,d)+get(x-(1<<i),1<<i,k-(1<<i),d+(1<<i))+
					get(1<<i,y-(1<<i),k-(1<<i),d+(1<<i))+get(x-(1<<i),y-(1<<i),k,d))%M;
	//if(x!=(1<<i))return (get(1<<i,y,k,d)+get(x-(1<<i),y,k-(1<<i),d))%M;
	return (get(x,1<<j,k,d)+get(x,y-(1<<j),k-(1<<j),d+(1<<j)))%M;
}
int main(){
	//cout<<get(7,30,123412,0)<<endl;
	int T;cin>>T;while(T--){
		int lx,ly,rx,ry,k;
		cin>>lx>>ly>>rx>>ry>>k;
		cout<<((0LL+get(rx,ry,k,0)+get(lx-1,ly-1,k,0)-get(rx,ly-1,k,0)-get(lx-1,ry,k,0))%M+M)%M<<endl;
	}
}

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b) - 1, i >= a; i--)
#define F0Rd(i, a) for (int i = (a) - 1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const int SZ = 1 << 30;
void fast_io(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}

ll query(int u, int v, int k, int add = 0){
	//sum of (a xor b) + 1 over 0 <= a <= u, 0 <= b <= v as well as adding add to all elements inside, within the smaller grid determined by 0 and (1 << level) - 1
	//and in which (a xor b) + 1 <= k
	if (k <= 0) return 0LL;
	if (u < 0 || v < 0) return 0LL;
	if (u == 0 && v == 0){
		if (k >= 1LL){
			return (1LL + add)%MOD;
		}
		else{
			return 0LL;
		}
	}
	if (u < v) swap(u, v);
	//u is larger, this step does not change the answer
	ll pow2 = 1;
	while (2*pow2 <= u + 1){
		pow2 *= 2;
	}
	if (v > pow2 - 1){
		ll r = u + v + 2 - 2*pow2;
		assert(r > 0);
		if (k > pow2){
			ll over = min((long long) k, 2*pow2);
			ll n = (((pow2*pow2 + r*(over - pow2))%MOD)*add)%MOD + ((pow2/2*pow2)%MOD*(pow2 + 1))%MOD + (((over - pow2)*(over + pow2 + 1)/2)%MOD * (r))%MOD;
			return (n + query(u - pow2, v - pow2, k, add)) % MOD;
		}
		else{
			ll val = k;
			ll firsttri = (val*(val + 1))/2LL;
			firsttri %= MOD;
			ll secval = val*add;
			secval %= MOD;
			return (pow2*secval + pow2*firsttri + query(u - pow2, v - pow2, k, add)) % MOD;
		}
	}
	else{
		ll r = min(((long long) k), pow2);
		ll inc = r*add;
		r *= (r+1LL);
		r /= 2LL;
		r %= MOD;
		return ((v + 1)*(r + inc) % MOD + query(u - pow2, v, k - pow2, add + pow2)) % MOD;
	}
}

int main(){
	int Q;
	cin >> Q;
	for (int i = 0; i < Q; i++){
		int x1, y1, x2, y2, k; cin >> x1 >> y1 >> x2 >> y2 >> k;
		x1--; y1--; x2--; y2--;
		ll res = query(x2, y2, k) - query(x1 - 1, y2, k) - query(x2, y1 - 1, k) + query(x1 - 1, y1 - 1, k);
		res %= MOD;
		if (res < 0){
			res += MOD;
		}
		cout << res << endl;
	}
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = (1e9)+7;

pair<ll, ll> query(int x, int y, int k, int lim) {
	if(x <= 0 || y <= 0 || k < 0 || lim <= 0) return {0, 0};
	int pw = 1 << k;
	ll cnt = 0, sum = 0;
	if(x >= pw || y >= pw) {
		int val_cnt = min(pw, lim), node_cnt = min(pw, min(x, y));
		cnt = 1ll * val_cnt * node_cnt % MOD;
		sum = 1ll * (val_cnt * (val_cnt + 1) / 2 % MOD) * node_cnt;
	} else {
		pw >>= 1;
		pair<ll, ll> res[2][2] = {
			query(x, y, k-1, lim),
			query(x - pw, y, k-1, lim - pw),
			query(x, y - pw, k-1, lim - pw),
			query(x - pw, y - pw, k-1, lim)
		};
		cnt = (res[0][0].second + res[0][1].second + res[1][0].second + res[1][1].second) % MOD;
		sum = (res[0][0].first + res[0][1].first + res[1][0].first + res[1][1].first) % MOD;
		sum = (sum + (res[0][1].second + res[1][0].second) * pw) % MOD;
	}
	return {sum, cnt};
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while(q--) {
		int x1, y1, x2, y2, lim;
		cin >> x1 >> y1 >> x2 >> y2 >> lim;
		int k = 0;
		while(1ll << k <= max(x2, y2)) ++k;
		ll res = query(x2, y2, k, lim).first
					 - query(x1 - 1, y2, k, lim).first
					 - query(x2, y1 - 1, k, lim).first
					 + query(x1 - 1, y1 - 1, k, lim).first;
		cout << res << '\n';
	}
}

#include <bits/stdc++.h>
#define LL long long
#define MP make_pair
using namespace std;

template<class T> inline
void read(T& x) {
	int f = 1; x = 0;
	char ch = getchar();
	while (ch < '0' || ch > '9')   {if (ch == '-') f = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
	x *= f;
}

/*============ Header Template ============*/

typedef pair<LL, LL> PI;

int Q;
int N, M, K;
int vis[35][2][2][2];
PI f[35][2][2][2], ans;

PI operator + (const PI &a, const PI &b) {
	return MP(a.first + b.first, a.second + b.second);
}

PI dfs(int d, int limx, int limy, int limk) {
	if (d == -1) return MP(1, 0);
	if (vis[d][limx][limy][limk]) return f[d][limx][limy][limk];
	vis[d][limx][limy][limk] = 1;
	int upx = limx ? ((N >> d) & 1) : 1;
	int upy = limy ? ((M >> d) & 1) : 1;
	int upk = limk ? ((K >> d) & 1) : 1;
	PI ans = MP(0, 0);
	for (int i = 0; i <= upx; i++) {
		for (int j = 0; j <= upy; j++) {
			if (!limk || ((i ^ j) <= upk)) {
				PI t = dfs(d - 1, limx && (i == upx), limy && (j == upy), limk && ((i ^ j) == upk));
				ans = ans + t;
				ans.second = ans.second + t.first * ((i ^ j) << d);
			}
		}
	}
	// printf("f[%d][%d][%d][%d] = %lld %lld\n", d, limx, limy, limk, ans.first, ans.second);
	return f[d][limx][limy][limk] = ans;
}

PI solve(LL n, LL m, LL k) {
	if (n == -1 || m == -1 || k == -1) return MP(0, 0);
	N = n, M = m, K = k;
	memset(f, 0, sizeof f);
	memset(vis, 0, sizeof vis);
	return dfs(31, 1, 1, 1);
}

int main() {
	read(Q);
	for (int i = 1, a, b, c, d, k; i <= Q; i++) {
		read(a), read(c), read(b), read(d), read(k); a--, b--, c--, d--, k--;
		// printf("%lld\n", solve(b, d, k));
		PI tmp1 = solve(b, d, k) + solve(a - 1, c - 1, k);
		PI tmp2 = solve(b, c - 1, k) + solve(a - 1, d, k);
		printf("%lld\n", tmp1.first + tmp1.second - tmp2.first - tmp2.second);
	}
	return 0;
}

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define sc second

const long long mdl = 1000 * 1000 * 1000 + 7;
const int size = 100;
const int bits = 31;

map <pair <int, int>, int > sum[bits];
map <pair <int, pair <int, int> >, int> ans[bits];
map <pair <int, pair <int, int> >, int> cnt[bits];

int getcount(int b, int x, int y, int k) {
	if (x < 0 || y < 0 || k == 0)
		return 0;
	if (x > y)
		return getcount(b, y, x, k);

	auto it = cnt[b].find(mp(k, mp(x, y)));
	if (it != cnt[b].end())
		return it->sc;

	if (b == 0) {
		int ansv = 0;
		for (int i = 0; i <= x; i++)
			for (int j = 0; j <= y; j++) {
				int d = (i ^ j);
				if (d <= k)
					ansv++;
			}
		cnt[b][mp(k, mp(x, y))] = ansv;

		return ansv;
	}

	int ansv = 0;
	int len = (1 << b);
	if ((k >> b) & 1) {
		int h = min(x, len - 1) + 1;
		int w = min(y, len - 1) + 1;
		ansv = (h * 1ll * w) % mdl;

		h = max(0, x - len + 1);
		w = max(0, y - len + 1);
		ansv = (ansv + h * 1ll * w) % mdl;

		ansv += getcount(b - 1, min(x, len - 1), y - len, k - len);
		if (ansv >= mdl)
			ansv -= mdl;

		ansv += getcount(b - 1, x - len, min(y, len - 1), k - len);
		if (ansv >= mdl)
			ansv -= mdl;
	} else {
		ansv = getcount(b - 1, min(x, len - 1), min(y, len - 1), k);
		ansv += getcount(b - 1, x - len, y - len, k);
		if (ansv >= mdl)
			ansv -= mdl;
	}

	cnt[b][mp(k, mp(x, y))] = ansv;

//	cerr << "count " << b << ' ' << k << ' ' << x << ' ' << y << ' ' << ansv << endl;

	return ansv;
}

int getsum(int b, int x, int y) {
	if (x < 0 || y < 0)
		return 0;
	if (x > y)
		return getsum(b, y, x);

	auto it = sum[b].find(mp(x, y));
	if (it != sum[b].end())
		return it->sc;

	int ansv = 0;
	if (b == 0) {
		for (int i = 0; i <= x; i++)
			for (int j = 0; j <= y; j++)
				ansv += (i ^ j);
	} else {
		int len = (1 << b);
		ansv = getsum(b - 1, min(x, len - 1), min(y, len - 1));
		ansv += getsum(b - 1, x - len, y - len);
		if (ansv >= mdl)
			ansv -= mdl;

		ansv += getsum(b - 1, min(x, len - 1), y - len);
		if (ansv >= mdl)
			ansv -= mdl;

		ansv += getsum(b - 1, x - len, min(y, len - 1));
		if (ansv >= mdl)
			ansv -= mdl;

		int w = max(0, x - len + 1);
		int h = min(y, len - 1) + 1;
		ansv = (ansv + ((w * 1ll * h) % mdl) * 1ll * len) % mdl;

		w = min(x, len - 1) + 1;
		h = max(0, y - len + 1);
		ansv = (ansv + ((w * 1ll * h) % mdl) * 1ll * len) % mdl;
	}

	sum[b][mp(x, y)] = ansv;

//	cerr << "sum " << b << ' ' << x << ' ' << y << ' ' << ansv << endl;

	return ansv;
}

int getans(int b, int x, int y, int k) {
	if (x < 0 || y < 0 || k == 0)
		return 0;
	if (x > y)
		return getans(b, y, x, k);

	auto it = ans[b].find(mp(k, mp(x, y)));
	if (it != ans[b].end())
		return it->sc;

	if (b == 0) {
		int ansv = 0;
		for (int i = 0; i <= x; i++)
			for (int j = 0; j <= y; j++) {
				int d = (i ^ j);
				if (d <= k)
					ansv += d;
			}
		ans[b][mp(k, mp(x, y))] = ansv;

		return ansv;
	}

	int ansv = 0;
	int len = (1 << b);
	if ((k >> b) & 1) {
		ansv = getsum(b - 1, min(x, len - 1), min(y, len - 1));
		ansv += getsum(b - 1, x - len, y - len);
		if (ansv >= mdl)
			ansv -= mdl;

		ansv += getans(b - 1, min(x, len - 1), y - len, k - len);
		if (ansv >= mdl)
			ansv -= mdl;
		int h = getcount(b - 1, min(x, len - 1), y - len, k - len);
		ansv = (ansv + h * 1ll * len) % mdl;

		ansv += getans(b - 1, x - len, min(y, len - 1), k - len);
		if (ansv >= mdl)
			ansv -= mdl;
		h = getcount(b - 1, x - len, min(y, len - 1), k - len);
		ansv = (ansv + h * 1ll * len) % mdl;
	} else {
		ansv = getans(b - 1, min(x, len - 1), min(y, len - 1), k);
		ansv += getans(b - 1, x - len, y - len, k);
		if (ansv >= mdl)
			ansv -= mdl;
	}

	ans[b][mp(k, mp(x, y))] = ansv;

//	cerr << "ans " << b << ' ' << k << ' ' << x << ' ' << y << ' ' << ansv << endl;

	return ansv;
}

int main () {
/*
	freopen("input.txt", "w", stdout);
	printf("10000\n");
	for (int i = 0; i < 10 * 1000; i++) {
		printf("%d %d %d\n", 900 * 1000 * 1000, 1000 * 1000 * 1000 - i, 1000 * 1000 * 1000 - 9999 + i);
	}

	return 0;
*/

	int q;

   // freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);

	scanf("%d", &q);
	for (int t = 0; t < q; t++) {
		int l, r, u, d, k;
		scanf("%d%d%d%d%d", &l, &u, &r, &d, &k);
		l--, r--, u--, d--;
		k--;

		long long ans = 0ll + getans(bits - 1, r, d, k) - getans(bits - 1, l - 1, d, k) - getans(bits - 1, r, u - 1, k) + getans(bits - 1, l - 1, u - 1, k);
		ans += (0ll + getcount(bits - 1, r, d, k) - getcount(bits - 1, l - 1, d, k) - getcount(bits - 1, r, u - 1, k) + getcount(bits - 1, l - 1, u - 1, k));

		printf("%d\n", int(ans % mdl));
	//	cerr << t << endl;
	}

    return 0;
}

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mod = 10e9+7;

inline void add(ll &a, ll b)
{
    a += b;
    if(a > mod)
        a -= mod;
}

inline void sub(ll &a, ll b)
{
    a -= b;
    if(a < 0)
        a += mod;
}

inline ll mul(ll a, ll b)
{
    return (a*b)%mod;
}

inline int dig(int a, int i)
{
    return 1 & (a >> (31-i));
}
ll solve(int x, int y, int k)
{
    ll ct[32][2][2][2], sum[32][2][2][2];
    memset(ct, 0, sizeof ct);
    memset(sum, 0, sizeof sum);
    if(x < 0 || y < 0 || k < 0)
        return 0;
    ct[0][1][1][1] = 1;
    for(int i = 0; i < 31; i++)    {
        for(int xx = 0; xx < 2; xx++)
            for(int yy = 0; yy < 2; yy++)   {
                int kk = xx ^ yy;
                for(int fx = 0; fx < 2; fx++)
                    for(int fy = 0; fy < 2; fy++)
                        for(int fk = 0; fk < 2; fk++)   {
                            if(fx && xx>dig(x,i+1)) continue;
                            if(fy && yy>dig(y,i+1)) continue;
                            if(fk && kk>dig(k,i+1)) continue;
                            add(ct[i+1][fx&(xx==dig(x,i+1))][fy&(yy==dig(y,i+1))][fk&(kk==dig(k,i+1))], ct[i][fx][fy][fk]);
                            add(sum[i+1][fx&(xx==dig(x,i+1))][fy&(yy==dig(y,i+1))][fk&(kk==dig(k,i+1))], sum[i][fx][fy][fk]);
                            add(sum[i+1][fx&(xx==dig(x,i+1))][fy&(yy==dig(y,i+1))][fk&(kk==dig(k,i+1))], mul(kk<<(30-i), ct[i][fx][fy][fk]));
                        }
            }

    }
    ll ans = 0;
    for(int xx = 0; xx < 2; xx++)
        for(int yy = 0; yy < 2; yy++)
            for(int kk = 0; kk < 2; kk++)   {
                add(ans, ct[31][xx][yy][kk]);
                add(ans, sum[31][xx][yy][kk]);
            }
    return ans;
}

int main()
{
    int q;
    cin>>q;
    while(q--)  {
        int x, y, x2, y2, k;
        cin>>x>>y>>x2>>y2>>k;
        x--;y--;x2--;y2--;k--;
        ll res = 0;
        add(res,solve(x2,y2,k));
        sub(res,solve(x2,y-1,k));
        sub(res,solve(x-1,y2,k));
        add(res,solve(x-1,y-1,k));
        cout<<res<<'\n';
    }
}

#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,ssse3,sse4.1,sse4.2,popcnt")
#include<bits/extc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
using state = array<int, 2>;
state dp[31][2][2][2];
state inc(state a, int b) {
	return {a[0], (a[1] + a[0]*1ll*b)%mod};
}
void add(array<int, 2> &a, array<int, 2> b) {
	a[0] += b[0], a[1] += b[1];
}
int f(int x, int y, int k) {
	memset(dp, 0, sizeof dp);
	dp[0][0][0][0] = {1, 0};
	for(int i = 0; i < 30; i++) {
		int ca = (x>>i)&1, cb = (y>>i)&1, cc = (k>>i)&1;
		for(int ra = 0; ra < 2; ra++)
			for(int rb = 0; rb < 2; rb++)
				for(int rc = 0; rc < 2; rc++)
					for(int na = 0; na < 2; na++)
						for(int nb = 0; nb < 2; nb++) {
							int nra = (ca == na      ? ra : na      < ca);
							int nrb = (cb == nb      ? rb : nb      < cb);
							int nrc = (cc == (na^nb) ? rc : (na^nb) < cc);
							add(dp[i+1][nra][nrb][nrc],
							(na^nb) ? inc(dp[i][ra][rb][rc], 1<<i) : dp[i][ra][rb][rc]
							);
						}
	}
	auto t = dp[30][1][1][1];
	return (t[0] + t[1])%mod;
}
signed main() {
	cin.tie(0)->sync_with_stdio(0);
	int t, xl, xr, yl, yr, k;
	cin >> t;
	while(t--) {
		cin >> xl >> yl >> xr >> yr >> k;--xl, --yl;
		cout << (mod*4ll + f(xr, yr, k) - f(xl, yr, k) - f(xr, yl, k) + f(xl, yl, k))%mod << '\n';
	}

}

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mo=1e9+7;
#define y1 ovo
int pw[64],k,T,x1,y1,x2,y2;
int solve(int x,int y,int f)
{
	if(x==0||y==0||f>k)return 0;
	if(x<y)swap(x,y);
	if(x==(x&-x))
	{
		int l=f,r=min(k,f+x-1);
		return (l+r)*(r-l+1)/2%mo*y%mo;
	}
	int d=1,res=0;
	while((d<<1)<x)d<<=1;
	if(y<=d)
	{
		res+=solve(d,y,f)+solve(x-d,y,f+d);res%=mo;
	}
    else
	{
		res+=(solve(d,d,f)+solve(x-d,d,f+d)+solve(d,y-d,f+d)+solve(x-d,y-d,f))%mo;res%=mo;
    }
    return res;
}
signed main()
{
    cin>>T;
    pw[0]=1;
    for(int i=1;i<=60;i++)pw[i]=pw[i-1]<<1,pw[i]%=mo;
    while(T--)
    {
        cin>>x1>>y1>>x2>>y2>>k;
        cout<<(solve(x2,y2,1)-solve(x1-1,y2,1)-solve(x2,y1-1,1)+solve(x1-1,y1-1,1))%mo<<endl;
    }
}
/*
每次按照2的次幂砍
1
3 2 5 4 5
*/

#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = (l), i##end = (r);i <= i##end;++i)
using std::cin; using std::cout;
const int maxn = 200200;
typedef long long ll;
const int mod = 998244353;
inline ll pow(ll a,int b,int ans = 1) {
	for(;b;b >>= 1,a = a * a % mod) if(b & 1)
		ans = ans * a % mod;
	return ans;
}
inline ll inverse(int x){ return pow(x, mod - 2); }
inline int calc(int x, int y, int k) {
	ll ans = 0; int v = x ^ y;
	for(int i = 29;i >= 0;--i) if(x >> i & 1) {
		for(int j = 29;j >= 0;--j) if(y >> j & 1) {
			int max = std::max(i, j), p = (v ^ 1 << i ^ 1 << j) & ~((1 << max) - 1), cnt = 1 << std::min(i, j);
			if(p < k) {
				const int first = p, end = std::min(k, p + (1 << max));
				const int sum = (1ll + first + end) * (end - first) / 2 % mod;
				ans = (ans + (ll) sum * cnt) % mod;
			}
		}
	}
	return ans;
}
int main() {
	std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int q;
	cin >> q;
	for(int i = 0;i < q;++i) {
		int x1, y1, x2, y2, k;
		cin >> x1 >> y1 >> x2 >> y2 >> k;
		int ans = calc(x2, y2, k) - calc(x2, y1 - 1, k) - calc(x1 - 1, y2, k) + calc(x1 - 1, y1 - 1, k);
		cout << (unsigned) (ans + mod + mod) % mod << '\n';
	}
}

#include <iostream>
#include <algorithm>
using namespace std;
#define MOD 1000000007
#define ll long long

int psum(long long v)
{
	long long ans = (v*(v+MOD-1))%MOD;
	ans = (ans*500000004LL)%MOD;
	return (int)ans;
}

pair<ll,ll> getDP(int x,int y,int k)	//num,sum
{
	if(x==0 || y==0 || k==0) return make_pair(0,0);
	if(x==1)
	{
		int low = min(y,k);
		return make_pair(low,psum(low));
	}
	if(y==1)
	{
		int low = min(x,k);
		return make_pair(low,psum(low));
	}
	if(k==1)
	{
		int low = min(x,y);
		return make_pair(low,low);
	}
	pair<ll,ll> v1 = getDP((x+1)/2,(y+1)/2,(k+1)/2);
	pair<ll,ll> v2 = getDP(x/2,(y+1)/2,k/2);
	pair<ll,ll> v3 = getDP((x+1)/2,y/2,k/2);
	pair<ll,ll> v4 = getDP(x/2,y/2,(k+1)/2);
	int num = (((long long)v1.first) + ((long long)v2.first) + ((long long)v3.first) + ((long long)v4.first))%MOD;
	int ans = (2LL*v1.second + MOD - v1.first)%MOD;
	ans = (ans + 2LL*v4.second + MOD - v4.first)%MOD;
	ans = (ans + 2LL*v2.second + 2LL*v3.second)%MOD;
	return make_pair(num,ans);
}

int mask[31];

pair<ll,ll> getWays(int x,int y,int k)
{
	if(x==0 || y==0 || k==0) return make_pair(0,0);
	int totNum = 0;
	int totSum = 0;
	for(int t=0;t<2;t++)
	{
		for(int i=30;i>=0;i--) if(x&(1<<i))
		{
			x ^= (1<<i);
			for(int j=i-1;j>=0;j--) if(y&(1<<j))
			{
				y ^= (1<<j);
				if(((x^y)>>i) < (k>>i))
				{
					int cnum = ((1LL<<i)*(1LL<<j))%MOD;
					totNum = (totNum + cnum)%MOD;
					totSum = (totSum + (1LL<<j)*psum(1LL<<i))%MOD;
					totSum = (totSum + (((x^y)>>i)<<i)*cnum)%MOD;
				}
				else if(((x^y)>>i) == (k>>i))
				{
					int lowk = (k&((1<<i)-1));
					int cnum = (lowk*(1LL<<j))%MOD;
					totNum = (totNum + cnum)%MOD;
					totSum = (totSum + (1LL<<j)*psum(lowk))%MOD;
					totSum = (totSum + (((x^y)>>i)<<i)*cnum)%MOD;
				}

				y ^= (1<<j);
			}
			x ^= (1<<i);
		}
		swap(x,y);
	}
	for(int i=30;i>=0;i--) if(x&(1<<i)) if(y&(1<<i))
	{
		x ^= (1<<i);
		y ^= (1<<i);
		if(((x^y)>>i) < (k>>i))
		{
			int cnum = ((1LL<<i)*(1LL<<i))%MOD;
			totNum = (totNum + cnum)%MOD;
			totSum = (totSum + (1LL<<i)*psum(1LL<<i))%MOD;
			totSum = (totSum + (((x^y)>>i)<<i)*cnum)%MOD;
		}
		else if(((x^y)>>i) == (k>>i))
		{
			int lowk = (k&((1<<i)-1));
			int cnum = (lowk*(1LL<<i))%MOD;
			totNum = (totNum + cnum)%MOD;
			totSum = (totSum + (1LL<<i)*psum(lowk))%MOD;
			totSum = (totSum + (((x^y)>>i)<<i)*cnum)%MOD;
		}
		x ^= (1<<i);
		y ^= (1<<i);
	}
	return make_pair(totNum,(totNum+totSum)%MOD);
}

int main()
{
	int Q,x1,y1,x2,y2,k;
	cin >> Q;
	for(int i=0;i<Q;i++)
	{
		cin >> x1 >> y1 >> x2 >> y2 >> k;
		int ans = getWays(x2,y2,k).second;
		ans = (ans + MOD - getWays(x2,y1-1,k).second)%MOD;
		ans = (ans + MOD - getWays(x1-1,y2,k).second)%MOD;
		ans = (ans + getWays(x1-1,y1-1,k).second)%MOD;
		cout << ans << '\n';

	}
}

//#pragma GCC optimize("O2")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
const int maxn = 3e2 + 55, mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, a[maxn][maxn], cnt[maxn];
ll ari(ll x) {
	return x*(x+1)/2;
}
pi get(ll l, ll r, ll k) {
	if(!l||!r) return {0, 0};
	if(k<=0) return {0, 0};
	if(l==1&&r==1) return {1, 1};
	ll a = 1;
	while((a<<1)<l||(a<<1)<r)a<<=1;
	if(2*a==l&&2*a==r) return {2ll*a*ari(min(k, 2ll*a)), 2ll*a*min(k, 2ll*a)};
	ll res = 0;
	ll cnt = 0;
	pi t = get(min(a, l), min(a, r), k);
	(res += t.first)%=mod, (cnt += t.second)%=mod;
	if(l>=a&&r>=a) {
		t = get(l-a, r-a, k);
		(res += t.first)%=mod, (cnt += t.second)%=mod;
	}
	if(r>=a) {
		pi t = get(min(l, a), r-a, k-a);
		(res += t.first + a*t.second)%=mod;
		(cnt += t.second)%=mod;
	}
	if(l>=a) {
		pi t = get(l-a, min(r, a), k-a);
		(res += t.first + a*t.second)%=mod;
		(cnt += t.second)%=mod;
	}
//	cout << ";" << endl;
//	cout << l << " " << r << " " << k << " " << res << endl;
	return {res, cnt};
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
//	cout << get(5, 2, 5);
//	return 0;
	ll t, lx, rx, ly, ry, k;
	cin >> t;
	while(t--) {
		cin >> lx >> ly >> rx >> ry >> k, lx--, ly--;
		ll ans = get(rx, ry, k).first;
		if(lx) ans -= get(lx, ry, k).first;
		if(ly) ans -= get(rx, ly, k).first;
		if(lx&&ly) ans += get(lx, ly, k).first;
		cout << ans << "\n";
	}
}

#include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;
using ld=long double;

const int mod = 1000000007;
struct Mod {
public:
	int num;
	Mod() : Mod(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) {
		static_assert(mod<INT_MAX / 2, "mod is too big, please make num 'long long int' from 'int'");
	}
	Mod(int n) : Mod(static_cast<long long int>(n)) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int  b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b)*a; }
Mod operator*(const Mod a, const int b) { return Mod(b)*a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod& a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod mod_pow(const Mod a, const long long  int n) {
	if (n == 0) return Mod(1);
	Mod res = mod_pow((a * a), (n / 2));
	if (n % 2) res = res * a;
	return res;
}

Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	return Mod(a) / b;
}
Mod operator/=(Mod &a, const Mod b) {
	return a = a / b;
}

#define MAX_MOD_N 1024000

Mod fact[MAX_MOD_N], factinv[MAX_MOD_N];
void init(const int amax = MAX_MOD_N) {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < amax - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}

int x1, x2, yy, y2;
int K;
int cnt=0;

long long solve(int lu_num,long long int l, long long int r, long long int u, long long int d) {
	cnt++;
	if(r<=x1||x2<=l||d<=yy||y2<=u)return Mod(0);
	long long  ans = 0;
	bool lr_break = x1 <= l&&r <= x2;
	bool ud_break = yy <= u&&d <= y2;
	if (lr_break&&ud_break) {
		long long int sz=max((r-l),(d-u));
		long long int set_num=min((r-l),(d-u));
		//lu_num kara lu_num+sz-1 がset_num こある

		long long int  sum= (lu_num+lu_num + sz - 1)*sz/2;
		sum%=mod;
		long long int  n_sum=0;
		if (lu_num > K) {
			n_sum=0;
		}
		else if (lu_num + sz - 1 > K) {
			long long int minus = (K + 1 + lu_num + sz - 1)*(lu_num+sz-1-K-1+1) / 2;
			n_sum = (mod + sum - minus) % mod;
		}
		else {
			n_sum=sum;
		}
		return (n_sum*set_num)%mod;
	}
	else if (lr_break) {
		ans+=solve(lu_num,l,r,u,(u+d)/2);
		ans+=solve(lu_num,l,r,(u+d)/2,d);
	}
	else if (ud_break) {
		ans+=solve(lu_num,l,(l+r)/2,u,d);
		ans+=solve(lu_num,(l+r)/2,r,u,d);
	}
	else {
		ans+=solve(lu_num,l,(l+r)/2,u,(u+d)/2);
		ans+=solve(lu_num+(r-l)/2,(l+r)/2,r,u,(u+d)/2);
		ans+=solve(lu_num+(r-l)/2,l,(l+r)/2,(u+d)/2,d);
		ans+=solve(lu_num,(l+r)/2,r,(u+d)/2,d);
	}
	ans%=mod;
	return ans;
}

int main() {
	int Q;cin>>Q;
	while (Q--) {
		scanf("%d %d %d %d %d",&x1,&yy,&x2,&y2,&K);
		x1--;yy--;
		long long int  answer=solve(1,0,(1<<30),0,(1<<30));
		int aans=answer%mod;
		const char ch='/'+1;
		printf("%d\n",aans);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007
#define LL long long

LL qpow(LL x,LL y){
    LL e=1;
    while(y){
        if(y%2)e=(e*x)%MOD;
        x=x*x%MOD;
        y/=2;
    }
    return e;
}

LL low(LL x){

    for(LL i=1;i<=x;i<<=1){
        if(i<<1>x)
            return i;
    }
}

LL getS1(LL x,LL n){
    LL a1=(1+x)*x%MOD*qpow(2,MOD-2)%MOD*x%MOD;
    LL an=(a1+(x*x%MOD)*((n-1)*x%MOD))%MOD;
    return (a1+an)*n%MOD*qpow(2,MOD-2)%MOD;
}

LL getS2(LL x,LL y,LL n){
    return (y-x+1)*(x+y)%MOD*qpow(2,MOD-2)%MOD*n%MOD;
}

LL dfs(int a,int b,int k){

    if(a==0||b==0) return 0;

    LL l=min(a,b);
    LL r=max(a,b);

    LL x=low(l);

    LL r1=r/x;
    LL ans=0;

    if(k<=r){
        r1=k/x;
        if(k%x==0){

        }
        else if(r/x>k/x||r%x==0)
            ans=(ans+getS2(k-k%x+1,k,x))%MOD;
        else
            ans=(ans+getS2(k-k%x+1,k,r%x))%MOD;
    }
    else{
        if(r%x!=0)
            ans=(ans+getS2(r-r%x+1,r-r%x+x,r%x))%MOD;
    }
    if(k>x&&l>x){
        if(k<=l){
            ans=(ans+getS2(k-k%x+1,k,l%x))%MOD;
        }
        else{
            ans=(ans+getS2(l-l%x+1,l-l%x+x,l%x))%MOD;
        }
    }

    ans=(ans+getS1(x,r1))%MOD;
    return (ans+dfs(l-x,r-x,k))%MOD;
}

int main() {

    int N,x1,x2,y1,y2,k;
    scanf("%d", &N);
    for(int i=1;i<=N;i++){
        scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
        LL ans=dfs(x2,y2,k)-dfs(x1-1,y2,k)-dfs(x2,y1-1,k)+dfs(x1-1,y1-1,k);
        printf("%lld\n",ans%MOD);
    }
    return 0;
}

#include <algorithm>
#include <iostream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

int __gcd(int x, int y) { if(!y) return x; return __gcd(y, x % y); }

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define sag (k + k + 1)
#define sol (k + k)

#define type(x) __typeof(x.begin())

#define orta ((bas + son) >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
int mod = (int) 1e9 + 7;
const int logN = 18;
const int inf = 1e9 + 9;

const int N = 4e5 + 6;

int X, Y, K, q, l, r, a, b, k;
int dp[33][2][2][2];
int dp2[33][2][2][2];

int upd(int f, int c, int p) {
	if(f || (p < c)) return 1;
	return 0;
}

int ways(int bit, int f1, int f2, int f3) {
	if(bit == -1) return 1;
	int &r = dp2[bit][f1][f2][f3];
	if(r != -1) return r;
	int c1 = (X >> bit) & 1;
	if(f1) c1 = 1;
	int c2 = (Y >> bit) & 1;
	if(f2) c2 = 1;
	int c3 = (K >> bit) & 1;
	r = 0;
	FOR(p1, 0, c1)
		FOR(p2, 0, c2) {
			if(!f3 && c3 == 0 && (p1 ^ p2) == 1) continue;
			if(!f3 && (p1 ^ p2) < c3) {
				r = (r + ways(bit - 1, upd(f1, c1, p1), upd(f2, c2, p2), 1)) % mod;
			}
			else
				r = (r + ways(bit - 1, upd(f1, c1, p1), upd(f2, c2, p2), f3)) % mod;
		}
	return r;
}

int f(int bit, int f1, int f2, int f3) {
	if(bit == -1) return 0;
	int &r = dp[bit][f1][f2][f3];
	if(r != -1) return r;
	int c1 = (X >> bit) & 1;
	if(f1) c1 = 1;
	int c2 = (Y >> bit) & 1;
	if(f2) c2 = 1;
	int c3 = (K >> bit) & 1;
	r = 0;
	FOR(p1, 0, c1)
		FOR(p2, 0, c2) {
			if(!f3 && c3 == 0 && (p1 ^ p2) == 1) continue;
			if(!f3 && (p1 ^ p2) < c3) {
				r = (r + f(bit - 1, upd(f1, c1, p1), upd(f2, c2, p2), 1) + (1 << bit) * (ll) (p1 ^ p2) % mod * ways(bit - 1, upd(f1, c1, p1), upd(f2, c2, p2), 1)) % mod;
			}
			else
				r = (r + f(bit - 1, upd(f1, c1, p1), upd(f2, c2, p2), f3) + (1 << bit) * (ll) (p1 ^ p2) % mod * ways(bit - 1, upd(f1, c1, p1), upd(f2, c2, p2), f3)) % mod;
		}
	return r;
}

int get(int x, int y, int k) {
	X = x; Y = y; K = k;
	if(x == -1 || y == -1) return 0;
	memset(dp, -1, sizeof dp);
	memset(dp2, -1, sizeof dp2);
	return f(31, 0, 0, 0) + ways(31, 0, 0, 0);
}

int main() {

	scanf("%d", &q);

	FOR(i, 1, q) {
		scanf("%d %d %d %d %d", &l, &a, &r, &b, &k); l--; r--;  a--; b--; k--;
		int p1 = get(r, b, k);
		int p2 = get(r, a - 1, k);
		int p3 = get(l - 1, b, k);
		int p4 = get(l - 1, a - 1, k);
		int ans = ((p1 - p2 + mod) % mod);
		ans = (ans - p3 + mod) % mod;
		ans = (ans + p4) % mod;
		printf("%d\n", ans);
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;

int pot [40];
typedef long long ll;
int prod ( int a, int b ) {
    return int( (ll(a)*ll(b)) % ll(mod) );
}
struct ans{
    int n, s;
    void add( ans val ){
        n += val.n;
        n %= mod;
        s += val.s;
        s %= mod;
    }
    void mult ( ans val , int i ){
        n += val.n;
        n %= mod;
        s += (val.s + prod ( val.n , pot[i] ) ) ;
        s %= mod;
        //cout << " mult " << endl ;
    }
};

ans mem [35][2][2][2];
int vis [35][2][2][2];
int h, w, k, cont = 0;

int is_one( int x, int i ) {
    return (x>>i)&1;
}

ans dp ( int i , int same_h , int same_w , int same_k ){
    if( i == -1 ){
        ans r = ans{1,0};
        return r;
    }
    ans &ret = mem [i][same_h][same_w][same_k];
    if( vis[i][same_h][same_w][same_k] == cont ){
        return ret;
    }
    ret.n = ret.s = 0;
    /// Colocar 0 en x y 0 en y
    ret.add( dp( i-1 , same_h & (1^is_one(h,i)), same_w & (1^is_one(w,i)), same_k & (1^is_one(k,i)) ) );
    ///Colocar ( 1 , 1 )
    if( (!same_h or is_one(h,i)) and (!same_w or is_one(w,i)) ){
        ret.add( dp( i-1 , same_h & is_one(h,i) , same_w & is_one(w,i) , same_k & (1^is_one(k,i)) ) );
    }
    ///Colocar ( 1 , 0 )
    if( (!same_h or is_one(h,i)) and ( !same_k or is_one(k,i) ) ){
        ret.mult( dp( i-1 , same_h & is_one(h,i) , same_w & (1^is_one(w,i)) , same_k & is_one(k,i) ), i );
    }
    ///Colocar ( 0 , 1 )
    if( (!same_w or is_one(w,i)) and ( !same_k or is_one(k,i) ) ){
        ret.mult( dp( i-1 , same_h & (1^is_one(h,i)) , same_w & is_one(w,i) , same_k & is_one(k,i) ), i );
    }
    vis [i][same_h][same_w][same_k] = cont;
    return ret;
}

int count ( int x, int y ) {
    if ( x < 0 || y < 0 ) return 0;
    h = x;
    w = y;
    cont ++;
    ans a = dp( 31 , 1 , 1 , 1 );
    return (  a.s + a.n ) % mod;
}

int main(){
    pot[0] = 1;
    for( int i = 1 ; i < 35 ; i ++ )
        pot[i] = (2*pot[i-1]) % mod ;

    int q, x1, y1, x2, y2, res;
    scanf("%d",&q);
    for( int i = 0 ; i < q ; i ++ ){
        scanf( "%d%d%d%d%d" , &x1 , &y1 , &x2 , &y2 , &k );
        x1--;
        x2--;
        y2--;
        y1--;
        k--;

        res = count(x2,y2) - count(x2,y1-1) - count(x1-1,y2) + count(x1-1,y1-1);
        res = ( res % mod + mod ) % mod;
        printf("%d\n",res);
    }
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=1e6+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int complete(int start,int level,int k)
{
    int endd=(start+(1ll<<level)-1);
    if (k<start) return 0;
    else if (k<=endd) return ((1ll<<level)*((start+k)*(k-start+1)/2%mod)%mod);
    else return ((1ll<<level)*((start+endd)*(endd-start+1)/2%mod)%mod);
}

int f(int x,int y,int lx,int ly,int k,int start,int level)
{
    //cout<<x<<' '<<y<<' '<<lx<<' '<<ly<<' '<<k<<' '<<start<<' '<<level<<endl;
    if (x==0||y==0) return 0;
    if (x==(lx+(1ll<<level)-1)&&y==(ly+(1ll<<level)-1))
        return complete(start,level,k);

    int mid1=(lx+(1ll<<(level-1))-1),mid2=(ly+(1ll<<(level-1))-1);

    if (x<=mid1&&y<=mid2) return f(x,y,lx,ly,k,start,level-1);
    else if (x>mid1&&y<=mid2)
        return f(mid1,y,lx,ly,k,start,level-1)+f(x,y,mid1+1,ly,k,start+(1ll<<(level-1)),level-1);
    else if (x<=mid1&&y>mid2)
        return f(x,mid2,lx,ly,k,start,level-1)+f(x,y,lx,mid2+1,k,start+(1ll<<(level-1)),level-1);
    else return f(mid1,mid2,lx,ly,k,start,level-1)+f(x,mid2,mid1+1,ly,k,start+(1ll<<(level-1)),level-1)
                +f(mid1,y,lx,mid2+1,k,start+(1ll<<(level-1)),level-1)+f(x,y,mid1+1,mid2+1,k,start,level-1);
}

int32_t main()
{
    IOS
    int q;
    cin>>q;
    while (q--)
    {
        int a,b,c,d,k;
        cin>>a>>b>>c>>d>>k;
        cout<<f(c,d,1,1,k,1,35)-f(a-1,d,1,1,k,1,35)-f(c,b-1,1,1,k,1,35)+f(a-1,b-1,1,1,k,1,35)<<endl;
    }
}

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int mod = 1e9 + 7;
long long f[100][3][3][3], g[100][3][3][3];

long long calc(long long a, long long b, long long k) {
	if (a < 0 || b < 0) return 0;
	memset(f, 0, sizeof(f));
	memset(g, 0, sizeof(g));
	f[0][1][1][1] = 1;
	g[0][1][1][1] = 0;
	for (int i = 1; i <= 31; ++i) {
		for (int x = 0; x <= 1; ++x)
		for (int X = 0; X <= 1; ++X) {
			int flagx = 0;
			if (a & (1 << (31 - i))) flagx = 1;
			if (x == 1 && X == 1 && !flagx) continue;
			int xx = (x && (X == flagx));
			for (int y = 0; y <= 1; ++y)
			for (int Y = 0; Y <= 1; ++Y) {
				int flagy = 0;
				if (b & (1 << (31 - i))) flagy = 1;
				if (y == 1 && Y == 1 && !flagy) continue;
				int yy = (y && (Y == flagy));
				for (int z = 0; z <= 1; ++z) {
					int Z = (X ^ Y);
					int flagz = 0;
					if (k & (1 << (31 - i))) flagz = 1;
					if (z == 1 && Z == 1 && !flagz) continue;
					int zz = (z && (Z == flagz));
					f[i][xx][yy][zz] = (f[i][xx][yy][zz] + f[i - 1][x][y][z]) % mod;
					g[i][xx][yy][zz] = (g[i][xx][yy][zz] + g[i - 1][x][y][z] + f[i - 1][x][y][z] * (Z << (31 - i))) % mod;
				}
			}
		}
	}
	long long ans = 0;
	for (int x = 0; x <= 1; ++x) {
		for (int y = 0; y <= 1; ++y) {
			for (int z = 0; z <= 1; ++z) {
				ans = (ans + f[31][x][y][z] + g[31][x][y][z]) % mod;
			}
		}
	}
	return ans;
}

int main() {
	//int x, y, z;
	//while (cin >> x >> y >> z)
	//cout << calc(x - 1, y - 1, z - 1) << endl;
	int q;
	scanf("%d", &q);
	for (int i = 1; i <= q; ++i) {
		int x, y, X, Y, K;
		scanf("%d%d%d%d%d", &x, &y, &X, &Y, &K);
		long long ans = (calc(X - 1, Y - 1, K - 1) - calc(X - 1, y - 2, K - 1)
						- calc(x - 2, Y - 1, K - 1) + calc(x - 2, y - 2, K - 1) + mod * 4) % mod;
		printf("%I64d\n", ans);
	}
	return 0;
}

//marico el que lo lea
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <stdlib.h>
using namespace std;

void fastIO() {
	std::ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
}

#define FOR(i,f,t) for(int i=f; i<(int)t; i++)
#define FORR(i,f,t) for(int i=f; i>(int)t; i--)
#define pb push_back
#define all(obj) obj.begin(), obj.end()
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define ms2(obj, val, sz) memset(obj, val, sizeof(obj[0])*sz)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rii(x,y) ri(x), ri(y)
#define rll(x,y) rl(x), rl(y)

#define fst first
#define snd second
#define dprintf debug && printf
#define debug false

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;

const ll MOD = 1e9+7;
int X, Y, K;

ll dps[31][2][2][2], dpc[31][2][2][2];
ll fc(int b, int lx, int ly, int lk){
	//printf("%d %d %d %d\n",b,lx,ly,lk);
	if(b==-1) return 1;
	ll &ret = dpc[b][lx][ly][lk];
	//printf("   %lld\n",ret);
	if(ret!=-1) return ret;
	ret = 0;

	//printf("%d %d %d %d\n",b,lx,ly,lk);
	FOR(bx,0,2) FOR(by,0,2){
		int bk=bx^by, BX=(X>>b)&1, BY=(Y>>b)&1, BK=(K>>b)&1;
		if(bk > BK && !lk) continue;
		if(bx > BX && !lx) continue;
		if(by > BY && !ly) continue;
		//printf("%d %d %d\n",bx,by,bk);
		ret = (ret + fc(b-1,lx|(bx<BX),ly|(by<BY),lk|(bk<BK)))%MOD;
	}
	return ret;
}
ll fs(int b, int lx, int ly, int lk){
	if(b==-1) return 0;
	ll &ret = dps[b][lx][ly][lk];
	if(ret!=-1) return ret;
	ret = 0;
	FOR(bx,0,2) FOR(by,0,2){
		int bk=bx^by, BX=(X>>b)&1, BY=(Y>>b)&1, BK=(K>>b)&1;
		if(bk > BK && !lk) continue;
		if(bx > BX && !lx) continue;
		if(by > BY && !ly) continue;
		ret = (ret + (bk<<b)*fc(b-1,lx|(bx<BX),ly|(by<BY),lk|(bk<BK))%MOD+fs(b-1,lx|(bx<BX),ly|(by<BY),lk|(bk<BK)))%MOD;
	}
	return ret;
}
ll query(int x, int y, int k){
	X = x;
	Y = y;
	K = k-1;
	ms(dps,-1);
	ms(dpc,-1);
	return (fc(30,0,0,0) + fs(30,0,0,0))%MOD;
}

ll query(int x1, int y1, int x2, int y2, int k){
	ll ret = query(x2, y2, k);
	if(x1>0) ret = (ret - query(x1-1, y2, k))%MOD;
	if(y1>0) ret = (ret - query(x2, y1-1, k))%MOD;
	if(x1>0 && y1>0) ret = (ret + query(x1-1, y1-1, k))%MOD;
	return (ret%MOD +MOD)%MOD;
}

int main(){
	int q; ri(q);
	while(q--){
		int x1, x2, y1, y2, k;
		rii(x1,y1); rii(x2,y2); ri(k);
		x1--; x2--; y1--; y2--; k--;
		printf("%lld\n",query(x1,y1,x2,y2,k));
	}
}

#include <bits/stdc++.h>
#define y1 FUCK_THE_Y1
#define mod 1000000007
using namespace std;

inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
	return x*f;
}
inline void Add(int &x,int y)
{
	x+=y; if(x>=mod) x-=mod;
	if(x<0) x+=mod;
}
int lst1[40],lst2[40],lst3[40],sl;
int sum[40][2][2][2],cnt[40][2][2][2];
inline pair<int,int> Ask(int l,int s1,int s2,int s3)
{
	if(l==0) return make_pair(0,1);
	if(cnt[l][s1][s2][s3]!=-1)
		return make_pair(sum[l][s1][s2][s3],cnt[l][s1][s2][s3]);
	int t1=s1? lst1[l]:1;
	int t2=s2? lst2[l]:1;
	int t3=s3? lst3[l]:1;
	sum[l][s1][s2][s3]=cnt[l][s1][s2][s3]=0;
	for(int i=0;i<=t1;i++) for(int j=0;j<=t2;j++)
		for(int k=0;k<=t3;k++) if((i^j)==k)
		{
			pair<int,int> tmp=Ask(l-1,s1&(i==t1),s2&(j==t2),s3&(k==t3));
			if(k==1) Add(sum[l][s1][s2][s3],1ll*(1ll<<(l-1))*tmp.second%mod);
			Add(sum[l][s1][s2][s3],tmp.first);
			Add(cnt[l][s1][s2][s3],tmp.second);
		}
	return make_pair(sum[l][s1][s2][s3],cnt[l][s1][s2][s3]);
}
inline int calc(int x,int y,int k)
{
	memset(lst1,0,sizeof lst1);
	memset(lst2,0,sizeof lst2);
	memset(lst3,0,sizeof lst3);
	memset(sum,-1,sizeof sum);
	memset(cnt,-1,sizeof cnt);
	if(!x||!y) return 0;
	x--; y--; k--;
	while(x) lst1[++lst1[0]]=x&1,x/=2;
	while(y) lst2[++lst2[0]]=y&1,y/=2;
	while(k) lst3[++lst3[0]]=k&1,k/=2;
	sl=max(max(lst1[0],lst2[0]),lst3[0]);
	pair<int,int> rec=Ask(sl,1,1,1);
	return rec.first+rec.second;
}
int main()
{
//	freopen("read.in","r",stdin);
	int n=read();
	for(int i=1;i<=n;i++)
	{
		int x1=read(), y1=read(),
			x2=read(), y2=read(), k=read();
		int Ret=calc(x1-1,y1-1,k);
		Add(Ret,-calc(x1-1,y2,k));
		Add(Ret,-calc(x2,y1-1,k));
		Add(Ret,calc(x2,y2,k));
		cout << Ret << endl;
	}
	return 0;
}

//  Created by Sengxian on 2017/05/24.
//  Copyright (c) 2017年 Sengxian. All rights reserved.
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
inline int readInt() {
	static int n, ch;
	n = 0, ch = getchar();
	while (!isdigit(ch)) ch = getchar();
	while (isdigit(ch)) n = n * 10 + ch - '0', ch = getchar();
	return n;
}

const int MAX_N = 200 + 3;
int n;
ll a[MAX_N][MAX_N];

int mex(int i, int j) {
	static bool vis[MAX_N * 2];
	memset(vis, 0, sizeof vis);
	for (int k = 1; k < i; ++k) vis[a[k][j]] = true;
	for (int k = 1; k < j; ++k) vis[a[i][k]] = true;
	for (int k = 1; k < MAX_N * 2; ++k) if (!vis[k]) return k;
}

void printBit(int x) {
	for (int i = 15; i >= 0; --i)
		printf("%d", x >> i & 1);
	cout << endl;
}

pair<ll, ll> operator + (const pair<ll, ll> &lhs, const pair<ll, ll> &rhs) {
	return make_pair(lhs.first + rhs.first, lhs.second + rhs.second);
}

pair<ll, ll> operator - (const pair<ll, ll> &lhs, const pair<ll, ll> &rhs) {
	return make_pair(lhs.first - rhs.first, lhs.second - rhs.second);
}

pair<ll, ll> operator * (const pair<ll, ll> &lhs, const int &x) {
	return make_pair(lhs.first + lhs.second * x, lhs.second);
}

pair<ll, ll> calc(ll n, ll k) {
	if (k >= n) return make_pair((n + 1) * n / 2 * n, n * n);
	return make_pair((k + 1) * k / 2 * n, n * k);
}

pair<ll, ll> solve(int x, int y, int k) {
	if (x <= 0 || y <= 0 || k <= 0) return make_pair(0, 0);
	if (x == 1 && y == 1) return k >= 1 ? make_pair(1, 1) : make_pair(0, 0);
	if (x < y) swap(x, y);

	int logX = ceil(log2(x)), halfN = 1 << (logX - 1);
	if (x <= n && y <= n && k >= (1 << logX)) return make_pair(a[x][y], (ll)x * y);

	if (y <= halfN) {
		return solve(halfN, y, k) + (solve(x - halfN, y, k - halfN) * halfN);
	} else {
		return calc(halfN, k) + solve(x - halfN, y - halfN, k) + solve(x - halfN, halfN, k - halfN) * halfN + solve(halfN, y - halfN, k - halfN) * halfN;
	}
}

int main() {
#ifdef DEBUG
	freopen("test.in", "r", stdin);
#endif
	n = 200;
	a[1][1] = 1;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= n; ++j)
			a[i][j] = mex(i, j);
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= n; ++j)
			a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];

	int q = readInt();
	while (q--) {
		int x1 = readInt(), y1 = readInt(), x2 = readInt(), y2 = readInt(), k = readInt();
		pair<ll, ll> ans = solve(x2, y2, k) - solve(x2, y1 - 1, k) - solve(x1 - 1, y2, k) + solve(x1 - 1, y1 - 1, k);
		cout << ans.first << endl;
	}

	return 0;
	/*
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j)
			printf("%4d", a[i][j]);
		cout << endl;
	}
	for (int k = 1; k <= 16; ++k) {
		puts("-------------------------------------------------------------------------");
		printf("%d ", k);
		printBit(k);
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				if (a[i][j] == k) printf("x ");
				else printf("  ");
//				printf("%d ", a[i][j] == k);
			}
			cout << endl;
		}
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j)
				if (a[i][j] == k) cout << i << ' ' << j << ' ' << (i ^ j ^ abs(i - j)) << endl;

	}
	*/
	return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
using namespace std;
typedef long long int LL;
typedef pair<LL,LL> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
	if(vec_n==-1)vec_n=vec_s.size();
	for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
	if(vec_n==-1)vec_n=vec_s.size();
	for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}

P calc(LL x,LL y,LL t){
	if(x>y)swap(x,y);
	if(x==0)return make_pair(0,0);
	int i,j,k;
	LL a,b,c;
	P pa;
	for(i=0;(1<<i)<=y;i++);
	i--;
	a=1<<i;
	if(t<a){
		b=t*(t+1)/2%MAX*min(x,a)%MAX;
		if(a<x)pa=calc(x-a,y-a,t);
		return make_pair(b+pa.first,t*min(x,a)+pa.second);
	}
	if(a<x){
		b=a*(a+1)/2%MAX*a%MAX;
		if(t<2*a){
			b+=(t+a+1)*(t-a)/2%MAX*(x+y-2*a)%MAX;
			c=a*a+(x+y-2*a)*(t-a);
		}else{
			b+=(3*a+1)*a/2%MAX*(x+y-2*a)%MAX;
			c=(x+y-a)*a;
		}
		pa=calc(x-a,y-a,t);
		b+=pa.first;
		pa.second%=MAX;
		return make_pair(b%MAX,c+pa.second);
	}
	b=a*(a+1)/2%MAX*x%MAX;
	pa=calc(x,y-a,t-a);
	b+=pa.first;
	pa.second%=MAX;
	b+=a*pa.second;
	return make_pair(b%MAX,x*a+pa.second);
}

int main(){
	int n,m;
	int i,j,k;
	LL a,b,c,d,e;
	LL s=0;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>a>>b>>c>>d>>e;
		s=calc(c,d,e).first-calc(a-1,d,e).first-calc(c,b-1,e).first+calc(a-1,b-1,e).first;
		if(s<0)s+=2*MAX;
		s%=MAX;
		cout<<s<<endl;
	}
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = (1e9)+7;

pair<ll, ll> query(int x, int y, int k, int lim) {
	if(x <= 0 || y <= 0 || k < 0 || lim <= 0) return {0, 0};
	int pw = 1 << k;
	ll cnt = 0, sum = 0;
	if(x >= pw || y >= pw) {
		int val_cnt = min(pw, lim), node_cnt = min(pw, min(x, y));
		cnt = 1ll * val_cnt * node_cnt % MOD;
		sum = 1ll * (val_cnt * (val_cnt + 1) / 2 % MOD) * node_cnt;
	} else {
		pw >>= 1;
		pair<ll, ll> res[2][2] = {
			query(x, y, k-1, lim),
			query(x - pw, y, k-1, lim - pw),
			query(x, y - pw, k-1, lim - pw),
			query(x - pw, y - pw, k-1, lim)
		};
		cnt = (res[0][0].second + res[0][1].second + res[1][0].second + res[1][1].second) % MOD;
		sum = (res[0][0].first + res[0][1].first + res[1][0].first + res[1][1].first) % MOD;
		sum = (sum + (res[0][1].second + res[1][0].second) * pw) % MOD;
	}
	return {sum, cnt};
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while(q--) {
		int x1, y1, x2, y2, lim;
		cin >> x1 >> y1 >> x2 >> y2 >> lim;
		int k = 0;
		while(1ll << k <= max(x2, y2)) ++k;
		ll res = query(x2, y2, k, lim).first
					 - query(x1 - 1, y2, k, lim).first
					 - query(x2, y1 - 1, k, lim).first
					 + query(x1 - 1, y1 - 1, k, lim).first;
		cout << res << '\n';
	}
}

#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")

#define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>

using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_set>
# include <unordered_map>
# else
# include <tr1/unordered_map>
# include <tr1/unordered_set>
using namespace tr1;
# endif
# else
# include <unordered_map>
# include <unordered_set>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n"
#define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n"

#ifdef _MSC_VER
#define ALIGN(x) __declspec(align(x))
#else
#define ALIGN(x) __attribute__((aligned(x)))
#endif

#ifdef LOCAL
#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
#else
#define CURTIME() ;
#endif
double __begin;
#define DTIME(ccc) __begin = clock(); ccc; cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef int itn;

template<class T1, class T2, class T3>
struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };
template<class T1, class T2, class T3>
bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;}
template<class T1, class T2, class T3>
bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;}
#define tri triple<int,int,int>
#define trll triple<ll,ll,ll>

#define FI(n) for(int i=0;i<n;++i)
#define FJ(n) for(int j=0;j<n;++j)
#define FK(n) for(int k=0;k<n;++k)
#define FL(n) for(int l=0;l<n;++l)
#define FQ(n) for(int q=0;q<n;++q)
#define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i)
#define all(a) std::begin(a), std::end(a)
#define reunique(v) v.resize(unique(v.begin(), v.end()) - v.begin())

#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(1.0 * (x))
#define pow(x, n) pow(1.0 * (x), n)

#define COMPARE(obj) [&](const std::decay<decltype(obj)>::type& a, const std::decay<decltype(obj)>::type& b)
#define COMPARE_BY(obj, field) [&](const std::decay<decltype(obj)>::type& a, const std::decay<decltype(obj)>::type& b) { return a.field < b.field; }

#define checkbit(n, b) (((n) >> (b)) & 1)
#define setbit(n, b) ((n) | (static_cast<decltype(n)>(1) << (b)))
#define removebit(n, b) (~setbit(~(n), (b)))
inline int bits_count(int v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int bits_count(ll v){int t=v>>32;int p=(v& ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
template<class T>
inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; }
inline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); }
unsigned long long rdtsc() {
    unsigned long long ret;
    __asm__ __volatile__("rdtsc" : "=A" (ret) : :);
    return ret;
}

//STL output *****************************************************************************************************
#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p);
TT1 ostream& operator << (ostream& os, const vector<T>& v);
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v);
TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }
TT1T2 void printarray(T1 a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }

//STL input *****************************************************************************************************
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t);
TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
TT1 inline istream& operator >> (istream& os, vector<T>& v) {
    if (v.size()) for (T& t : v) os >> t; else {
        string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;}
        stringstream ss(s); while (ss >> obj) v.push_back(obj);
    }
    return os;
}
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }

//Pair magic *****************************************************************************************************
#define PT1T2 pair<T1, T2>
TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }

#undef TT1
#undef TT1T2
#undef TT1T2T3

#define FREIN(FILE) freopen(FILE, "rt", stdin)
#define FREOUT(FILE) freopen(FILE, "wt", stdout)

template<class T> bool isPrime(T x) { if (x < 2) return 0; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return 0; return 1; }
inline void normmod(ll &x, ll m) { x %= m; if (x < 0) x += m; }
inline ll summodfast(ll x, ll y, ll m) { ll res = x + y; if (res >= m) res -= m; return res; }
inline int summodfast(int x, int y, int m) { int res = x + y; if (res >= m) res -= m; return res; }
inline void addmodfast(ll &x, ll y, ll m) { x += y; if (x >= m) x -= m; }
inline void addmodfast(int &x, int y, int m) { x += y; if (x >= m) x -= m; }
inline void submodfast(ll &x, ll y, ll m) { x -= y; if (x < 0) x += m; }
inline void submodfast(int &x, int y, int m) { x -= y; if (x < 0) x += m; }
inline ll mulmod(ll x, ll n, ll m){ ll res = 0; normmod(x, m); normmod(n, m); while (n){ if (n & 1) res = summodfast(res, x, m); x = summodfast(x, x, m); n >>= 1; } return res; }
inline ll powmod(ll x, ll n, ll m){ ll res = 1; while (n){ if (n & 1)res = (res*x) % m; x = (x*x) % m; n >>= 1; }return res; }
inline ll gcd(ll a, ll b){ ll t; while (b){ a = a%b; t = a; a = b; b = t; }return a; }
inline int gcd(int a, int b){ int t; while (b){ a = a%b; t = a; a = b; b = t; }return a; }
inline ll lcm(ll a, ll b){ return a / gcd(a, b)*b; }
inline ll gcd(ll a, ll b, ll c){ return gcd(gcd(a, b), c); }
inline int gcd(int a, int b, int c){ return gcd(gcd(a, b), c); }
ll gcdex(ll a, ll b, ll& x, ll& y) {
    if (!a) { x = 0; y = 1; return b; }
    ll y1;
    ll d = gcdex(b % a, a, y, y1);
    x = y1 - (b / a) * y;
    return d;
}

// Useful constants

//int some_primes[10] = {24443, 100271, 500179, 1000003, 1000333, 2000321, 5000321, 98765431, 1000000123};
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define EPS         (double)1e-10
#define mod         1000000007
#define PI          3.14159265358979323
#define link        asaxlajrewqwe
#define rank        wahayawehasdakw
//*************************************************************************************

void add(ll& x, ll y) {
    x += y;
    if (x >= mod) x -= mod;
}
void sub(ll& x, ll y) {
    x -= y;
    if (x < 0) x += mod;
}
ll solve2(ll x, ll y, ll k, ll bonus = 0) {
    if (x <= 0 || y <= 0 || k <= 0) return 0;
    if (x == y && (x & (x - 1)) == 0) {
        k = min(k, x);
        return (k * (k + 1) % mod * x + bonus * k * 2 % mod * x) % mod;
    }
    if (x < y) swap(x, y);
    ll ty = 1;
    while (ty * 2 <= y) {
        ty *= 2;
    }
    ll tx = 1;
    while (tx * 2 <= x) {
        tx *= 2;
    }
    ll res;
    if (tx == ty) {
        ll txk = min(tx, k);
        res = txk * (txk + 1) % mod * ty + bonus * txk * 2 % mod * ty;
        res %= mod;
        add(res, solve2(x - tx, tx, k - tx, bonus + tx));
        add(res, solve2(y - tx, tx, k - tx, bonus + tx));
        add(res, solve2(x - tx, y - tx, k, bonus));
    } else {
        ll txk = min(tx, k);
        res = txk * (txk + 1) % mod * y + bonus * txk * 2 % mod * y;
        res %= mod;
        add(res, solve2(x - tx, y, k - tx, bonus + tx));
    }
    return res;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef LOCAL
    FREIN("input.txt");
//    FREOUT("output.txt");
#endif
    int q;
    cin >> q;
    while (q--) {
        int x1, y1, x2, y2, k;
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        ll ans = solve2(x2, y2, k) - solve2(x1 - 1, y2, k) - solve2(x2, y1 - 1, k) + solve2(x1 - 1, y1 - 1, k);
        if (ans < 0) {
            ans += mod;
        }
        ans %= mod;
        cout << ans * powmod(2, mod - 2, mod) % mod << endl;
    }
    CURTIME();
    return 0;
}

/*
    ┬┴┬┴┤(･_├┬┴┬┴ hiding from those WAs
*/

#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> pii;
typedef pair<pii,pii> rect;
const int mod = (int) 1e9+7;
#define x first
#define y second
#define fi first
#define se second

rect cut(rect a,rect b){
	return rect(pii(max(a.x.fi, b.x.fi), min(a.x.se, b.x.se)),
				pii(max(a.y.fi, b.y.fi), min(a.y.se, b.y.se)));
}
bool inside(rect a,rect b){
	return cut(a, b) == a;
}

int query(rect cur, rect field, int lim, int rem){
	if (cur.x.first > cur.x.second
		|| cur.y.first > cur.y.second)
		return 0;
	if (rem+1 > lim)
		return 0;

	//cout << cur.x.fi << ' ' << cur.x.se << ' ' << cur.y.fi << ' ' << cur.y.se << endl;
	//cout << field.x.fi << ' ' << field.x.se << ' ' << field.y.fi << ' ' << field.y.se << ' ' << rem << endl;

	if (inside(field, cur)){
		int len = cur.x.second - cur.x.first + 1;
		int okval = min(len, lim - rem);

		//cout << "inside\n " << 1ll * rem * len * len + 1ll * len * okval * (okval + 1) / 2;
		return (1ll * rem * len % mod * len + 1ll * okval * (okval + 1) / 2 % mod * len) % mod;
	}

	int dim = __builtin_ctz(field.x.second - field.x.first + 1);

	int xmid = field.x.first + (1<<(dim-1));
	int ymid = field.y.first + (1<<(dim-1));
	int xout = field.x.first + (1<<(dim));
	int yout = field.y.first + (1<<(dim));

	rect topleft  = {{field.x.first, xmid - 1}, {field.y.first, ymid - 1}};
	rect topright = {{field.x.first, xmid - 1}, {ymid, 			yout - 1}};
	rect botleft  = {{xmid, 		 xout - 1}, {field.y.first, ymid - 1}};
	rect botright = {{xmid, 		 xout - 1}, {ymid, 			yout - 1}};

	return  (0ll + query(cut(cur, topleft) , topleft,  lim, rem)
		+	query(cut(cur, topright), topright, lim, rem + (1<<(dim-1)))
		+	query(cut(cur, botleft) , botleft,  lim, rem + (1<<(dim-1)))
		+ 	query(cut(cur, botright), botright, lim, rem)) % mod;
}
void ngk(){
    int q;
    cin >> q;
    while(q--){
    	int x1, y1, x2, y2, k;
    	cin >> x1 >> y1 >> x2 >> y2 >> k;
    	rect ask = {{x1, x2}, {y1, y2}};
    	rect field = {{1, (1<<30)}, {1, (1<<30)}};

    	cout << query(ask, field, k, 0) << '\n';
    }
}

int main(int argc, char *argv[]){
    iostream::sync_with_stdio(0);
    int test = 1;
    while(test--){
        ngk();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

using db = double;
using ll = long long;
using vi = vector <int>;
#define op operator
#define pb push_back

const ll mod = 1e9L + 7;

ll f(ll x, ll y, ll k, ll offset) {
	//cout << x << ' ' << y << ' ' << k << ' ' << offset << '\n';
	k -= offset;
	if(x > y) swap(x, y);
	if(k >= y) k = y;
	if(k <= 0) return 0;
	//cout << x << ' ' << y << ' ' << k << ' ' << offset << ' ' << (k * (1 + k) / 2 % mod * x % mod + x * y % mod * offset % mod) << '\n';
	return (k * (1 + k) / 2 % mod * x % mod + k * x % mod * offset % mod) % mod;
}

ll get(ll n, ll m) {
	return n ^ m;
	if(n > m) swap(n, m);
	if(n == m) return 0;
	//if(n * 2 == m) return n;
	return n + m;
}

ll solve(ll x, ll y, ll k, ll offset = 0) {
	//cout << x << ' ' << y << '\n';
	//if(x && y) cout << x << ' ' << y << ' ' << k << ' ' << offset << '\n';
	if(x > y) swap(x, y);
	if(x == 0) return 0;
	ll n = 1, m = 1;
	while(n * 2 <= x) n *= 2;
	while(m * 2 <= y) m *= 2;
	//cout << n << ' ' << m << '\n';

	ll r = f(n, m, k, offset);

	if(n == m) {
		r += solve(x - n, m, k, offset ^ n);
		r += solve(n, y - m, k, offset ^ m);
		r += solve(x - n, y - m, k, offset);
	} else {

		r += solve(x - n, m, k, offset);
		r += solve(n, y - m, k, offset ^ m);
		r += solve(x - n, y - m, k, offset ^ get(n, m));
	}
	return r % mod;
}

int main() {

	//cout << solve(2, 5, 2) << '\n';

	cout << fixed << setprecision(9);
	ios :: sync_with_stdio(0);
	/*
	for(int i : {1, 2, 4})
		for(int j : {1, 2, 4}) cout << get(i, j) << '\n';
*/

	int q; cin >> q;
	while(q --) {
		ll x1, x2, y1, y2, k;
		cin >> x1 >> y1 >> x2 >> y2 >> k;
		//cout << "------\n";
		//cout << x1 << ' ' << x2 << ' ' << y1 << ' ' << y2 << ' ' << k << '\n';

		//cout << solve(x2, y2, k) << ' ' << solve(x1 - 1, y2, k) << ' ' << solve(x2, y1 - 1, k) << ' ' << solve(x1 - 1, y1 - 1, k) << '\n';

		ll ans = solve(x2, y2, k) - solve(x1 - 1, y2, k) - solve(x2, y1 - 1, k) + solve(x1 - 1, y1 - 1, k);
		ans = (ans % mod + mod) % mod;
		cout << ans << '\n';
	}

	return 0;
}

#include <bits/stdc++.h>
#define FO(i,a,b) for (int i = (a); i < (b); i++)
#define sz(v) int(v.size())

using namespace std;

int main() {
}

#include <bits/stdc++.h>
using namespace std;
long long  n, a[300007], r[300007],  mod = 1000000007, sum=0;
int main(){
  std::cin >> n;
  r[0] = 1;
  for(int i = 0; i<n; i++)  cin >> a[i], r[i+1] = (r[i]*2)%mod;
  sort(a,a+n);
  for(int i = 0; i<n; i++){
    sum+=(a[i]*(r[i]-r[n-i-1]))%mod;
    sum%=mod;
  }
  std::cout << sum%mod << '\n';
}

#include<algorithm>
#include<stdio.h>
using namespace std;
const int mod=1e9+7;
int UP(int x) //二进制最高位
{
	int l=0,r=32;
	int ans=0;
	while (l<=r){
		int mid=(l+r)>>1;
		if (!(x>>mid)){
			ans=mid;
			r=mid-1;
		}else l=mid+1;
	}
	return ans;
}
int qury(int x,int y,int k,int va)
{
	if (x==0||y==0)return 0;
	int up1=UP(x)-1;
	int up2=UP(y)-1;
	int ret=0;
	int n=min(k,max(1<<up1,1<<up2));
		ret=((1LL*n*(n+1)/2)%mod*min(1<<up1,1<<up2))%mod;
		ret=(ret+(n*min(1<<up1,1<<up2)%mod*va%mod))%mod;
		if (k-(1<<up1)>0)	ret=(ret+qury(x-(1<<up1),1<<up2,k-(1<<up1),va+(1<<up1)))%mod;
		if (k-(1<<up2)>0)	ret=(ret+qury(1<<up1,y-(1<<up2),k-(1<<up2),va+(1<<up2)))%mod;
	ret=(ret+qury(x-(1<<up1),y-(1<<up2),k,va))%mod;
	return ret;
}
main()
{
	int x1,y1,x2,y2;
	int k;
	int n;
	scanf("%d",&n);
	while (n--){
		scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
		int q1=qury(x1-1,y1-1,k,0);
		int q2=qury(x2,y1-1,k,0);
		int q3=qury(x1-1,y2,k,0);
		int q4=qury(x2,y2,k,0);
		//printf("%d_%d_%d_%d ",q1,q2,q3,q4);
		int ans=(((q4+q1)%mod-(q2+q3)%mod)%mod+mod)%mod;
		printf("%d\n",ans);
	}
}

//Be Name Khoda
/*
tane adami sharifast be jane adamiat
na hamin lebase zibast neshane adamiat
agar adami be cheshmasto dahano goosho bini
che miane naghshe divaro miane adamiat
khoro khabo khashmo shahvat shaghab asto jahlo zolmat
hayavan khabar nadarad ze jahane adamiat
be haghighat adami bash vagarna morgh bashad
ke hami sokhan begooyad be zabane adamiat
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using ordered_multiset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long LL;
typedef LL ll;
typedef long double ld;
typedef string str;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;

#define X first
#define Y second
#define PB push_back
#define For(i,a,b) for (ll i=a;i<b;++i)
#define Ford(i,a,b) for (ll i=a;i>=b;--i)
#define smax(a,b) a=max(a,b)
#define smin(a,b) a=min(a,b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a,n,x) (lower_bound(a,(a)+(n),x)-(a))
#define RET(x) { cout << x; exit(0); }
#define MKP make_pair
#define RETD(x) { cout << fixed << setprecision(15) << x; } //exit(0); }
#define GEI(x) scanf("%d",&x)
#define GEL(x) scanf("%lld",&x)
#define ALL(x) x.begin(),x.end()
const ll M=1e5+5,LG=32,SM=1000,inf=1e9+5;
ll mod=1e5+7;
ll pw(ll a,ll n) { return (n==0?1:pw(a*a%mod,n/2)*(n%2?a:1)%mod); }
#define MOK(x) if (x>mod) x-=mod;

pll mrg(pll a,pll b,ll f){
  pll r;
  r.Y=(a.Y+b.Y)%mod;
  r.X=(a.X+b.X+b.Y*f)%mod;
  return r;
}

pll contb1(ll y,ll k){
  ll f=min(k,y);
  ll f2=(f*(f+1)/2)%mod;
  return {y*f2%mod,(y*f)%mod};
}

pll contb2(ll mx,ll y,ll k){
  ll f=min(k,mx);
  ll f2=(f*(f+1)/2)%mod;
  return {y*f2%mod,(y*f)%mod};
}

pll cont(ll x,ll y,ll k){
  //ER(x);ER(y);ER(k);
  if (min(x,y)==0 || k<=0) return {0,0};
  if (x<y) swap(x,y);
  if (y==1){
    smin(x,k);
    return {(x*(x+1)/2)%mod,x};
  }
  ll mx=0,my=0;
  For(i,0,LG){
    if ((1<<i)&x) mx=(1<<i);
    if ((1<<i)&y) my=(1<<i);
  }
  if (mx==x && my==x){
     return contb1(y,k);
  }
  if (mx==x) mx/=2;
  if (my==y) my/=2;
  if (mx!=x || my!=y){
    if (mx==my){
      pll a=contb1(mx,k);
      pll b=cont(x-mx,y-mx,k);
      a.X+=b.X;
      a.Y+=b.Y;
      pll c=contb2(mx,x-mx,k-mx);
      pll d=contb2(mx,y-mx,k-mx);
      c.X+=d.X;
      c.Y+=d.Y;
      return mrg(a,c,mx);
    }else{
      return mrg(cont(mx,y,k),cont(x-mx,y,k-mx),mx);
    }
  }
  //ER(x);ER(y);ER(k);ER(y*min(y,k));
}

int main()
{
  ios::sync_with_stdio(0);
  ll q;
  cin >> q;
  while (q--){
    ll x1,y1,x2,y2,k;
    cin >> x1 >> y1 >> x2 >> y2 >> k;
    //x1=rand()%mod;x2=rand()%mod;y1=rand()%mod;y2=rand()%mod;k=rand()%mod;
    if (x1>x2) swap(x1,x2);
    if (y1>y2) swap(y1,y2);
    x1--;y1--;
    //ER(x2);
    ll t=cont(x2,y2,k).X-cont(x2,y1,k).X-cont(x1,y2,k).X+cont(x1,y1,k).X;
    t%=mod;
    t+=mod;
    t%=mod;
    cout << t << '\n';
  }
}

/// a.cpp

# include <stdio.h>
# include <bits/stdc++.h>
using namespace std;
const pair < int , int > DD[] = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
# define fi cin
# define fo cout
# define x first
# define y second
# define ll long long
# define IOS ios_base :: sync_with_stdio(0);cin.tie(0)
# define p(v) cerr << #v << " = " << v << '\n'
# define p2(v) cerr << #v << " = " << (complex < __typeof(v.x) > (v.x,v.y)) << '\n'
# define vi vector < int >
# define vl vector < ll >
# define pll pair < ll , ll >
# define pii pair < int , int >
# define mp make_pair
# define db long double
# define fail puts("-1")
# define yes puts("YES")
# define no puts("NO")
# define PP puts("Possible")
# define II puts("Impossible")
# define vii vector < pii >
# define vll vector < pll >
# define pb push_back
# define pdd pair < db , db >
# define int ll
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
int32_t main(void)
{
    #ifdef CF
    freopen("input","r",stdin);
    #endif // CF
    srand(time(0));
    fo << fixed << setprecision(7);
    cerr << fixed << setprecision(7);
    int t;
    const int mod = 1e9 + 9;
    auto add = [&](auto &x,auto y)
    {
        x += y;
        if (x >= mod)
            x -= mod;
    };
    auto mult = [&](auto x,auto y)
    {
        auto ans = (1ll * x * y) % mod;
        return ans;
    };
    auto query = [&](int x,int y,int k)
    {
        if (k < 0 || x < 0 || y < 0)
            return 0ll;
        static ll dp1[55][2][2][2];
        static ll dp2[55][2][2][2];
        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));
        dp1[31][1][1][1] = 1;
        dp2[31][1][1][1] = 0;
        for (int i = 31;i;--i)
            for (int a = 0;a < 2;++a)
                for (int b = 0;b < 2;++b)
                    for (int c = 0;c < 2;++c)
                        for (int p1 = 0;p1 < 2;++p1)
                            for (int p2 = 0;p2 < 2;++p2)
                            {
                                const int p3 = p1 ^ p2;
                                const int x1 = ((x >> (i - 1)) & 1);
                                const int y1 = ((y >> (i - 1)) & 1);
                                const int k1 = ((k >> (i - 1)) & 1);
                                if (a && x1 < p1) continue;
                                if (b && y1 < p2) continue;
                                if (c && k1 < p3) continue;
                                add(dp1[i - 1][a & (p1 == x1)][b & (p2 == y1)][c & (p3 == k1)],dp1[i][a][b][c]);
                                add(dp2[i - 1][a & (p1 == x1)][b & (p2 == y1)][c & (p3 == k1)],dp2[i][a][b][c]);
                                add(dp2[i - 1][a & (p1 == x1)][b & (p2 == y1)][c & (p3 == k1)],mult(p3 << (i - 1),dp1[i][a][b][c]));
                            }
        int ans = 0;
        for (int a = 0;a < 2;++a)
            for (int b = 0;b < 2;++b)
                for (int c = 0;c < 2;++c)
                    add(ans,dp1[0][a][b][c]),
                    add(ans,dp2[0][a][b][c]);
        return ans;
    };
    fi>>t;
    while (t --)
    {
        int x1,y1,x2,y2,k;
        fi>>x1>>y1>>x2>>y2>>k;
        --x1;--y1;--x2;--y2;--k;
        ll ans = 0;
        add(ans,query(x2,y2,k));
        add(ans,query(x1 - 1,y1 - 1,k));
        add(ans,mod - query(x1 - 1,y2,k));
        add(ans,mod - query(x2,y1 - 1,k));
        fo << ans << '\n';
    }
    cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

const long long mod = 1000000007;
const int n = 30;

int xx, yy, zz;
long long re_f[35][2][2][2], re_g[35][2][2][2];
int sign_f[35][2][2][2], sign_g[35][2][2][2], timer;

int get_bit(int x, int i){
    return (x>>(i-1))%2;
}

long long f(int i, int x, int y, int z){
    if(i==0) return 1;
    if(sign_f[i][x][y][z]==timer) return re_f[i][x][y][z];

    re_f[i][x][y][z] = 0;
    for(int j=0;j<=1;j++){
        for(int k=0;k<=1;k++){
            if(x==0 && j>get_bit(xx,i)) continue;
            if(y==0 && k>get_bit(yy,i)) continue;
            if(z==0 && (j^k)>get_bit(zz,i)) continue;
            re_f[i][x][y][z] =
                (re_f[i][x][y][z] + f(i-1,x|(j<get_bit(xx,i)),y|(k<get_bit(yy,i)),z|((j^k)<get_bit(zz,i))))%mod;
        }
    }

    sign_f[i][x][y][z] = timer; return re_f[i][x][y][z];
}

long long g(int i, int x, int y, int z){
    if(i==0) return 0;
    if(sign_g[i][x][y][z]==timer) return re_g[i][x][y][z];

    re_g[i][x][y][z] = 0;
    for(int j=0;j<=1;j++){
        for(int k=0;k<=1;k++){
            if(x==0 && j>get_bit(xx,i)) continue;
            if(y==0 && k>get_bit(yy,i)) continue;
            if(z==0 && (j^k)>get_bit(zz,i)) continue;
            re_g[i][x][y][z] =
                (re_g[i][x][y][z] + ((j^k)<<(i-1))*f(i-1,x|(j<get_bit(xx,i)),y|(k<get_bit(yy,i)),z|((j^k)<get_bit(zz,i)))
                                + g(i-1,x|(j<get_bit(xx,i)),y|(k<get_bit(yy,i)),z|((j^k)<get_bit(zz,i))))%mod;
        }
    }

    sign_g[i][x][y][z] = timer; return re_g[i][x][y][z];
}

long long get(int x, int y, int value){
    if(x<0 || y<0) return 0LL;
    timer++;
    xx = x; yy = y; zz = value;
    return (g(n,0,0,0) + f(n,0,0,0))%mod;
}

int main(){
    int query; scanf("%d", &query);
    while(query--){
        int x1, x2, y1, y2, value; scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &value);
        long long res = get(x2-1,y2-1,value-1);
        res = (res + mod - get(x1-2,y2-1,value-1))%mod;
        res = (res + mod - get(x2-1,y1-2,value-1))%mod;
        res = (res + get(x1-2,y1-2,value-1))%mod;
        cout << res << "\n";
    }

    return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <set>
#include <iostream>
#define cl(a,b) memset(a,b,sizeof(a))
#define fr(a,b,c) for(int a = b; a < c; a++)
#define rp(a,b) fr(a,0,b)
#define sc(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d%d", &a, &b)
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define oo 0x3f3f3f3f

using namespace std;

typedef pair<int,int> pii;
typedef long long ll;

#define N 200010
#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<

#define MOD 1000000007LL

ll get(int i, int j, ll l, ll v, int a, int b, ll k) {
	if (v > k) return 0;
	if (i > a || j > b) return 0;
	if (i <= a && i+l-1 <= a) {
		ll tam = min(ll(b), j+l-1) - j + 1;
		ll v2 = min(k, v + l - 1);
		return (tam * ((v + v2) * (v2 - v + 1) / 2) % MOD) % MOD;
	}
	if (j <= b && j+l-1 <= b) {
		ll tam = min(ll(a), i+l-1) - i + 1;
		ll v2 = min(k, v + l - 1);
		return (tam * ((v + v2) * (v2 - v + 1) / 2) % MOD) % MOD;
	}

	ll l2 = l>>1LL;
	return 	(get(i, j, l2, v, a, b, k) +
			get(i, j+l2, l2, v+l2, a, b, k) +
			get(i+l2, j, l2, v+l2, a, b, k) +
			get(i+l2, j+l2, l2, v, a, b, k)) % MOD;
}

int query(int i, int j, ll k) {
	if (i < 0 || j < 0) return 0;
	ll l = 1;
	while (l <= max(i,j)+1) l<<=1LL;
	return get(0, 0, l, 1, i, j, k);
}

int main() {
	int n; sc(n);
	rp(i,n) {
		int i1, j1, i2, j2, k;
		sc2(i1,j1);
		sc2(i2,j2);
		i1--, j1--, i2--, j2--;
		sc(k);
		printf("%lld\n", (query(i2, j2, k) - query(i2, j1-1, k) - query(i1-1, j2, k) + query(i1-1, j1-1, k) + 2*MOD) % MOD);
	}
	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <complex>
#include <cstdio>
#include <cmath>
#include <map>
#include <queue>
#include <ctime>
#define ll long long
#define INF 1000000000
#define MAXN 300000
#define fpos qwertyhfrb
#define int long long
using namespace std;
int q, i, mod = 1000000007;
int rec (int x, int y, int k, int level, int cnt = 0) {
    int side = 1 << level;
    //cout << x << " " << y << " " << k << " " << level << " " << side << endl;
    if (!x || !y)
        return 0;
    if (x == side && y == side)
        return (side * ((k * (k + 1) / 2 + k * cnt) % mod)) % mod;

    int side2 = side / 2;

    if (k <= side2) {
        if (x <= side2 && y <= side2)
            return rec(x, y, k, level - 1, cnt);

        if (x > side2 && y > side2) {
            return (rec(x - side2, y - side2, k, level - 1, cnt)
                + rec(side2, side2, k, level - 1, cnt)) % mod;
        }

        return rec(min(x, side2), min(y, side2), k, level - 1, cnt);
    }
    else {
        if (x <= side2 && y <= side2)
            return rec(x, y, side2, level - 1, cnt);

        if (x > side2 && y > side2) {
            return (rec(x - side2, side2, k - side2, level - 1, cnt + side2)
                + rec(side2, y - side2, k - side2, level - 1, cnt + side2)
             + rec(side2, side2, side2, level - 1, cnt) + rec(x - side2, y - side2, side2, level - 1, cnt)) % mod;
        }

        if (x > side2)
            return (rec(x - side2, y, k - side2, level - 1, cnt + side2) +
                rec(side2, y, side2, level - 1, cnt)) % mod;

        return (rec(x, y - side2, k - side2, level - 1, cnt + side2) +
                rec(x, side2, side2, level - 1, cnt)) % mod;
    }
}
main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie();
    cin >> q;
    for (i = 0; i < q; i++) {
        int x1, y1, x2, y2, k;
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        cout << rec(x2, y2, k, 30) - rec(x2, y1 - 1, k, 30) - rec(x1 - 1, y2, k, 30) + rec(x1 - 1, y1 - 1, k, 30) << endl;
    }
    return 0;
}

//sobskdrbhvk
//remember...
#include <bits/stdc++.h>

using namespace std;

typedef long long int LL;
typedef long double LD;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

#define PB push_back
#define MP make_pair
#define L first
#define R second
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()

//#define COMB_TRIPLE
//const int maxcomb = 1000 * 1000 + 100;
//#define COMB_DP
//const int maxcomb = 5000 + 10;

const LL Mod = 1000 * 1000 * 1000 + 7;
//const LL Mod = 998244353;

template <typename T, T fenmod = 0>
struct BIT{
    T *fen;
    int size;

    BIT(int size) { fen = new T[size + 1], this->size = size; }

    void inc(int idx, T val) {
        for (idx++; idx <= size; idx += idx & -idx) {
            fen[idx] += val;
            if (fenmod) fen[idx] %= fenmod;
        }
    }

    T get(int idx) {
        T sum = 0;
        for (; idx; idx -= idx & -idx) {
            sum += fen[idx];
            if (fenmod) sum %= fenmod;
        }
        return sum;
    }

    T get(int st, int en) { return get(en) - get(st); }
};

// Segment Tree
struct SegmentTree {
    int *seg;
    int segL, segR;

    SegmentTree(int segl, int segr) {
        segL = segl;
        segR = segr;
        seg = new int[(segr - segl) << 2];
    }

    int build(int l, int r, int id) {
        if (r - l == 1) {
            seg[id] = l;
            return seg[id];
        }
        int mid = (l + r) >> 1;
        int res = 0;
        res += build(l, mid, id << 1 | 0);
        res += build(mid, r, id << 1 | 1);
        seg[id] = res;
        return res;
    }
    int build() {
        return build(segL, segR, 1);
    }

    int pointQuery(int idx, int val, int l, int r, int id) {
        if (idx < l or r <= idx)
            return 0;
        if (r - l == 1) {
            seg[id] = val;
            return seg[id];
        }
        int mid = (l + r) >> 1;
        int res = 0;
        res += pointQuery(idx, val, l, mid, id << 1 | 0);
        res += pointQuery(idx, val, mid, r, id << 1 | 1);
        return res;
    }
    int pointQuery(int idx, int val) { return pointQuery(idx, val, segL, segR, 1); }

    int segQuery(int st, int en, int val, int l, int r, int id) {
        if (st >= r or en <= l)
            return 0;
        if (st <= l and r <= en)
            return seg[id];
        int mid = (l + r) >> 1;
        int res = 0;
        res += segQuery(st, en, val, l, mid, id << 1 | 0);
        res += segQuery(st, en, val, mid, r, id << 1 | 1);
        return res;
    }
    int segQuery(int st, int en, int val) { return segQuery(st, en, val, segL, segR, 1); }
};

class SuffixStructure {
    string s;
    int *sa;
    int *pos;
    int *lcp;

    int tn;
    int *par;
    int *h;
    vector<pair<int, pii>> *adj;

    SuffixStructure(string str) {
        s = str + "$";
    }

    void build_sa() {
        int n = sz(s);
        sa = new int[n];
        pos = new int[n];
        for (int i = 0; i < n; i++)
            sa[i] = i, pos[i] = s[i];
        int gap;
        auto cmp = [&](int i, int j) -> bool {
            if (pos[i] != pos[j]) return pos[i] < pos[j];
            i += gap, j += gap;
            return (i < n and j < n) ? pos[i] < pos[j] : i > j;
        };
        for (gap = 1; ; gap <<= 1) {
            sort(sa, sa + n, cmp);
            int tmp = 0;
            pos[sa[0]] = 0;
            for (int i = 1; i < n; i++) {
                tmp += cmp(sa[i - 1], sa[i]);
                pos[sa[i]] = tmp;
            }
            if (tmp == n - 1) break;
        }
    }

    void build_lcp() {
        int n = sz(s);
        lcp = new int[n];
        for (int i = 0, cur = 0; i < n; i++) {
            if (int j = pos[i] + 1; j < n) {
                while (max(i, j) + cur < n and s[i + cur] == s[j + cur])
                    cur++;
                lcp[pos[i] + 1] = cur;
            }
            if (cur) cur--;
        }
    }

    void build_tree() {
        int n = sz(s);
        tn = 2 * n - 1;
        adj = new vector<pair<int, pii>>[tn];
        par = new int[tn];
        h = new int[tn];
        memset(par, -1, tn * sizeof(int));
        memset(h, 0, tn * sizeof(int));
        stack<pair<int, pii>> stc;
        int verts = n;
        stc.push(MP(verts, pii(0, 0)));
        verts++;
        auto seg_break = [](pii seg, int len) {
            pii le = seg,
                ri = seg;
            le.R = le.L + len;
            ri.L = le.R;
            return MP(le, ri);
        };
        for (int i = 0; i < n; i++) {
            while (h[stc.top().L] > lcp[i]) {
                auto top = stc.top();
                stc.pop();
                if (h[stc.top().L] < lcp[i]) {
                    pair<pii, pii> brk = seg_break(top.R, lcp[i] - h[stc.top().L]);
                    stc.push(MP(verts, brk.L));
                    h[verts] = lcp[i];
                    verts++;

                    top.R = brk.R;
                }
                par[top.L] = stc.top().L;
                adj[stc.top().L].PB(top);
            }
            h[i] = n - sa[i];
            stc.push(MP(i, pii(i + h[stc.top().L], n)));
        }
        while (h[stc.top().L] > 0) {
            auto top = stc.top();
            stc.pop();
            par[top.L] = stc.top().L;
            adj[stc.top().L].PB(top);
        }
    }
};

LL Pow(LL x, LL y) {
    LL ans = 1,
       an = x % Mod;
    while (y) {
        if (y & 1LL)
            ans = ans * an % Mod;
        an = an * an % Mod;
        y >>= 1;
    }
    return ans;
}

LL Inv(LL x) { return Pow(x, Mod - 2); }

#ifdef COMB_TRIPLE
LL fact[maxcomb];
LL fact_inv[maxcomb];
LL comb(LL n, LL r) {
    static bool comped = false;
    if (!comped) {
        fact[0] = fact_inv[0] = 1;
        for (int i = 1; i < maxcomb; i++) {
            fact[i] = fact[i - 1] * i % Mod;
            fact_inv[i] = Inv(fact[i]);
        }
        comped = true;
    }
    if (r < 0 or r > n) return 0;
    return (fact[n] * fact_inv[r] % Mod) * fact_inv[n - r] % Mod;
}
#elif defined(COMB_DP)
LL comb_array[maxcomb][maxcomb];
LL comb(LL n, LL r) {
    static bool comped = false;
    if (!comped) {
        comb_array[0][0] = 1;
        for (int i = 1; i < maxcomb; i++) {
            comb_array[i][0] = 1;
            for (int j = 1; j <= i; j++)
                comb_array[i][j] = (comb_array[i - 1][j - 1] + comb_array[i - 1][j]) % Mod;
        }
        comped = true;
    }
    if (r < 0 or r > n) return 0;
    return comb_array[n][r];
}
#endif

int solve();

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while (q--)
		solve();
	return 0;
}

LL getpw2(LL base, LL r, LL c, LL k) {
    if (r > c) swap(r, c);
    LL cm1 = (1 << c) - 1;
    base ^= base & cm1;
    if (base >= k)
        return 0;
    LL ri = min(1LL << c, k - base);
    LL sum = (ri * (ri - 1) / 2) % Mod;
    LL cnt = ri << r;
    LL res = ((base + 1) * cnt + (sum << r)) % Mod;
    return res;
}

LL get(int x, int y, LL k) {
    LL ans = 0;
    int xt = x;
    for (int i = 0; xt; i++)
        if (((xt >> i) & 1LL)) {
            xt ^= 1 << i;
            int yt = y;
            for (int j = 0; yt; j++)
                if (((yt >> j) & 1)) {
                    yt ^= 1 << j;
                    ans = (ans + getpw2(xt ^ yt, i, j, k)) % Mod;
                }
        }
    return ans;
}

int solve() {
    LL x1, x2, y1, y2, k;
    cin >> x1 >> y1 >> x2 >> y2 >> k;
    x1--, y1--;
    LL ans = get(x2, y2, k) - get(x1, y2, k) - get(x2, y1, k) + get(x1, y1, k);
    ans %= Mod;
    ans = (ans + Mod) % Mod;
    cout << ans << '\n';
    return 0;
}

#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>

#define ll long long
#define f first
#define s second
#define pii pair<int, int>
#define pb push_back
#define debug(a) printf("//debug %d\n", a)
#define eos '\0'

#define INF 1000000000000000000
#define MAXN 100000
#define MOD 1000000007

using namespace std;

ll k;

bool less1(ll x1, ll y1, ll x2, ll y2){
	if(x1<=x2&&y1<=y2)return true;
	return false;
}

ll val(ll x, ll y){
	return x^y;
}

ll sum(ll x1, ll y1, ll x2, ll y2){
	ll len = x2-x1+1;
	ll ans = (len*(len+1)/2)%MOD;
	ans = (ans*len)%MOD;
	ll temp = (len*len)%MOD;
	temp = (temp*val(x1, y1))%MOD;
	ans = (ans+temp)%MOD;
}

ll query(ll x1, ll y1, ll x2, ll y2, ll x3, ll y3, ll x4, ll y4){
	if(!( less1(x1, y1, x4, y4) && less1(x3, y3, x2, y2) )){
		return 0;
	}
	if( less1(x1, y1, x3, y3) && less1(x4, y4, x2, y2) && val(x3, y3)<=k){
		return sum(x3, y3, x4, y4);
	}
	if( less1(x1, y1, x3, y3) && less1(x4, y4, x2, y2) && val(x3, y3)>k){
		return 0;

	}
	ll midx = (x3+x4)/2, midy = (y3+y4)/2;
	return query(x1, y1, x2, y2, x3, y3, midx, midy)+query(x1, y1, x2, y2, midx+1, y3, x4, midy)+query(x1, y1, x2, y2, x3, midy+1, midx, y4)+query(x1, y1, x2, y2, midx+1, midy+1, x4, y4);
}

int main(){
	ll q, x1, x2, y1, y2, m, p, i;
	cin>>q;
	while(q--){
		cin>>x1>>y1>>x2>>y2>>k;
		x1--; x2--; y1--; y2--; k--;
		if(x2==0 && y2==0){
			cout<<1<<endl;
			continue;
		}
		p = 1; m = max(x2, y2);
		while(p<=m){
			p *= 2;
		}
		cout<<query(x1, y1, x2, y2, 0, 0, p-1, p-1)<<endl;
	}
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define ll long long
#define N 100010
#define P 1000000007
#define mp(x,y) make_pair((x),(y))
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
typedef pair<int,int> pii;
struct data
{
	int x,y,k;
	bool operator <(const data&a) const
	{
		return x<a.x||x==a.x&&y<a.y||x==a.x&&y==a.y&&k<a.k;
	}
	bool operator ==(const data&a) const
	{
		return x==a.x&&y==a.y&&k==a.k;
	}
};
map<data,pii> f;
pii operator +(pii a,pii b)
{
	return mp(a.first+b.first,a.second+b.second);
}
pii calc(int x,int y,int k)
{
	if (x>y) swap(x,y);
	data qwq=(data){x,y,k};
	if (f.find(qwq)!=f.end()) return f[qwq];
	if (x==0||y==0||k<=0) return f[qwq]=mp(0,0);
	if (x==1&&y==1) return f[qwq]=mp(1,1);
	int u=1;
	while ((u<<1)<max(x,y)) u<<=1;
	pii ans=mp(0,0);
	if (x<=u)
	{
		ans=calc(x,u,k);
		pii tmp=calc(x,y-u,k-u);
		ans=ans+tmp;ans.first=(ans.first+1ll*tmp.second*u)%P;
	}
	else
	{
		ans=calc(u,u,k);
		ans=ans+calc(x-u,y-u,k);
		pii tmp=calc(x-u,u,k-u);
		ans=ans+tmp;ans.first=(ans.first+1ll*tmp.second*u)%P;
		tmp=calc(u,y-u,k-u);
		ans=ans+tmp;ans.first=(ans.first+1ll*tmp.second*u)%P;
	}
	return f[qwq]=ans;
}
signed main()
{
	int q=read();
	while (q--)
	{
		int x1=read(),y1=read(),x2=read(),y2=read(),k=read();
		int ans=0;
		ans=(ans+calc(x2,y2,k).first)%P;
		ans=(ans-calc(x1-1,y2,k).first+P)%P;
		ans=(ans-calc(x2,y1-1,k).first+P)%P;
		ans=(ans+calc(x1-1,y1-1,k).first)%P;
		printf("%d\n",ans);
	}
	return 0;
	//NOTICE LONG LONG!!!!!
}

#include<map>
#include<set>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define sz(a) ((int)(a).size())
#define all(x) (x).begin(),(x).end()
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define R(a) scanf("%d",&a)
#define per(i,a,b) for(int i=a;i>=int(b);i--)
#define rep(i,a,b) for(int i=a;i<=int(b);i++)
#define rix(i,x) for(int i=last[x];i;i=pre[i])
#define block(x) int l=x*L,r=min((x+1)*L,n)-1
const ld eps=1e-8;
int sgn(ld x){return x<-eps?-1:x>eps;}
auto Mn(auto&a,auto b){if(a>b){a=b;return 1;}return 0;}
auto Mx(auto&a,auto b){if(a<b){a=b;return 1;}return 0;}
ll gcd(ll a,ll b){return a==0?b:gcd(b%a,a);}
ll inv(ll a,ll b){return a==1?1:(1-b*inv(b%a,a))/a;}
ll exg(ll a,ll b,int&x,int&y){!a?x=0,y=1:(exg(b%a,a,y,x),x-=b/a*y);}
ll pow(ll a,ll b,ll M) {ll t=1;for(a%=M;b;b>>=1){if(b&1)t=t*a%M;a=a*a%M;}return t;}
const int M=1e9+7;
int get(int x,int y,int k,int d){
	if(x<1||y<1||k<1)return 0;
	if(x>y)swap(x,y);
	int i=31-__builtin_clz(x);
	int j=31-__builtin_clz(y);
	if(y==(1<<j)){
		if(y<=k)return 1LL*(1+y)*y/2%M*x%M+1LL*d*x%M*y%M;
		return 1LL*(1+k)*k/2%M*x%M+1LL*d*k%M*x%M;;
	}
	if(i==j)return (0LL+get(1<<i,1<<i,k,d)+get(x-(1<<i),1<<i,k-(1<<i),d+(1<<i))+
					get(1<<i,y-(1<<i),k-(1<<i),d+(1<<i))+get(x-(1<<i),y-(1<<i),k,d))%M;
	//if(x!=(1<<i))return (get(1<<i,y,k,d)+get(x-(1<<i),y,k-(1<<i),d))%M;
	return (get(x,1<<j,k,d)+get(x,y-(1<<j),k-(1<<j),d+(1<<j)))%M;
}
int main(){
	cout<<get(7,30,123412,0)<<endl;
	int T;cin>>T;while(T--){
		int lx,ly,rx,ry,k;
		cin>>lx>>ly>>rx>>ry>>k;
		cout<<((0LL+get(rx,ry,k,0)+get(lx-1,ly-1,k,0)-get(rx,ly-1,k,0)-get(lx-1,ry,k,0))%M+M)%M<<endl;
	}
}