#include <bits/stdc++.h>
using namespace std;
int T, N, M, A[1000005], B[1000005], C[1000005], Phi[1000005];
bool V[1000005];
inline void Add(int& x, const int& y) {
  x += y;
  if (x >= 1000000007)
    x -= 1000000007;
  else if (x <= -1000000007)
    x += 1000000007;
}
int main() {
  for (int i = 1, x = 0, y = 0; i < 1000005; i++)
    Add(x, 2 * i + 2), Add(y, 2ll * i * i % 1000000007),
        Add(A[i], (1ll * i * i * i - 1ll * x * i + y) % 1000000007);
  for (int i = 1; i < 1000005; i++) Phi[i] = i;
  for (int i = 2; i < 1000005; i++)
    if (!V[i])
      for (int j = i; j < 1000005; j += i)
        V[j] = 1, Phi[j] = Phi[j] / i * (i - 1);
  for (int i = 1; i < 1000005; i++)
    for (int j = i; j < 1000005; j += i)
      Add(A[j], 2ll * i * Phi[j / i] % 1000000007);
  for (int i = 1; i < 1000005; i++)
    B[i] = (1ll * A[i] * i + B[i - 1]) % 1000000007,
    C[i] = (1ll * A[i] * i % 1000000007 * i + C[i - 1]) % 1000000007,
    Add(A[i], A[i - 1]);
  for (scanf("%d", &T); T--;) {
    scanf("%d%d", &N, &M);
    int t = min(++N, ++M);
    printf("%d\n",
           ((1ll * N * M % 1000000007 * A[t] - 1ll * (N + M) * B[t] + C[t]) %
                1000000007 +
            1000000007) %
               1000000007);
  }
}
