#include <bits/stdc++.h>
using namespace std;
bool composite[1000001];
long long phi[1000001], g[1000001];
long long s0[1000001], s1[1000001], s2[1000001];
int main() {
  for (int i = 1; i < 1000001; i++) phi[i] = 1;
  for (int p = 2; p < 1000001; p++) {
    if (composite[p]) continue;
    if (1LL * p * p < 1000001) {
      for (int i = p * p; i < 1000001; i += p) composite[i] = true;
    }
    for (int i = p; i < 1000001; i += p) phi[i] *= p - 1;
    int mul = p;
    while (1LL * mul * p < 1000001) {
      mul *= p;
      for (int i = mul; i < 1000001; i += mul) phi[i] *= p;
    }
  }
  for (int d = 1; d < 1000001; d++) {
    for (int i = d; i < 1000001; i += d) {
      g[i] += d * phi[i / d];
      g[i] %= 1000000007;
    }
  }
  for (int k = 1; k < 1000001; k++) {
    long long c = 0;
    c += 1LL * k * (k + 1) * (2 * k + 1) / 3 - 1LL * 3 * k * k + 1000000007;
    c += 1LL * 2 * g[k];
    c %= 1000000007;
    s0[k] = s0[k - 1] + c;
    s0[k] %= 1000000007;
    s1[k] = s1[k - 1] + c * k;
    s1[k] %= 1000000007;
    s2[k] = s2[k - 1] + c * k % 1000000007 * k;
    s2[k] %= 1000000007;
  }
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, m;
    scanf("%d%d", &n, &m);
    int k = min(n, m);
    long long total = 0;
    total += s0[k] * (n + 1) % 1000000007 * (m + 1);
    total += (1000000007 - s1[k]) * (n + m + 2);
    total += s2[k];
    total %= 1000000007;
    printf("%lld\n", total);
  }
}
