#include <bits/stdc++.h>
using namespace std;
const int p = 1000000007;
const int N = 1000000;
int sq[1200000], phi[1200000], gcd[1200000];
int h0[1200000], h1[1200000], h2[1200000];
bool nisp[1200000];
int h, i, j, n, s, t, x, y;
int main() {
  for (i = 1; i <= N; i++) sq[i] = (sq[i - 1] + 1LL * i * i) % p;
  for (i = 1; i <= N; i++) phi[i] = i;
  for (i = 2; i <= N; i++)
    if (!nisp[i])
      for (j = i; j <= N; j = j + i)
        nisp[j] = true, phi[j] = phi[j] / i * (i - 1);
  for (i = 1; i <= N; i++)
    for (j = i; j <= N; j = j + i) gcd[j] = (gcd[j] + 1LL * j / i * phi[i]) % p;
  for (i = 1; i <= N; i++) {
    t = (2LL * sq[i - 1] - 1LL * i * i % p + 2LL * gcd[i] + p) % p;
    h0[i] = (h0[i - 1] + t) % p;
    h1[i] = (h1[i - 1] + 1LL * t * i) % p;
    h2[i] = (h2[i - 1] + 1LL * t * i % p * i) % p;
  }
  scanf("%d", &n);
  for (h = 1; h <= n; h++) {
    scanf("%d%d", &x, &y);
    if (x > y) swap(x, y);
    s = (1LL * (x + 1) * (y + 1) % p * h0[x] % p -
         1LL * (x + y + 2) * h1[x] % p + h2[x] + p) %
        p;
    printf("%d\n", s);
  }
  return 0;
}
