#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
long long cnt[32][2][2][2];
long long sum[32][2][2][2];
vector<int> A, B, C;
long long solve(int x, int y, int k) {
  if (x < 0 || y < 0 || k < 0) {
    return 0;
  }
  memset(cnt, 0, sizeof(cnt));
  memset(sum, 0, sizeof(sum));
  A.clear(), B.clear(), C.clear();
  for (int i = 0; i <= 30; i++) {
    A.push_back(x % 2);
    x /= 2;
    B.push_back(y % 2);
    y /= 2;
    C.push_back(k % 2);
    k /= 2;
  }
  reverse(A.begin(), A.end());
  reverse(B.begin(), B.end());
  reverse(C.begin(), C.end());
  cnt[0][1][1][1] = 1;
  sum[0][1][1][1] = 0;
  for (int i = 0; i <= 30; i++) {
    for (int a = 0; a < 2; a++) {
      for (int b = 0; b < 2; b++) {
        for (int c = 0; c < 2; c++) {
          for (int b1 = 0; b1 < 2; b1++) {
            for (int b2 = 0; b2 < 2; b2++) {
              int z = b1 ^ b2;
              if (a == 1 && A[i] == 0 && b1 == 1) {
                continue;
              }
              if (b == 1 && B[i] == 0 && b2 == 1) {
                continue;
              }
              if (z == 1 && C[i] == 0 && c == 1) {
                continue;
              }
              cnt[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] +=
                  cnt[i][a][b][c];
              cnt[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] %=
                  MOD;
              sum[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] +=
                  sum[i][a][b][c];
              sum[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] %=
                  MOD;
              sum[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] +=
                  (cnt[i][a][b][c] * z * (1ll << (30 - i))) % MOD;
              sum[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] %=
                  MOD;
            }
          }
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) {
        ans += sum[31][i][j][k];
        ans %= MOD;
        ans += cnt[31][i][j][k];
        ans %= MOD;
      }
    }
  }
  return ans;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int x1, x2, y1, y2, k;
    scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
    x1--, x2--, y1--, y2--;
    k--;
    long long ans = 0;
    ans += solve(x1 - 1, y1 - 1, k);
    ans %= MOD;
    ans -= solve(x1 - 1, y2, k);
    while (ans < 0) {
      ans += MOD;
    }
    ans %= MOD;
    ans -= solve(x2, y1 - 1, k);
    while (ans < 0) {
      ans += MOD;
    }
    ans %= MOD;
    ans += solve(x2, y2, k);
    ans %= MOD;
    printf("%lld\n", ans);
  }
  return 0;
}
