1
2 2
1 1
1 2
 1
1 3
3
1 2 3
 1
1 1
1000
1000
 1
2 2
2 2
2 2
 5
4 5
10 8 6 4
1 2 3 4 5
1 1
3
3
1 1
3
2
5 3
1000 2 2 2 3
3 1 5
5 5
1 2 3 4 5
1 2 3 4 5
 1
4 4
1 1 1 1
1 2 3 4
 1
2 3
1 1
1 2 3

YES
1 1
 YES
1 3
 YES
1 1000
 YES
1 2
 YES
1 4
YES
1 3
NO
YES
1 3
YES
1 1
 YES
1 1
 YES
1 1

#include <bits/stdc++.h>
using namespace std;
int read() {
  int ans = 0, sign = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') sign = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    ans = (ans << 1) + (ans << 3) + (ch ^ 48);
    ch = getchar();
  }
  return ans * sign;
}
long long gcd(long long x, long long y) { return y == 0 ? x : (gcd(y, x % y)); }
bool a[100005];
int main() {
  int T;
  cin >> T;
  string s;
  while (T--) {
    int i, x;
    for (int i = 0; i < 100003; i++) a[i] = 1;
    cin >> s >> x;
    int len = s.length();
    int flag = 1;
    for (i = 0; i < len; i++) {
      if (s[i] == '0') {
        if (i - x >= 0) a[i - x] = 0;
        if (i + x < len) a[i + x] = 0;
      }
    }
    for (i = 0; i < len; i++) {
      if (s[i] == '1') {
        int yx = 0;
        if (i - x >= 0) {
          if (a[i - x]) yx++;
        }
        if (i + x < len)
          if (a[i + x]) yx++;
        if (yx == 0) {
          flag = 0;
          break;
        }
      }
    }
    if (!flag)
      cout << "-1" << endl;
    else
      for (i = 0; i < len; i++) cout << a[i];
    cout << endl;
  }
}
