#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
  return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
  return a < b ? a : b;
}
const char lf = '\n';
namespace ae86 {
const int bufl = 1 << 15;
char buf[bufl], *s = buf, *t = buf;
inline int fetch() {
  if (s == t) {
    t = (s = buf) + fread(buf, 1, bufl, stdin);
    if (s == t) return EOF;
  }
  return *s++;
}
inline int ty() {
  int a = 0;
  int b = 1, c = fetch();
  while (!isdigit(c)) b ^= c == '-', c = fetch();
  while (isdigit(c)) a = a * 10 + c - 48, c = fetch();
  return b ? a : -a;
}
}  // namespace ae86
using ae86::ty;
const int N = 1000003, _ = N + 7, mo = 1000000007;
template <typename T1, typename T2>
inline T1 ad(T1 &a, T2 b) {
  return a = a + b >= mo ? a + b - mo : a + b;
}
template <typename T1, typename T2>
inline T1 dl(T1 &a, T2 b) {
  return a = a >= b ? a - b : a - b + mo;
}
template <typename T1, typename T2>
inline T1 add(T1 a, T2 b) {
  return a + b >= mo ? a + b - mo : a + b;
}
template <typename T1, typename T2>
inline T1 del(T1 a, T2 b) {
  return a >= b ? a - b : a - b + mo;
}
long long powa(long long a, long long t) {
  long long b = 1;
  a = (a + mo) % mo;
  while (t) {
    if (t & 1) b = b * a % mo;
    a = a * a % mo, t >>= 1;
  }
  return b;
}
inline long long inva(long long a) { return powa(a, mo - 2); }
int isnp[_] = {0};
long long fai[_] = {0}, si[_] = {0}, isi[_] = {0}, i2si[_] = {0};
int main() {
  ios::sync_with_stdio(0), cout.tie(nullptr);
  for (int i = 1, a = 0, b = 0; i <= N; i++)
    ad(a, i + i + 2), ad(b, 2ll * i * i % mo),
        si[i] = (1ll * i * i % mo * i % mo - 1ll * a * i % mo + b + mo) % mo;
  isnp[1] = 1;
  for (int i = 1; i <= N; i++) fai[i] = i;
  for (int i = 2; i <= N; i++)
    if (!isnp[i])
      for (int j = i; j <= N; j += i)
        isnp[j] = 1, fai[j] = fai[j] / i * (i - 1);
  for (int i = 1; i <= N; i++)
    for (int j = i, k = 1; j <= N; j += i, k++)
      ad(si[j], 2ll * i * fai[k] % mo);
  for (int i = 1; i <= N; i++)
    isi[i] = si[i] * i % mo, i2si[i] = si[i] * i % mo * i % mo;
  for (int i = 1; i <= N; i++)
    ad(si[i], si[i - 1]), ad(isi[i], isi[i - 1]), ad(i2si[i], i2si[i - 1]);
  int t = ty();
  while (t--) {
    int a = ty() + 1, b = ty() + 1, c = min(a, b);
    cout << add(del(si[c] * a % mo * b % mo, isi[c] * (a + b) % mo), i2si[c])
         << lf;
  }
  return 0;
}
