#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long int t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    double a;
    long long int i, j, b, c;
    a = k / 2;
    i = round(a);
    b = n % k;
    if (n >= k) {
      c = int(n) - b;
      if (i <= b) {
        c = c + i;
        cout << c << endl;
      } else {
        cout << n << endl;
      }
    } else {
      if (i <= b) {
        cout << i << endl;
      } else {
        cout << n << endl;
      }
    }
  }
  return 0;
}
