#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 9;
const int MOD = 1e9 + 7;
int phi[N];
bool notP[N];
int prime[N / 10], pN;
long long f[N];
long long pre[N];
long long preI[N];
long long preII[N];
inline void euler(int lim) {
  phi[1] = 1;
  for (int i = 2; i <= lim; ++i) {
    if (!notP[i]) {
      prime[++pN] = i;
      phi[i] = i - 1;
    }
    for (int j = 1; j <= pN && (long long)prime[j] * i <= lim; ++j) {
      notP[i * prime[j]] = true;
      if (i % prime[j] == 0) {
        phi[i * prime[j]] = phi[i] * prime[j];
        break;
      }
      phi[i * prime[j]] = phi[i] * phi[prime[j]];
    }
  }
}
int main() {
  euler(1e6);
  for (long long d = 1; d <= 1e6; ++d)
    for (int L = d; L <= 1e6; L += d)
      f[L] = (f[L] + 2 * d * phi[L / d] % MOD) % MOD;
  for (long long L = 1; L <= 1e6; ++L) {
    long long t = 0;
    t = L * L * L % MOD;
    t = (t - L * L * (L + 1) % MOD + MOD) % MOD;
    t = (t + L * (L + 1) * (2 * L + 1) / 3 % MOD) % MOD;
    t = (t - 2 * L * L % MOD + MOD) % MOD;
    f[L] = (f[L] + t) % MOD;
  }
  for (int i = 1; i <= 1e6; ++i) {
    pre[i] = (pre[i - 1] + f[i]) % MOD;
    preI[i] = (preI[i - 1] + f[i] * i % MOD) % MOD;
    preII[i] = (preII[i - 1] + f[i] * i % MOD * i % MOD) % MOD;
  }
  int Case;
  scanf("%d", &Case);
  while (Case--) {
    long long n, m;
    scanf("%I64d%I64d", &n, &m);
    if (n > m) swap(n, m);
    long long ans = 0;
    ans = (n + 1) * (m + 1) % MOD * pre[n] % MOD;
    ans = (ans - (n + m + 2) * preI[n] % MOD + MOD) % MOD;
    ans = (ans + preII[n]) % MOD;
    printf("%I64d\n", ans);
  }
  return 0;
}
