#include <bits/stdc++.h>
int main() {
  long long *n, *m, o, p, y;
  int q, t = 0, sum1, h, sum2;
  scanf("%d", &q);
  m = (long long *)malloc(q * sizeof(long long));
  n = (long long *)malloc(q * sizeof(long long));
  for (int i = 0; i < q; i++) {
    scanf("%lld %lld", n + i, m + i);
  }
  for (int i = 0; i < q; i++) {
    o = 0, sum1 = 0, sum2 = 0;
    t = 0;
    y = m[i] % 10;
    do {
      t++;
      y = (y + (m[i] % 10)) % 10;
      sum1 = sum1 + y;
    } while (y != m[i] % 10);
    o = sum1 * ((n[i] / m[i]) / t);
    h = (n[i] / m[i]) % t;
    for (int f = 0; f < h; f++) {
      sum2 = sum2 + y;
      y = (y + (m[i] % 10)) % 10;
    }
    o = o + sum2;
    printf("%lld\n", o);
  }
  return 0;
}
