#include <bits/stdc++.h>
using namespace std;
long long cif(long long m) { return m % 10; }
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    vector<long long> v;
    v.push_back(cif(m));
    long long x = m * 2;
    while (cif(x) != v[0]) {
      v.push_back(cif(x));
      x += m;
    }
    long long sum = 0;
    for (long long i = 0; i < v.size(); i++) sum += v[i];
    x = n / m;
    long long ans = 0;
    ans += (x / v.size()) * sum;
    for (long long i = 0; i < x % v.size(); i++) ans += v[i];
    cout << ans << endl;
  }
}
