#include <bits/stdc++.h>
using namespace std;
const int N = 1000000, nn = N + 10;
long long is[nn], phi[nn], s[nn], s0[nn], s1[nn], s2[nn], c1[nn], c2[nn],
    c3[nn];
void add(long long &x, long long y) { x = (x + y) % (1000000007ll); }
long long calc(long long x, long long y, long long z) {
  if (x % 3 == 0)
    x /= 3;
  else if (y % 3 == 0)
    y /= 3;
  else if (z % 3 == 0)
    z /= 3;
  return (long long)x * (long long)y % (1000000007ll) * (long long)z %
         (1000000007ll);
}
int main() {
  for (int i = 1; i <= (int)(N); ++i) is[i] = 1, phi[i] = i;
  is[1] = 0;
  for (int i = 2; i <= (int)(N); ++i) {
    if (!is[i]) continue;
    phi[i]--;
    for (int j = i + i; j <= N; j += i)
      is[j] = 0, phi[j] = phi[j] / i * (i - 1);
  }
  for (int k = 1; k <= (int)(N); ++k)
    for (int i = k; i <= N; i += k) add(s[i], phi[i / k] * k);
  for (int i = 1; i <= (int)(N); ++i) {
    add(s0[i], s0[i - 1] + s[i]);
    add(s1[i], s1[i - 1] + (long long)i * (long long)s[i] % (1000000007ll));
    add(s2[i], s2[i - 1] + (long long)i * (long long)i % (1000000007ll) *
                               (long long)s[i] % (1000000007ll));
    long long L = i;
    long long tmp =
        ((long long)calc(L, L + 1, 2 * L + 1) - 3ll * L * L) % (1000000007ll);
    add(c1[i], c1[i - 1] + tmp);
    add(c2[i], c2[i - 1] + tmp * (long long)i % (1000000007ll));
    add(c3[i], c3[i - 1] + tmp * (long long)i % (1000000007ll) * (long long)i %
                               (1000000007ll));
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int n, m;
    scanf("%d%d", &n, &m);
    if (n > m) swap(n, m);
    long long ans = (long long)(n + 1) * (long long)(m + 1) % (1000000007ll) *
                        (c1[n] + 2ll * s0[n]) % (1000000007ll) +
                    (c3[n] + 2ll * s2[n]) % (1000000007ll) -
                    (long long)(c2[n] + s1[n] * 2ll % (1000000007ll)) %
                        (1000000007ll) * (long long)(m + n + 2) %
                        (1000000007ll);
    ans %= (1000000007ll);
    cout << (ans + (1000000007ll)) % (1000000007ll) << endl;
  }
  return 0;
}
