#include <bits/stdc++.h>
using namespace std;
const int L = 1000000;
const int MM = 1000000007;
long long phi[L + 5];
long long tot[L + 5];
long long A[L + 5], B[L + 5], C[L + 5], S[L + 5];
bool chk[L + 5];
void setting() {
  phi[1] = 1;
  for (int i = 1; i <= L; i++) phi[i] = i;
  for (int i = 2; i <= L; i++) {
    if (chk[i]) continue;
    for (int j = i; j <= L; j += i) {
      chk[j] = true;
      phi[j] = phi[j] * (i - 1) / i;
    }
  }
  for (int i = 1; i <= L; i++) {
    for (int j = i; j <= L; j += i) {
      tot[j] += i * phi[j / i];
      tot[j] %= MM;
    }
  }
  for (long long i = 1; i <= L; i++) {
    A[i] = (2 * i * i * i + i) / 3 - 2 * i * i + 2 * tot[i];
    A[i] %= MM;
    S[i] = S[i - 1] + A[i];
    S[i] %= MM;
  }
  for (long long i = 1; i <= L; i++) {
    B[i] = B[i - 1] + S[i];
    B[i] %= MM;
    C[i] = (C[i - 1] + (i + 1) * S[i] - B[i] + MM) % MM;
    C[i] %= MM;
  }
}
long long solve(int P, int Q) { return ((Q + 1) * B[P] - C[P] + MM) % MM; }
int main() {
  setting();
  int T;
  scanf("%d", &T);
  for (int t = 1; t <= T; t++) {
    int P, Q;
    scanf("%d%d", &P, &Q);
    printf("%lld\n", solve(min(P, Q), max(P, Q)));
  }
}
