#include <bits/stdc++.h>
using namespace std;
long long getRimSum(long long n, long long m) {
  n = (n) % (m * 10);
  n = n / m;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += (m * i) % 10;
  }
  return sum;
}
long long getSum(long long n, long long m) {
  if (n < m * 10) return 0;
  long long sum = 0;
  for (int i = 1; i <= 10; i++) {
    sum += (m * i) % 10;
  }
  return sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    cout << (n / (m * 10)) * getSum(n, m) + getRimSum(n, m) << "\n";
  }
  return 0;
}
