#include <bits/stdc++.h>
int main() {
  int q, i, a[10000];
  long long int n, m, sum, x, d, l, k, xh, ge;
  while (~scanf("%d", &q)) {
    while (q--) {
      memset(a, 0, sizeof(a));
      sum = 0;
      scanf("%lld %lld", &n, &m);
      ge = m % 10;
      for (i = 0, d = m; i < 10; i++, d += m) {
        a[i] = d % 10;
        sum += a[i];
      }
      l = n / (m * 10);
      k = l * sum;
      xh = (n - l * m * 10) / m;
      for (i = 0; i < xh; i++) k += a[i];
      printf("%lld\n", k);
    }
  }
}
