#include <bits/stdc++.h>
using namespace std;
long long t, n, m, f[1000010], ans;
long long a[1000010], b[1000010], c[1000010], s[1000010], d[1000010],
    e[1000010];
bool vis[1000010];
inline void work() {
  register long long cnt = 0;
  memset(vis, 1, sizeof(vis)), d[1] = 1;
  for (register long long i = 2; i <= 1000000; ++i) {
    if (vis[i]) cnt++, e[cnt] = i, d[i] = i - 1;
    for (register long long j = 1; j <= cnt; ++j) {
      if (i * e[j] > 1000000) break;
      vis[i * e[j]] = false;
      if (i % e[j] == 0) {
        d[i * e[j]] = e[j] * d[i];
        break;
      } else
        d[i * e[j]] = (e[j] - 1) * d[i];
    }
  }
}
inline void init() {
  for (register long long i = 1; i <= 1000000; i++)
    for (long long j = i, t = 1; j <= 1000000; j += i, t++)
      (s[j] += t * d[i] % 1000000007) % 1000000007;
  for (register long long i = 1; i <= 1000000; i++)
    s[i] = (s[i] - i + 1000000007) % 1000000007;
  for (register long long i = 1; i <= 1000000; i++) {
    register long long sum =
        (2ll * i % 1000000007 * i % 1000000007 * i % 1000000007 -
         6ll * i % 1000000007 * i % 1000000007 + 7ll * i % 1000000007 +
         1000000007) *
        333333336 % 1000000007;
    f[i] = (sum + 2 * s[i]) % 1000000007;
  }
  for (register long long i = 1; i <= 1000000; i++) {
    a[i] = (a[i - 1] + 1ll * f[i]) % 1000000007;
    b[i] = (b[i - 1] + 1ll * i * f[i] % 1000000007) % 1000000007;
    c[i] =
        (c[i - 1] + 1ll * i * i % 1000000007 * f[i] % 1000000007) % 1000000007;
  }
}
int main() {
  scanf("%lld", &t);
  work();
  init();
  for (register int i = 1; i <= t; ++i) {
    scanf("%lld%lld", &n, &m);
    long long x = min(n, m);
    ans = (n * m + n + m + 1) % 1000000007 * a[x] % 1000000007;
    ans = (ans - (n + m + 2) % 1000000007 * b[x] % 1000000007 + c[x] +
           1000000007) %
          1000000007;
    printf("%lld\n", ans);
  }
}
