#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const int mo = 1e9 + 7;
int f1[1000000 + 10], f2[1000000 + 10], f3[1000000 + 10], f[1000000 + 10],
    phi[1000000 + 10], prime[1000000 + 10], pri[1000000 + 10];
int C1(int x) { return (long long)x * (x + 1) / 2 % mo; }
int C2(int x) { return (long long)x * (x + 1) * (2 * x + 1) / 6 % mo; }
void prep() {
  phi[1] = 1;
  for (int i = 2; i <= 1000000; ++i) {
    if (!pri[i]) pri[i] = prime[++prime[0]] = i, phi[i] = i - 1;
    for (int j = 1; j <= prime[0]; ++j) {
      int t = i * prime[j];
      if (t > 1000000) break;
      pri[t] = prime[j];
      if (pri[t] == pri[i]) {
        phi[t] = phi[i] * pri[i];
        break;
      } else
        phi[t] = phi[i] * (pri[t] - 1);
    }
  }
  for (int i = 1; i <= 1000000; ++i)
    for (int j = i; j <= 1000000; j += i) f[j] += i * phi[j / i];
  for (int L = 2; L <= 1000000; ++L) {
    long long tmp = (long long)L * L % mo;
    tmp = (tmp + 2ll * (f[L] - L) + ((long long)L * L - 2 * L) * (L - 1)) % mo;
    tmp = (tmp + (-2ll * L) * C1(L - 1)) % mo;
    tmp = (tmp + 2ll * C2(L - 1)) % mo;
    f1[L] = (f1[L - 1] + tmp * L % mo * L) % mo;
    f2[L] = (f2[L - 1] + tmp * L) % mo;
    f3[L] = (f3[L - 1] + tmp) % mo;
  }
}
int main() {
  prep();
  int T;
  for (scanf("%d", &T); T--;) {
    long long n, m;
    scanf("%I64d%I64d", &n, &m);
    int L = min(n, m);
    printf("%d\n", (((n + 1) * (m + 1) % mo * f3[L] - (n + m + 2) * f2[L] +
                     f1[L] + n * m) %
                        mo +
                    mo) %
                       mo);
  }
}
