#include <bits/stdc++.h>
using namespace std;
set<long long int> dgt;
int dgts[100] = {0};
void init() {
  dgt.clear();
  memset(dgts, 0, sizeof(dgts));
}
void solve() {
  long long int n, m;
  scanf("%lld %lld", &n, &m);
  long long int t = m % 10;
  long long int sum = 0;
  int count = 0;
  while (dgt.find(t % 10) == dgt.end()) {
    count++;
    dgts[count] = dgts[count - 1] + t;
    sum += t;
    dgt.insert(t % 10);
    t = (t + m) % 10;
  }
  printf("%lld\n", sum * ((n / m) / count) + dgts[(n / m) % count]);
}
int main() {
  int n;
  scanf("%d", &n);
  while (n--) {
    init();
    solve();
  }
  return 0;
}
