#include <bits/stdc++.h>
using namespace std;
void solve() {
  string s;
  cin >> s;
  int x;
  cin >> x;
  int n = s.size();
  string p;
  map<int, int> mp;
  for (int i = 0; i < n; i++) p.push_back('%');
  int ans = 0;
  int flag = 0, f1 = 0, f2 = 0;
  for (int i = 0; i < n; i++) {
    flag = 0;
    f1 = 0;
    f2 = 0;
    if (s[i] == '0') {
      if (i - x >= 0) {
        flag = 1;
        if (!mp[i - x]) {
          p[i - x] = '0';
          mp[i - x] = 1;
        } else {
          if (p[i - x] == '1') {
            ans = -1;
            break;
          }
        }
      }
      if (i + x < n) {
        p[i + x] = '0';
        mp[i + x] = 1;
      }
    } else {
      if (i - x >= 0) {
        flag = 1;
        if (!mp[i - x]) {
          p[i - x] = '1';
          mp[i - x] = 1;
        } else if (p[i - x] == '1')
          p[i - x] = '1';
        else {
          f1 = 1;
        }
      }
      if (flag == 0 || f1 == 1) {
        if (i + x < n) {
          mp[i + x] = 1;
          p[i + x] = '1';
        } else {
          f2 = 1;
        }
      }
      if (f2 == 1) {
        ans = -1;
        break;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (p[i] == '%') p[i] = '1';
  }
  if (ans == -1)
    cout << "-1" << endl;
  else
    cout << p << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
}
