#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U >> 1;
const int Mod = int(1e9) + 7;
const int maxN = 1000000 + 100;
template <class T>
void tension(T &a, const T &b) {
  if (b < a) a = b;
}
template <class T>
void relax(T &a, const T &b) {
  if (b > a) a = b;
}
int phi[maxN], s_gcd[maxN], p_s[maxN], pi_s[maxN], pi2_s[maxN];
inline void addMod(int &x, int y) {
  x += y;
  if (x >= Mod) x -= Mod;
}
inline long long powMod(long long a, long long x) {
  a %= Mod;
  long long ret = 1;
  for (; x; x >>= 1, a = a * a % Mod)
    if (x & 1) ret = ret * a % Mod;
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  int n = 1000000;
  for (int i = 1; i <= n; ++i) phi[i] = i;
  for (int i = 1; i <= n; ++i)
    for (int j = i + i; j <= n; j += i) phi[j] -= phi[i];
  for (int i = 1; i <= n; ++i)
    for (int j = i; j <= n; j += i)
      addMod(s_gcd[j], 1ll * (i - 1) * phi[j / i] % Mod);
  for (int i = 1; i <= n; ++i) {
    p_s[i] = (p_s[i - 1] + s_gcd[i]) % Mod;
    pi_s[i] = (pi_s[i - 1] + 1ll * i * s_gcd[i] % Mod) % Mod;
    pi2_s[i] = (pi2_s[i - 1] + 1ll * i * s_gcd[i] % Mod * i % Mod) % Mod;
  }
  int _;
  for (cin >> _; _--;) {
    long long N, M;
    cin >> N >> M;
    if (N > M) swap(N, M);
    long long ans =
        ((N + 1) * (M + 1) % Mod * p_s[N] - (N + M + 2) * pi_s[N] + pi2_s[N]) %
        Mod * 2 % Mod;
    ans += -((N - 1) * N % Mod * (N + 1) % Mod * (N + 2) % Mod *
             ((3 + M * (27 - 6 * N) + N * (-17 + 4 * N)) % Mod)) %
           Mod * powMod(180, Mod - 2) % Mod;
    ans += (-N * (N + 1) % Mod * (N + 2) % Mod) *
           ((3 * N * N + N - 4 - 5 * M * (1 + N)) % Mod) % Mod *
           powMod(60, Mod - 2) % Mod;
    ans %= Mod, ans += Mod, ans %= Mod;
    cout << ans << endl;
  }
  return 0;
}
