#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 1000000;
const int kMod = 1000000007;
int pr[kMaxN + 1];
bool notPr[kMaxN + 1];
int f[kMaxN + 1];
int g[kMaxN + 1], h[kMaxN + 1], z[kMaxN + 1];
int main() {
  f[1] = 1;
  for (int i = 2; i <= kMaxN; ++i) {
    if (!notPr[i]) {
      pr[++pr[0]] = i;
      f[i] = i + i - 1;
    }
    for (int j = 1, k; j <= pr[0] && (k = pr[j] * i) <= kMaxN; ++j) {
      notPr[k] = true;
      int x = k, c = 0, t = 1;
      while (x % pr[j] == 0) {
        x /= pr[j];
        ++c;
        t *= pr[j];
      }
      f[k] = (long long)f[x] * ((c + 1) * t - c * (t / pr[j])) % kMod;
      if (i % pr[j] == 0) break;
    }
  }
  for (int i = 1; i <= kMaxN; ++i) {
    int a = i, b = i + 1, c = i * 2 + 1;
    if (a % 3 == 0) {
      a /= 3;
    } else if (b % 3 == 0) {
      b /= 3;
    } else {
      c /= 3;
    }
    int tmp = (long long)a * b % kMod * c % kMod;
    tmp -= (long long)i * i % kMod * 3 % kMod;
    tmp = (tmp % kMod + kMod) % kMod;
    f[i] = (long long)2 * f[i] % kMod;
    f[i] = (f[i] + tmp) % kMod;
    g[i] = (g[i - 1] + f[i]) % kMod;
    h[i] = (h[i - 1] + (long long)(i - 1) * f[i] % kMod) % kMod;
    z[i] =
        (z[i - 1] + (long long)(i - 1) * (i - 1) % kMod * f[i] % kMod) % kMod;
  }
  int t, n, m;
  for (scanf("%d", &t); t--;) {
    scanf("%d%d", &n, &m);
    if (n > m) swap(n, m);
    int res = (long long)n * m % kMod * g[n] % kMod;
    res = (res + z[n]) % kMod;
    res = (res - (long long)(n + m) * h[n] % kMod) % kMod;
    res = (res + kMod) % kMod;
    printf("%d\n", res);
  }
  return 0;
}
