#include <bits/stdc++.h>
using namespace std;
int flag;
int main() {
  int T;
  cin >> T;
  for (; T--;) {
    int n, x;
    string s;
    cin >> s >> x;
    n = s.size();
    vector<int> pd(n, -1);
    flag = 1;
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        if (i - x >= 0) pd[i - x] = 0;
        if (i + x < n) pd[i + x] = 0;
      }
    }
    for (int i = 0; i < n; i++)
      if (pd[i] < 0) pd[i] = 1;
    string ans;
    for (int i = 0; i < n; i++) {
      int w = 0;
      if (i - x >= 0 && pd[i - x]) w = 1;
      if (i + x < n && pd[i + x]) w = 1;
      if (w != s[i] - '0') flag = 0;
      ans += (pd[i] + '0');
    }
    if (flag)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
