#include <bits/stdc++.h>
using namespace std;
namespace SHENZHEBEI {
static const int GYN = 2333333;
char SZB[GYN], *S = SZB, *T = SZB;
inline char gc() {
  if (S == T) {
    T = (S = SZB) + fread(SZB, 1, GYN, stdin);
    if (S == T) return '\n';
  }
  return *S++;
}
inline long long read() {
  long long x = 0, g = 1;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    if (ch == '-') g = -1;
  for (; isdigit(ch); ch = gc()) x = x * 10 - 48 + ch;
  return x * g;
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
inline char readchar() {
  char ch = gc();
  for (; isspace(ch); ch = gc())
    ;
  return ch;
}
inline long long readstr(char *s) {
  char ch = gc();
  int cur = 0;
  for (; isspace(ch); ch = gc())
    ;
  for (; !isspace(ch); ch = gc()) s[cur++] = ch;
  s[cur] = '\0';
  return cur;
}
void Print(long long *a, int s, int t) {
  for (long long i = (long long)(s); i <= (long long)(t); ++i)
    printf("%lld ", a[i]);
  puts("");
}
void Print(int *a, int s, int t) {
  for (long long i = (long long)(s); i <= (long long)(t); ++i)
    printf("%d ", a[i]);
  puts("");
}
void Print(char *a, int s, int t) {
  for (long long i = (long long)(s); i <= (long long)(t); ++i) putchar(a[i]);
  puts("");
}
void writeln(long long x) {
  write(x);
  putchar('\n');
}
}  // namespace SHENZHEBEI
using namespace SHENZHEBEI;
const long long N = 1000010, mod = 1e9 + 7;
long long mark[N], pri[N], phi[N], sum[N], ans1[N], ans2[N], ans3[N], n, m;
void Init() {
  phi[1] = 1;
  for (long long i = (long long)(2); i <= (long long)(n); ++i) {
    if (!mark[i]) pri[++pri[0]] = i, phi[i] = i - 1;
    for (long long j = 1; j <= pri[0] && i * pri[j] <= n; ++j) {
      mark[i * pri[j]] = 1;
      if (!(i % pri[j])) {
        phi[i * pri[j]] = phi[i] * pri[j];
        break;
      }
      phi[i * pri[j]] = phi[i] * (pri[j] - 1);
    }
  }
  for (long long Gcd = (long long)(1); Gcd <= (long long)(n); ++Gcd)
    for (long long i = Gcd; i <= n; i += Gcd)
      ((sum[i]) = ((sum[i]) + (Gcd * phi[i / Gcd])) % mod);
}
int main() {
  n = 1e6;
  Init();
  for (long long i = (long long)(1); i <= (long long)(n); ++i) {
    long long val =
        (i * (i + 1) * (2 * i + 1) / 3 % mod - 3 * i * i + 2 * sum[i]) % mod;
    ans1[i] = (ans1[i - 1] + val) % mod;
    ans2[i] = (ans2[i - 1] + val * (i - 1)) % mod;
    ans3[i] = (ans3[i - 1] + val * (i - 1) % mod * (i - 1)) % mod;
  }
  for (long long T = read(); T--;) {
    n = read(), m = read();
    if (n > m) swap(n, m);
    long long ans = (n * m % mod * ans1[n] - (n + m) * ans2[n] + ans3[n]) % mod;
    ((ans) = ((ans) + (mod)) % mod);
    writeln(ans);
  }
}
