#include <bits/stdc++.h>
using namespace std;
int a[1005];
int f[14];
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    long long op = m % 10;
    long long ed = op;
    memset(f, 0, sizeof(f));
    int top = 1;
    long long c = 0;
    a[0] = 0;
    while (f[op] == 0) {
      a[top] = op + a[top - 1];
      f[op] = 1;
      c += op;
      op = (op + ed) % 10;
      top++;
    }
    top--;
    long long er = n / m;
    er /= top;
    c = er * c;
    c += a[(n / m) % top];
    cout << c << endl;
  }
}
